xfilterx Posted August 23, 2013 Share Posted August 23, 2013 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>"; } Link to comment https://forums.phpfreaks.com/topic/281496-setting-selected-attribute-of-a-dropdown-list-item-based-on-id-returned-from-table/ Share on other sites More sharing options...
xfilterx Posted August 23, 2013 Author Share Posted August 23, 2013 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... Link to comment https://forums.phpfreaks.com/topic/281496-setting-selected-attribute-of-a-dropdown-list-item-based-on-id-returned-from-table/#findComment-1446464 Share on other sites More sharing options...
Zane Posted August 23, 2013 Share Posted August 23, 2013 while($rowStandards = mysqli_fetch_array($standards)) { echo ""; $selected = $rowStandards['organization_id']; while ($rowOrganizations = mysqli_fetch_array($organizations)) { $selected = ($selected == $rowOrganizations['organization_id']) ? "selected='selected'" : null; echo "" . $rowOrganizations["organization"] . ""; } echo ""; } Link to comment https://forums.phpfreaks.com/topic/281496-setting-selected-attribute-of-a-dropdown-list-item-based-on-id-returned-from-table/#findComment-1446468 Share on other sites More sharing options...
xfilterx Posted August 23, 2013 Author Share Posted August 23, 2013 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(); } ?> Link to comment https://forums.phpfreaks.com/topic/281496-setting-selected-attribute-of-a-dropdown-list-item-based-on-id-returned-from-table/#findComment-1446472 Share on other sites More sharing options...
Zane Posted August 23, 2013 Share Posted August 23, 2013 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>"; } Link to comment https://forums.phpfreaks.com/topic/281496-setting-selected-attribute-of-a-dropdown-list-item-based-on-id-returned-from-table/#findComment-1446474 Share on other sites More sharing options...
xfilterx Posted August 23, 2013 Author Share Posted August 23, 2013 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. Link to comment https://forums.phpfreaks.com/topic/281496-setting-selected-attribute-of-a-dropdown-list-item-based-on-id-returned-from-table/#findComment-1446475 Share on other sites More sharing options...
Zane Posted August 23, 2013 Share Posted August 23, 2013 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. Link to comment https://forums.phpfreaks.com/topic/281496-setting-selected-attribute-of-a-dropdown-list-item-based-on-id-returned-from-table/#findComment-1446476 Share on other sites More sharing options...
xfilterx Posted August 23, 2013 Author Share Posted August 23, 2013 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! Link to comment https://forums.phpfreaks.com/topic/281496-setting-selected-attribute-of-a-dropdown-list-item-based-on-id-returned-from-table/#findComment-1446478 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.