Skylight_lady Posted June 16, 2011 Share Posted June 16, 2011 Hi Guys, I have the following code below. It get all the results in the database: <tr> <td width="100" valign="top">Address:</td> <td> <?php $qry = "SELECT user_id, address FROM shop_address"; $res = mysql_query($qry); ?> <select name="shopaddress" id="shopaddress"> <option value="">Select one...</option> <?php while($row = mysql_fetch_array($res)) { ?> <option value="<?php echo $row['user_id']; ?>" <?php if ($row['user_id']==$shopaddress) { ?> selected="selected"<?php } ?>><?php echo $row['address']; ?></option><?php } ?> </select> </td> </tr> However, how do i add something like the below code that will only show if the address is selected: <tr><td><?php echo $row['name']; ?></td></tr> It should be a simple solution but i have never done it before so i'm unsure how. Link to comment https://forums.phpfreaks.com/topic/239493-add-hidden-if-sqlphp-row-is-selected-similar-to-us-states/ Share on other sites More sharing options...
cyberRobot Posted June 16, 2011 Share Posted June 16, 2011 I'm not sure I understand the question. Are you looking to add the extra table row whenever the following select attribute is displayed: <?php if ($row['user_id']==$shopaddress) { ?> selected="selected"<?php } ?> If so, you could just do something like: <?php $shopAddressSelected = false; ?> <tr> <td width="100" valign="top">Address:</td> <td> <?php $qry = "SELECT user_id, address FROM shop_address"; $res = mysql_query($qry); ?> <select name="shopaddress" id="shopaddress"> <option value="">Select one...</option> <?php while($row = mysql_fetch_array($res)) { ?> <option value="<?php echo $row['user_id']; ?>" <?php if ($row['user_id']==$shopaddress) { echo ' selected="selected"'; $shopAddressSelected = true; } ?>><?php echo $row['address']; ?></option><?php } ?> </select> </td> </tr> <?php if($shopAddressSelected) { echo "<tr><td>{$row['name']}</td></tr>"; } ?> Link to comment https://forums.phpfreaks.com/topic/239493-add-hidden-if-sqlphp-row-is-selected-similar-to-us-states/#findComment-1230297 Share on other sites More sharing options...
Skylight_lady Posted June 16, 2011 Author Share Posted June 16, 2011 Hi, thanks for your reply. I tried your solution but it did not work. However, I did notice that one the address is selected the is no: selected="selected" added to the field once selected as checked in the source code This happen's with or without your solution. I'm sure i got this working before. Link to comment https://forums.phpfreaks.com/topic/239493-add-hidden-if-sqlphp-row-is-selected-similar-to-us-states/#findComment-1230489 Share on other sites More sharing options...
cyberRobot Posted June 16, 2011 Share Posted June 16, 2011 Where does $shopaddress come from? If it comes from a form being submitted, are you using $_GET['shopaddress'] or $_POST['shopaddress'] first? Link to comment https://forums.phpfreaks.com/topic/239493-add-hidden-if-sqlphp-row-is-selected-similar-to-us-states/#findComment-1230492 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.