Mr Chris Posted November 21, 2006 Share Posted November 21, 2006 Hi Guys,I have a list of areas in a DB and want to call these out to a Select Box:id | the_region ----------------1 |Slough 2 |BurnhamNow I thought the below code would output these, but it does not, it outputs the error message:Invalid argument supplied for foreach()?[code=php:0]<?PHP include("**************"); function db_result_to_array($result) { $res_array = array(); for ($count=0; $row = mysql_fetch_array($result); $count++) $res_array[$count] = $row; return $res_array; } function get_areas() { $query='SELECT * FROM prop_area ORDER BY the_region'; $result=mysql_query($query); if(FALSE==$result) return false; if((0 || FALSE)==mysql_num_rows($result)) { return false; } else { $result=db_result_to_array($result); return $result; } } ?> <select class="input" name="area" id="area"> <option value="" selected> </option> <?PHP $area_array=get_areas(); foreach ($area_array[$the_region] as $thisarea) { print("<option value=\"{$thisarea}\">{$thisarea}</option>"); } ?> </select>[/code]Can anyone please advise?ThanksChris Link to comment https://forums.phpfreaks.com/topic/27967-calling-data-from-a-db-to-a-select-box/ Share on other sites More sharing options...
CheesierAngel Posted November 21, 2006 Share Posted November 21, 2006 [code]<?PHP include("**************"); function db_result_to_array(&$result) { $res_array = array(); while($row = mysql_fetch_array($result)) { $res_array[] = $row; } return $res_array;} function get_areas() { $result = mysql_query('SELECT * FROM prop_area ORDER BY the_region'); return db_result_to_array($result); } ?> <select class="input" name="area" id="area"> <option value="" selected> </option> <?PHP $area_array = get_areas(); foreach ($area_array[$the_region] as $thisarea) { print("<option value=\"{$thisarea}\">{$thisarea}</option>"); } ?> </select>[/code] Link to comment https://forums.phpfreaks.com/topic/27967-calling-data-from-a-db-to-a-select-box/#findComment-127941 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.