SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Nov 2007
    Location
    Fort Benning, GA
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Select list not populating

    Hello all,
    I am relatively new to PHP so please bear with me. I have a site that I am rewriting from Coldfusion to PHP because I like PHP better and I am tired of using ColdFusion. Anyway, everything is going well except for this stupid select list that is not populating. Here is my code below:
    First, I query a table that joins two other tables together. If there is a result (there will be ONLY one result if there is), then I do this:

    PHP Code:
    $q2 "SELECT resort_id, gift_id FROM resort_gift WHERE gift_id = $id";

        
    $r2 = @mysqli_query($dbc$q2); 
    If there is a result of one record (again, there will be either one or no records as this is a one to many relationship:

    PHP Code:
    if (mysqli_num_rows($r2) == 1) {
            
    $q3 "SELECT Resort_ID, Resort_Name FROM Resorts";
            
    $r3 = @mysqli_query($dbc$q3);
            
    $row3 mysqli_fetch_array($r3MYSQLI_NUM);
            
        } 
    I then am creating an array to populate the <option> tag like so:
    PHP Code:
    $options="";
            
            while (
    $row=mysqli_fetch_array($r3)) {
            
                
    $id=$row3[0];
                
    $rn=$row3[1];
                
    $options="<OPTION VALUE=\"$id\">".$rn.'</option>';
            } 
    Then in the form I am doing this:

    PHP Code:
     <?php if (mysqli_num_rows($r3) > 0) {
                    
    ?> <select name="Resort_ID">
                    <?=$options?>
                    <?php ?> </select><br /><br />
    Now here is my question. One of the things that I need to do is, the resort that is tied to the gift certificate that I am editing should be automatically selected, but I still want to see all the resorts since the user could have selected the wrong resort by mistake.
    Can someone point out what I am doing wrong?

    Thanks,

    Bruce
    Last edited by cpradio; Mar 6, 2013 at 06:03. Reason: Added [php] tags

  2. #2
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,235
    Mentioned
    154 Post(s)
    Tagged
    0 Thread(s)
    First thing I see is this
    PHP Code:
    while ($row=mysqli_fetch_array($r3)) {

    $id=$row3[0];
    $rn=$row3[1];
    $options="<OPTION VALUE=\"$id\">".$rn.'</option>';

    Your while loop uses $row but you are using $row3 inside the while loop...

  3. #3
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    73 Post(s)
    Tagged
    0 Thread(s)
    Second is that you're overwriting $options every loop through. Need to .= instead of just =
    Third is that you have no logic for identifying the matching ID.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.


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
  •