Jump to content

Need To Give Each Row It's Own Unique Id In Foreach Loop For Jquery


webguync

Recommended Posts

I am displaying data in an html table from MySQL via PHP. I am trying to implement JQuery show/hide functionality, but in order for it to work, I need to make some adjustments in the PHP. Currently since my code is within a loop each row will have the same ID as I have it now, and I need each row to have a unique ID, so that you can open and close reach parent row independently.

 

Currently it is.

<table>
<tr class="parent" id="row1" title="click to expand">
<td>Name to be clicked</td>
<tr>
<tr class="child-row1">
<td>More Info</td>
</tr>

<tr class="parent" id="row1" title="click to expand">
<td>Another Name to be clicked</td>
<tr>
<tr class="child-row1">
<td>More Info</td>
</tr>
</table>

 

code I am using to pull in the data

 


<?php
$host="localhost";
$username="uname";
$password="pw";
$database="DB";

mysql_connect($host,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$results = mysql_query('SELECT * FROM organization ORDER BY notes_tdate_created_d ASC');
$results_array = array();
while ($row = mysql_fetch_array($results)) {
$results_array[$row['id']] = $row;
}
?>


<?php foreach ($results_array as $id => $record) { ?>
<tr class="parent" id="Org" title="click for more organizational information">

<td colspan="8" class="OrgName"><?php echo $record['company_t'];?></td></td>
</tr>

<tr class="child-Org">
<th>Address 1</th>
<th>Address 2</th>
<th>City</th>
<th>State</th>
<th>Zip</th>
<th>Phone Number</th>
<th>Email</th>
<th>Website</th>
</tr>

<tr class="child-Org">
<td><?php echo $record['address1_t'];?></td>
<td><?php echo $record['address2_t'];?></td>
<td><?php echo $record['city_t'];?></td>
<td><?php echo $record['state_t'];?></td>
<td><?php echo $record['zip_t'];?></td>
<td><?php echo $record['phone_t'];?></td>
<td><a href="mailto:<?php echo $record['email_t'];?>"><?php echo $record['email_t'];?></a></td>
<td><a href="http://<?php echo $record['url_t'];?>"><?php echo $record['url_t'];?></a></td>
</tr>
<tr class="child-Org">
<td colspan="8">
<?php echo $record['profile_t'];?></td>

</tr>
<?php } ?>

Link to comment
Share on other sites

sorry, I wasn't clear with that. I want to add a unique ID to the row in the HTML so that my JQuery will work, so it would need to be



<table>
<tr class="parent" id="row1" title="click to expand">
<td>Name to be clicked</td>
<tr>
<tr class="child-row1">
<td>More Info</td>
</tr>

<tr class="parent" id="row2" title="click to expand">
<td>Another Name to be clicked</td>
<tr>
<tr class="child-row2">
<td>More Info</td>
</tr>
</table>

Edited by webguync
Link to comment
Share on other sites

1. That parent and click to expand stuff doesn't even exist in your code yet. nvrnd. 

2. You are writing the header row within your foreach, so it will appear with every row.

3. Rather than building an array in the while loop then looping through that using foreach, you can do that all at one time.

4. You already have $id... Add it to "org". What is the problem?

Edited by Jessica
Link to comment
Share on other sites

id go with

<?php
$count = 0;
?>

<table>
<tr class="parent" id="row<?++count;?>" title="click to expand">
<td>Name to be clicked</td>
<tr>
<tr class="child-row<?++count;?>">
<td>More Info</td>
</tr>

 

or if you have a unique id in the DB use

echo $id;

Edited by tehjagjr
Link to comment
Share on other sites

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.