Jump to content

Calling data from a db to a select box?


Mr Chris

Recommended Posts

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 |Burnham

Now 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?

Thanks

Chris
Link to comment
https://forums.phpfreaks.com/topic/27967-calling-data-from-a-db-to-a-select-box/
Share on other sites

[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]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.