Rize S. answered 03/23/23
Master's in MISM, 25 yrs Exp: HTML Expert
There are a few issues with the code provided. First, the variable $i is not initialized before the while loop, so it should be initialized to 0. Second, the condition in the while loop should be $i < sizeof($phoneBrand) without the $i++ because the increment should be done inside the loop. Third, the check for the selected brand should be done inside the loop before displaying the data. Here's a corrected version of the code:
$brandSel = $_POST["brand"];
$i = 0;
//check if user has selected All or a specific brand display selections accordingly
while($i < sizeof($phoneBrand)){
if($brandSel == "All" OR $brandSel == $phoneBrand[$i]){
echo "<tr><td></td><td>".$phoneBrand[$i]."</td><td>".$phoneModel[$i]."</td><td>".$phonePrice[$i]."</td></tr>";
}
$i++;
}
With this code, the loop iterates through all the elements in the $phoneBrand array, and for each element, it checks if the user has selected "All" or the current brand matches the user's selection. If the condition is true, it displays the data for that brand.