Combo box in php?
Results 1 to 9 of 9

Thread: Combo box in php?

  1. #1
    Senior Member
    Join Date
    Mar 2005
    Posts
    167

    Combo box in php?

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

  2. #2
    Member
    Join Date
    May 2006
    Posts
    76
    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.

  3. #3
    .--..--...-.-.-.. rincewind456's Avatar
    Join Date
    Nov 2005
    Location
    Rimwards
    Posts
    1,355
    Have you done any debugging?

    Do some print_r()'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.

  4. #4
    Whats this for anyways?
    Join Date
    Nov 2006
    Posts
    10
    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 Code:
    <?PHP
    include("wtf.php");
    function 
    get_areas() {
        
    $query='SELECT * FROM areas ORDER BY area';
        
    $result=mysql_query($query);
        if(
    FALSE==$result)
            return 
    false;
        if((
    || 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>
    Meat cutter by day, pr0n star, I mean, Programmer by night!

    PHP | Coldfusion | DOM | Javascript | AJAX

  5. #5
    Senior Member
    Join Date
    Mar 2005
    Posts
    167
    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 Code:

    <?PHP 
    include("**************");

    function 
    get_areas() { 
        
    $query='SELECT * FROM prop_area ORDER BY the_region'
        
    $result=mysql_query($query); 
        if(
    FALSE==$result
            return 
    false
        if((
    || 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>

  6. #6
    .--..--...-.-.-.. rincewind456's Avatar
    Join Date
    Nov 2005
    Location
    Rimwards
    Posts
    1,355
    Did you do any of what I suggested in my earlier post?

  7. #7
    Senior Member
    Join Date
    Mar 2005
    Posts
    167
    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 Code:
    <?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((
    || 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>

  8. #8
    Senior Member
    Join Date
    Jul 2004
    Location
    Oxford, England
    Posts
    1,983
    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

  9. #9
    .--..--...-.-.-.. rincewind456's Avatar
    Join Date
    Nov 2005
    Location
    Rimwards
    Posts
    1,355
    Had you done as I said about using print_r() you would probably have seen the problem.

    Assuming that area is called area in your database then this change to your foreach loop
    PHP Code:
    foreach ($area_array as $thisarea) {
        
    //print_r($thisarea);
       
    print("<option value=\"".$thisarea['area']."\">".$thisarea['area']."</option>\n");

    should give results like this
    HTML Code:
    <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>
    Last edited by rincewind456; 11-21-2006 at 05:11 PM. Reason: spelling

Thread Information

Users Browsing this Thread

There are currently 3 users browsing this thread. (0 members and 3 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •