SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot
    Join Date
    May 2004
    Location
    uk
    Posts
    151
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Option Box function

    Hi,

    I've got this function to make a Option Box

    PHP Code:
    function build_options()
    {
        
    $q "select * from cats";
        
    $result mysql_query($q) or die (mysql_error());

        echo 
    "<select name =\"cat_id\">";
        while (
    $row mysql_fetch_array($result,MYSQL_ASSOC))
        {
            
    extract ($row);
            echo 
    "<option value =\"$cat_id\">$cat_id - $cat_text</option>\n";
        }
        echo 
    "</select>\n";

    I'd Like to be able to pass a Variable (cat_id) to it and for that option to be selected.

    Any ideas?

    Cheers

  2. #2
    SitePoint Addict launchcode's Avatar
    Join Date
    Dec 2004
    Location
    Bristol, UK
    Posts
    259
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    function build_options($selected)
    {
        
    $q "select * from cats";
        
    $result mysql_query($q) or die (mysql_error());

        echo 
    "<select name=\"cat_id\">";
        while (
    $row mysql_fetch_array($result,MYSQL_ASSOC))
        {
            
    extract ($row);
            if (
    $cat_id == $selected)
            {
                echo 
    "<option value=\"$cat_id\" selected>$cat_id - $cat_text</option>\n";
            }
            else
            {
                echo 
    "<option value=\"$cat_id\">$cat_id - $cat_text</option>\n";
            }
        }
        echo 
    "</select>\n";

    Should do it.
    Richard Davey

    Launchcode
    PHP Security Guide. Think your scripts are secure? Think again.

  3. #3
    SitePoint Zealot
    Join Date
    May 2004
    Location
    uk
    Posts
    151
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Richard,

    That's great thanks

    Cheers


Bookmarks

Posting Permissions

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