Pleased the code was useful...davidtube wrote:This has been very useful for me. Thank you.
I have a bit of a problem though. I've changed the code so the listboxes are populated from a database, and it seems to work but I have an error message when the page first loads. After selecting an option the error message vanishes. The error message I get is "Warning: Invalid argument supplied for foreach()
You can see how the code works here and which parts I've changed. Does anyone know what's wrong with the foreach loop?
Code: Select all
$otherslist=mysql_query("SELECT subcat FROM subcat WHERE catagory='Others' ORDER BY subcat"); while ($row = mysql_fetch_assoc($otherslist)) { $others[] = $row['subcat']; } $sublist=array('Games'=>$games, 'Electronics'=>$electronics, 'DVDs'=>$dvd, 'Music'=>$music, 'Others'=>$others); // Here to the end of the code remains as in the original example $value=1; if (!empty($_POST['first'])) { if (isset($sublist[$_POST['first']])) $value=$_POST['first']; } $name_list=''; // The error message I get is "Warning: Invalid argument supplied for foreach()" foreach ($sublist[$value] as $name) { $name_list.="<option value="{$name}">{$name}</option>\n"; } echo '<select name="second">'.$name_list.'</select>'; ?>
The answer is, luckily for me
Note... Please note my previous request that questions of the nature "can't get my modified code to work..." should be split off from the original topic. This is to avoid confusing others trying to get to grips with the original code.