Jump to content

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


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.

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.