Hi Guys,

I have a list of areas in a DB and want to call these out to a Combo Box:

id | area

1 |Slough
2 |Burnham

Now I thought the below code would output these, but it does not

<?php

include("******");

function get_areas()
{
   $query = 'SELECT *
             FROM areas
             ORDER BY area';
   $result = @mysql_query($query);
   if (!$result)
     return false;
   $num_areas = @mysql_num_rows($result);
   if ($num_areas ==0)
      return false;
   $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 as $thisarea)
    {
      echo '<option value="';
      echo $thisarea['area'];
      echo '">';
      echo $thisarea['area'];
      echo '</option>';
    }
?>
</select>

Can anyone please advise?

Thanks

Chris

    First, take the @ away from the mysql function calls to see if there are errors being generated there.

    Second, the only other place I could see that it could be broken is the db_result_to_array() function. Otherwise, it looks fine to me.

      Have you done any debugging?

      Do some [man]print_r/man's to see if the arrays are being populated.

      Have you checked to see if the function db_result_to_array() is included in your include files.

        Assuming that the key areas is what you are trying to get and that function groups each field into a multi dimensional array (example $value['fieldname'][1]) you want to try this.

        <?PHP
        include("wtf.php");
        function get_areas() {
        	$query='SELECT * FROM areas ORDER BY area';
        	$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['area'] as $thisarea) {
        	print("<option value=\"{$thisarea}\">{$thisarea}</option>";
        }
        ?>
        </select>

          Thanks,

          But getting Invalid argument supplied for foreach().

          Tried changing my db structure names and also:

          foreach ($area_array[$the_region] as $thisarea) {

          But I still have problems

          Any ideas?

          Thanks

          
          <?PHP 
          include("**************");
          
          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> 
          
          

            Did you do any of what I suggested in my earlier post?

              Yes I did Thank you,

              Here's my db_result_to_array function (not in the include file). Does this help anyone help me solve my problem?

              Thanks

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

                HTML does not support combo-boxes. To create a combo-box, some complicated Javascript would be required.

                Of course, if you mean a LIST BOX, the kind created by the "select" element, that is something completely different.

                A combo box is a little-used Windows control which allows the selection of a value from a dropdown list OR a free-text entry. This is probably not what you mean (judging from the above).

                Mark

                  Had you done as I said about using [man]print_r/man you would probably have seen the problem.

                  Assuming that area is called area in your database then this change to your foreach loop

                  foreach ($area_array as $thisarea) {
                  	//print_r($thisarea);
                     print("<option value=\"".$thisarea['area']."\">".$thisarea['area']."</option>\n");
                  }
                  

                  should give results like this

                  <select class="input" name="area" id="area">
                  <option value="" selected> </option>
                  <option value="burnham">burnham</option>
                  <option value="leeds">leeds</option>
                  <option value="slough">slough</option>
                  <option value="hythe">hythe</option>
                  </select>
                    Write a Reply...