SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict
    Join Date
    Sep 2000
    Location
    Ontario, Canada
    Posts
    320
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would like to make a dropdown box where that is populated with info from a certain field of all the records in the sql database.

    Here's what I have so far but its only printing the first record. How would I go about making it print all of them?

    PHP Code:
    $result mysql_query("SELECT owner,agent FROM homes;",$link);
    print 
    "<option value=\"$a_row[owner]\"SELECTED>$a_row[owner] ($a_row[agent])</option>";
    print 
    "<option value=\"\">---------";
    while(
    $a_row mysql_fetch_array($result))
    {
        print 
    "<option name=\"owner\" value=\"$a_row[owner]\">$owner ($agent)</option>";

    -- the <br/> tags aren't really there... they were put in by the forum.

    Any help is greatly appreciated!
    Jason Dulberg
    Extreme MTB
    http://extreme.nas.net

  2. #2
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well:

    Code:
    	$result = mysql_query("SELECT owner,agent FROM homes");
    	print '<select>';
    	while($a_row = mysql_fetch_array($result)) {
    		printf('<option name="owner" value="%s">%s</option>', $a_row[owner], $a_row[owner]);
    		}	
    	print '</select>';
    That code would print every row but from what yo uare after I assume you are trying to preselect one if a vr is present and equal to one of the elements of the drop down. Lets say a var called $owner

    Code:
    	$result = mysql_query("SELECT owner,agent FROM homes");
    	print '<select>';
    	while($a_row = mysql_fetch_array($result)) {
    		printf('<option name="owner" value="%s" %s>%s</option>', $a_row[owner], ($owner == $a_row[owner]) ? "selected" : "", $a_row[owner]);
    		}	
    	print '</select>';
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  3. #3
    SitePoint Addict
    Join Date
    Sep 2000
    Location
    Ontario, Canada
    Posts
    320
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Looks like Sitepoint fixed part of the code... I tried putting in your second bit of code but it only prints the number 1, not the contents of the field. And no matter how many records I have in there, it only prints "1" once.

    Also, is the %s a part of the script or something that the forum added in?

    You are correct in your second assumption. Basically, I'm using that part of the code to read the database, list what the current record contains and then list all of the other possibilities in the other records as well.

    Then for the other one, I just want to list all of what's there and leave out the current record part. This will be for adding data, the other is for modifying it.

    thanks again for your help.
    Last edited by jdulberg; Apr 8, 2001 at 20:16.
    Jason Dulberg
    Extreme MTB
    http://extreme.nas.net


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
  •