SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Thread: Select Box's

  1. #1
    SitePoint Addict manipura's Avatar
    Join Date
    Apr 2001
    Location
    Calgary,AB
    Posts
    345
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Select Box's

    Ok... If you enter data into a database via select box's and then try to edit that row in the database, how would you go about re-creating those dropdown box's with the user's selections showing first on the dropdown box? I have been thinking for awhile and havne't come up with a solution yet. hope one of you can help me

  2. #2
    Grumpy Mole Man Skunk's Avatar
    Join Date
    Jan 2001
    Location
    Lawrence, Kansas
    Posts
    2,067
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's tricky. Here's some example code from ssNews.

    In ssNews each news story belongs to a category - the categories are stored in a category table with the following structure:

    ssnewscats table
    =============
    ncat_id <-- ID of the category
    ncat_name <-- Name of the category

    Then each news article saves the ID of the category it belongs to in the news_cat column of the database.

    When you go to edit a news story, a select box is displayed with all of the news categories listed and the news category the story is currently assigned to selected. Here's the code:
    PHP Code:
    $result ss_query("SELECT * FROM ssnewscats");  // My custom function - returns
                             // SQL result variable
    while ($row mysql_fetch_array($result))
    {
        
    $cats[$row['ncat_id']] = $row['ncat_name'];
    }

    // $cats is now an array of news categories

    // $news_cat is the SELECTED category

    $cats_html "<select name=cat_id size=1>\n";
    while(list(
    $id$name) = each ($cats))
    {
        if (
    $id == $news_cat)
        {
            
    // Mark this select option as selected
            
    $selected ' selected';
        } else {
            
    $selected '';
        }
        
    $cats_html .= "<option value=\"$id\"$selected>$name\n";
    }
    $cats_html .= "</select>";

    // $cats_html now contains the HTML for the select box 
    Hope that helps,

    Skunk

  3. #3
    SitePoint Addict manipura's Avatar
    Join Date
    Apr 2001
    Location
    Calgary,AB
    Posts
    345
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you very much, this one had me stumped...

  4. #4
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ya know, you could save yourself a few lines of code with my favorite operator in the whole world. Change this...

    PHP Code:
        if ($id == $news_cat)
        {
            
    // Mark this select option as selected
            
    $selected ' selected';
        } else {
            
    $selected '';
        } 
    ...to this...

    PHP Code:
    $selected = ($id == $news_cat) ? " selected" ""


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
  •