I'm roughly new in PHP and I'm trying to display the contents of a multidimensional array in a table that was inside a select option list which has been selected by the user. What I'm trying to do is that the array key of the selected option is returned into a new variable such as $keyref = key($myarray);
so that I can use the key to display inside the table but i don't know how. Do I use a javascript function such as onChange=function()
in the <select>
list?
Here is the code:
<?php
$tshirt = array (
array("T-shirt A",15,"XS"),
array("T-shirt B",20,"S"),
array("T-shirt C",25,"M")
); //array("t-shirt type", price, "size")
$keyRef = 0; //the variable that will contain the selected array key
echo "<select id='shirtlist'>";
foreach($tshirt as $value){
echo "<option value='$value)'>$value[0]</option>";
}
echo "</select>";
echo "<p>Details of T-shirt:</p>";
echo "<table>";
echo "<tr>";
echo "<th>T-shirt Type</th>";
echo "<th>Price</th>";
echo "<th>Size</th>";
echo "</tr>";
echo "<tr>";
echo "<td>".$tshirt[$keyRef][0]."</td>";
echo "<td>".$tshirt[$keyRef][1]."</td>";
echo "<td>".$tshirt[$keyRef][2]."</td>";
echo "</tr>";
echo "</table>";
?>
The table manage to display the data of the first array but nothing happens when I selected the other options. As for trying javascript function I tried the json_encode($myarray)
method but that only returned the Array as String notice.