webguync Posted October 24, 2012 Share Posted October 24, 2012 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 } ?> Quote Link to comment Share on other sites More sharing options...
tehjagjr Posted October 24, 2012 Share Posted October 24, 2012 If i read this right.. you want to add a unique ID for a html div? or you want to add a unique ID for a mysql row? Quote Link to comment Share on other sites More sharing options...
webguync Posted October 24, 2012 Author Share Posted October 24, 2012 (edited) 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 October 24, 2012 by webguync Quote Link to comment Share on other sites More sharing options...
akphidelt2007 Posted October 24, 2012 Share Posted October 24, 2012 Add this to this line //this will create a unique id for the row. <tr class="parent" id="Org-<?php echo $id; ?>" title="click for more organizational information"> Quote Link to comment Share on other sites More sharing options...
Jessica Posted October 24, 2012 Share Posted October 24, 2012 (edited) 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 October 24, 2012 by Jessica Quote Link to comment Share on other sites More sharing options...
tehjagjr Posted October 24, 2012 Share Posted October 24, 2012 (edited) 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 October 24, 2012 by tehjagjr Quote Link to comment Share on other sites More sharing options...
webguync Posted October 24, 2012 Author Share Posted October 24, 2012 ok, thanks I got the the unique ID's to be added to the rows now. Need to tweak the JQuery some, but I will move any questions to that forum. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.