SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Wizard lukeurtnowski's Avatar
    Join Date
    Mar 2003
    Location
    Coronado
    Posts
    1,666
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    dynamic combo box?

    I have a dynamically created combo box
    HTML Code:
    <select name="email" size=1 id="email">
    <?php
        while($result = mysql_fetch_assoc($query)) { 
        printf('<option value="'.$result['Email'].'">'.$result['Email'] . '</option>');
        print "\n";
        } 
    ?>
    </select>
    Trouble is when no email address exists in the record, a blank option exists which maked the copmbo box look wierd, how can I make it so that if no email address is in the email row of my database no option would be made instead of a blank 1?
    Thanks
    "Oh, and Jenkins--apparently your mother died this morning."

  2. #2
    SitePoint Zealot
    Join Date
    Nov 2005
    Posts
    113
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you can check (mysql_num_rows($result) < 1) first, if zero no records, print no record message etc, if more than 1, print the combo box!

  3. #3
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Please see this function which populates the combo:
    PHP Code:
        function PopulateList($table,$valField,$selField,$selId,$param){
            
    $opt "";
            
    $rs mysql_query("SELECT $valField,$selField FROM $table WHERE 1 $param ORDER BY $selField ASC") or die(mysql_error());
            while(
    $rows mysql_fetch_array($rs)){
                if(
    $rows[$valField] == $selId)
                    
    $opt .= "<option value=$rows[$valField] selected>$rows[$selField]</option>";
                else
                    
    $opt .= "<option value=$rows[$valField]>$rows[$selField]</option>";
            }
            return 
    $opt;
        } 
    And you can use the function like this:
    PHP Code:
    <select name="countries" id="countries">
    <option selected="selected" value="000">Select Country</option>
    <?php echo PopulateList("tablename","id field of the table","field to display","the selected value","pass any parameter");?>
    </select>
    This is my own function. You can ask more if you don't understand.

  4. #4
    Quake 1 Addict CreedFeed's Avatar
    Join Date
    Feb 2002
    Location
    Milwaukee, WI
    Posts
    296
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can just use an if statement to check if it exists, and if it does, print it out:

    PHP Code:
    <select name="email" size=1 id="email">
    <?php
        
    while($result mysql_fetch_assoc($query)) {
            if (
    $result['Email'] != '') { 
                
    printf('<option value="'.$result['Email'].'">'.$result['Email'] . '</option>');
                print 
    "\n";
            }
        } 
    ?>
    </select>
    -- Steve Caponetto
    Quake 1 Resurrection :: CreedFeed


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
  •