1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

populating a combo box from a Mysql DB using php

Discussion in 'PHP' started by e_k, Feb 10, 2006.

  1. #1
    Hello guys!

    I need some help with populating the combo box from a database.

    I have managed to get the property numbers list printed on a web page using the 'for' loop (see the code below)

    But when i try and get it to work inside the combo box, it doesn't work...

    It only lists the last property number from the database (only one)

    How can i get it to display the entire property number list in the options of the combo box??

    here is the code:

    <html>
    <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>

    <body>
    <?php
    // connect to mySql
    $session = mysql_connect("localhost", "", "");
    // select the database
    mysql_select_db("dreamh");

    $query = "select * from property_for_rent";

    $result = mysql_query($query);
    if ( !$result )
    {
    echo ( "<P>Error doing a select: " . mysql_error() . "</P>" );
    }
    else
    {
    // succesful read - how many records ?
    print (" Number of rows read " . mysql_num_rows($result)."</br>");

    $rr=mysql_num_rows($result); //this is calculating the number of rows read from database and puts it into variable rr

    // loop over rows, counts the total number and prints them
    for ($i=0; $i<$rr; $i++) {
    $nextresult = mysql_fetch_array($result);
    $property_no = $nextresult['property_no'];
    print ($property_no."<br>");

    }

    }

    // close the connection
    mysql_close($session);

    ?>
    //this is form which contains the combo box, combo box will list all the property numbers

    <form name="form1" method="post" action="">
    <select name="getPropNo" id="getPropNo">
    <option><?php print($property_no); ?></option>
    <option selected "$user_input" </option>

    </select>
    </form>
    </body>
    </html>



    i sort of think that the 'for' loops needs to be inside the code for combo box options, but really can't figure out how?? any help will be appreciated. please help me....

    thanks
     
    e_k, Feb 10, 2006 IP
  2. Big 'G'

    Big 'G' Member

    Messages:
    89
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    48
    #2
    u could try using the while loop

    //call db and query
    $query=
    $result=
    <form .....>

    //populate form using db fields
    while($property =mysql_fetch_assoc($result)){
    echo "<option>{$property['property_no']}</option>";
    }
     
    Big 'G', Feb 10, 2006 IP