Jump to content

Setting 'selected' attribute of a dropdown list item based on id returned from table


Go to solution Solved by Zane,

Recommended Posts

So I'm trying to set the "selected" attribute of a dropdown list's option based on the stored id of a table. Here's my code so far which I can't seem to figure out what I'm doing wrong. Again, please note...I'm not a PHP guy...I'm an ASP.NET guy who has been literally thrown into the fire on this project. This is my first PHP project and it reminds me a lot of classic ASP from the 90's....I wish I could remember how I used to do all of this back then....

 

The standards result is the main table that contains the id of the organization within a field called organization_id. The organizations result is created by pulling the table organizations and binding that to a drop down list. I'm trying to compare the bound values with the value of organization_id in the standards table. 

while($rowStandards = mysqli_fetch_array($standards))
{
     echo "<select name='organization'>";
     while ($rowOrganizations = mysqli_fetch_array($organizations))
     {
          $selected=$rowStandards["organization_id"];
          echo "<option " . if($selected == $rowOrganizations["organization_id"]){ print "selected";} . " value=' . $rowOrganizations["organization_id"] . '>" . $rowOrganizations["organization"] . "</option>"; 
     }
     echo "</select>";
}
Edited by xfilterx

This list binds fine with this line here: 

 

echo "<option value='" . $rowOrganizations["organization_id"] . "' " . ' ' . ">" . $rowOrganizations["organization"] . "</option>"; 
It's when I add the if statement that it gives me an error saying unexpected token if. 
echo "<option value='" . $rowOrganizations["organization_id"] . "' " . if($selected == $rowOrganizations["organization_id"]){ print "selected";} . ">" . $rowOrganizations["organization"] . "</option>"; 

Looking through my PHP book and looks like my syntax for the if is correct. Not sure what's going on here...

Edited by xfilterx



while($rowStandards = mysqli_fetch_array($standards))
{
echo "";
}

 

Edited by Zane

Thanks for the attempt Zane. Your code makes sense to me however it's not working. The list is still bound with the values from the organizations table however the appropriate id is not being selected. It just defaults to the first item in the list. I did have to add a couple of quotes to your code...one after the value and one after the second period (a concatonation operator I assume). Here's the whole script so you can see what's going on. This organization_id is being retrieved correctly..,as it's being printed out in a text box below the while loop...

<?php   
    echo "<table>";

    while($rowStandards = mysqli_fetch_array($standards))
    {
        echo "<tr><td>Standard ID</td><td><input type='text' value='" . $rowStandards['standard_id'] . "' name='standard_id' disabled></td></tr>";
        echo "<tr><td>Organization</td><td><select name='organization'>";
        $selected = $rowStandards["organization_id"];
        while ($rowOrganizations = mysqli_fetch_array($organizations))
        {
            $selected = ($selected == $rowOrganizations['organization_id']) ? "selected='selected'" : null;
            echo "<option {$selected} value='" . $rowOrganizations["organization_id"] . "'>" . $rowOrganizations["organization"] . "</option>"; 
        }
        echo "</select></td></tr>";
        echo "<tr><td>Organization ID</td><td><input type='text' value='" . $rowStandards['organization_id'] . "' name='organization_id'></td></tr>";
        echo "<tr><td>Item No</td><td><input type='text' value='" . $rowStandards['item_no'] . "' name='item_no'></td></tr>";
        echo "<tr><td>Standard No</td><td><input type='text' value='" . $rowStandards['standard_no'] . "' name='standard_no'></td></tr>";
        echo "<tr><td>Title</td><td><input type='text' value='" . $rowStandards['title'] . "' name='title'></td></tr>";
        echo "<tr><td style='font-weight:bold;'>Keywords</td><td><input type='text' value='" . $rowStandards['keywords'] . "' name='keywords'></td></tr>";
        echo "<tr><td>Abstract</td><td><textarea name='abstract'>" . $rowStandards['abstract'] . "</textarea></td></tr>";
        echo "<tr><td>Link</td><td><input type='text' value='" . $rowStandards['link'] . "' name='link'></td></tr>";
    }
    
    echo "</table><br>";
    
    if (!$standards) {
        printf("<br><br>Error: %s\n", mysqli_error($con));
        exit();
    }
?>
Edited by xfilterx
  • Solution

Oh my mistake, it's overriding the $selected variable

 

Try this

while($rowStandards = mysqli_fetch_array($standards))
{
     echo "<select name='organization'>";
     $selected = $rowStandards['organization_id'];     

     while ($rowOrganizations = mysqli_fetch_array($organizations))
     {
          $sel = ($selected == $rowOrganizations['organization_id']) ? "selected='selected'" : null;
          echo "<option {$sel} value=" . $rowOrganizations["organization_id"] . ">" . $rowOrganizations["organization"] . "</option>"; 
     }
     echo "</select>";
}
Edited by Zane

Zing! That did it! Thanks so much bro....I'm knee deep in this project so I'm sure I'll have another question. I usually try Google searching what I need but I keep coming up with CodeProject and StackOverflow questions that are similar to mine that go completely unanswered.

 

The next critical piece of this will be to pull values from several lookup tables into multiple checkbox lists. Same thing....a standard can and will have several possibilities for check boxes so I will need to loop through all of the selected values within a table and check all of the corresponding check boxes in the list. Fun fun fun.

Yeah, most people get here from google searches. It's really worth it though to stick with a community (particularly this one) where you can ask very specific questions.

Yeah, most people get here from google searches. It's really worth it though to stick with a community (particularly this one) where you can ask very specific questions.

 

I plan too....thanks. Hope to see your response on my next question....stay tuned!  ;D

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.