Garling Posted October 22, 2013 Share Posted October 22, 2013 I have a function that reads from a database and list to a webpage the results. What I want is to have the selected item on a list box show when a PHP variable is found, here is my code I have so far: <?php while ($row = db2_fetch_assoc($stmt)){ $WHMARK = $row['WHMARK']; $WHHOTEL = $row['WHHOTEL']; $WHSTATUS = $row['WHSTATUS']; if ($WHSTATUS == 1) { $Status = "Active"; } else{ $Status = "In-Active"; } echo '<tr>'; echo '<td>'.$WHMARK.'</td>'; echo '<td>'.$WHHOTEL.'</td>'; echo '<td>'.$Status.'</td>'; echo '<td>'; echo '<label>'; echo ' <select name="hotelActive" id="hotelActive">'; echo ' <option value="1"' <?php if($WHSTATUS == 1) ? 'selected="selected">';?>' echo '>Active</option>'; echo ' <option value="0"' <?php if($WHSTATUS == 0) ? 'selected="selected">';?>' echo '>In-Active</option>'; echo ' </select>'; echo '</label>'; echo '</td>'; echo '</tr>'; } ?> I need option value 1 selected if the variable '$Status' is 1 and option value 2 selected if the variable '$Status' is 2. This is PHP embedded in a HTML table. Link to comment https://forums.phpfreaks.com/topic/283200-need-help-with-selected-item-in-a-select-list/ Share on other sites More sharing options...
Barand Posted October 22, 2013 Share Posted October 22, 2013 I need option value 1 selected if the variable '$Status' is 1 and option value 2 selected if the variable '$Status' is 2. This is PHP embedded in a HTML table. Then why are you checking $WHSTATUS for 0 or 1 and setting $Status to a string value?? Link to comment https://forums.phpfreaks.com/topic/283200-need-help-with-selected-item-in-a-select-list/#findComment-1455019 Share on other sites More sharing options...
Ch0cu3r Posted October 22, 2013 Share Posted October 22, 2013 Also this is incorrect syntax echo ' <option value="1"' <?php if($WHSTATUS == 1) ? 'selected="selected">';?>' echo '>Active</option>'; echo ' <option value="0"' <?php if($WHSTATUS == 0) ? 'selected="selected">';?>' echo '>In-Active</option>'; It should be echo ' <option value="1"' . (($WHSTATUS == 1) ? ' selected="selected"' ? '') . '>Active</option>'; echo ' <option value="0"' . (($WHSTATUS == 0) ? ' selected="selected"' ? '') . '>In-Active</option>'; Link to comment https://forums.phpfreaks.com/topic/283200-need-help-with-selected-item-in-a-select-list/#findComment-1455021 Share on other sites More sharing options...
Garling Posted October 22, 2013 Author Share Posted October 22, 2013 Barand, I am setting it so I can display Active and IN-Active instead of 0 and 1. Link to comment https://forums.phpfreaks.com/topic/283200-need-help-with-selected-item-in-a-select-list/#findComment-1455022 Share on other sites More sharing options...
Garling Posted October 22, 2013 Author Share Posted October 22, 2013 Ch0cu3r When I change my code to this, I am now getting an Internal Server Error unless I remove the php code. Link to comment https://forums.phpfreaks.com/topic/283200-need-help-with-selected-item-in-a-select-list/#findComment-1455023 Share on other sites More sharing options...
Ch0cu3r Posted October 22, 2013 Share Posted October 22, 2013 Ch0cu3r When I change my code to this, I am now getting an Internal Server Error unless I remove the php code. Sorry I had a typo. My code should of been echo ' <option value="1"' . (($WHSTATUS == 1) ? ' selected="selected"' : '') . '>Active</option>'; echo ' <option value="0"' . (($WHSTATUS == 0) ? ' selected="selected"' : '') . '>In-Active</option>'; Link to comment https://forums.phpfreaks.com/topic/283200-need-help-with-selected-item-in-a-select-list/#findComment-1455025 Share on other sites More sharing options...
davidannis Posted October 22, 2013 Share Posted October 22, 2013 I think you need something like this: echo ' <option value="1"' ; if ($WHSTATUS == 1) echo ' selected="selected"'; else echo '' ; echo '>Active</option>'; echo ' <option value="0"' ; if ($WHSTATUS == 0) echo ' selected="selected"' ; else echo ''; echo '>In-Active</option>'; Link to comment https://forums.phpfreaks.com/topic/283200-need-help-with-selected-item-in-a-select-list/#findComment-1455026 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.