Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Jun 2006
    Posts
    21
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Dynamically filled select form

    I have a piece of code

    PHP Code:
        $result mysql_query("SELECT * FROM category"
        or die(
    mysql_error());  
        
        echo 
    "<select name='catid' id='catid'>";
        
    // keeps getting the next row until there are no more to get
        
    while($row mysql_fetch_array$result )) {
        
    // Print out the contents of each row into a table
        
    echo "<option value='".$row['id']."'>"
        echo 
    $row['category'];
        echo 
    "</option>"
        echo 
    "\n"
    This is fine for an upload form (where I've got it from) but not for an edit form. I need it to haev selected=selected by the option that matches my category id.

    Just for reference the outputted html of the above is

    Code:
    <select name='catid' id='catid'><option value='1'>Cat1</option>
    <option value='2'>Cat2</option>
    <option value='3'>Cat3</option>
    <option value='4'>Cat4</option>
    </select>
    My script is getting the recordid and calling it $id

    It's then doing a sql select

    PHP Code:
    $sql "SELECT * FROM library WHERE id=$id"

    I want it to grab the catid from this (catid being a field in the table) and then add selected=selected in the initial code I posted.

    Thanks in advance,
    Ed

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Do you have the correct value in $id at the time you build your <select> tag? If so, simply add "if" statements to each iteration of your loop to check $id against what's just been selected from your category table and if it matches, echo the selected="selected" text.

    A popular way to do this is to use the ternary form of an "if" statement:

    PHP Code:
     echo "<option value='".$row['id']."' " .($row['id'] == $id "selected='selected'" "") . ">"
    http://us.php.net/ternary (page down a bit)

  • #3
    New Coder
    Join Date
    Jul 2008
    Posts
    91
    Thanks
    4
    Thanked 9 Times in 9 Posts
    erm... do you have the cat id in the url as the query string?

    otherwise it would be as simple as:

    PHP Code:
        $result mysql_query("SELECT * FROM category")  
        or die(
    mysql_error());   
         
        echo 
    "<select name='catid' id='catid'>"
        
    // keeps getting the next row until there are no more to get 
        
    while($row mysql_fetch_array$result )) { 
        
    // Print out the contents of each row into a table 
    if($_GET['cat_id'] == $row['id']){
    echo 
    "<option value=\"".$row['id']."\" selected=\"selected\">";  
    }else{
    echo 
    "<option value=\"".$row['id']."\">";  
    }
        echo 
    "<option value='".$row['id']."'>";  
        echo 
    $row['category']; 
        echo 
    "</option>";  
        echo 
    "\n"

    Last edited by scoop_987; 07-23-2008 at 10:10 PM. Reason: forgot the php tags

  • #4
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    if you want the result as $row['xxxxxx'] you must use mysql_fetch_assoc instead of mysql_fetch_array

    regards

  • #5
    Regular Coder Iszak's Avatar
    Join Date
    Jun 2007
    Location
    Perth, Western Australia
    Posts
    332
    Thanks
    2
    Thanked 58 Times in 57 Posts
    oesxyl no you don't have to use mysql_fetch_assoc compared to mysql_fetch_array... you can add a parameter to use number or association or both and by default array is both If I recall. So he doesn't need to use mysql_fetch_assoc compared to mysql_fetch_array.

    http://www.php.net/mysql_fetch_array - Should check it out oesxyl.

    Edit: I might add that it is faster but by no means MUST he use it.
    Last edited by Iszak; 07-23-2008 at 10:32 PM.


  •  

    Posting Permissions

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