Jump to content

Add hidden <tr></tr> if sql/php row is selected? Similar to US states.


Skylight_lady

Recommended Posts

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.

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>";
}
?>

 

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  :wtf: This happen's with or without your solution.

 

I'm sure i got this working before.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.