Staggan Posted November 20, 2012 Share Posted November 20, 2012 Hello I am trying to display a list of groups, and for each group on that list I want to show it's members... For example: Group A Member A 1 Member A 2 Group B Member B 1 Member B 2 I would also like to be able to Click on the group heading to hide / show the members... I get the groups and members from our database, no problem, but I am unsure how to display the results... Anyone make any suggestions or point me in a direction? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/270946-dynamic-tables/ Share on other sites More sharing options...
Muddy_Funster Posted November 20, 2012 Share Posted November 20, 2012 the click to hide/show is best done with javascript - I have found the JQueryUI library to be good for that sort of thing. as for the displaying content, you can just echo it out as a table or into divs (better option for the show/hide idea). Let's see what you have just now and well point you in the right direction. Quote Link to comment https://forums.phpfreaks.com/topic/270946-dynamic-tables/#findComment-1393829 Share on other sites More sharing options...
Staggan Posted November 20, 2012 Author Share Posted November 20, 2012 So, this is what I have... $res=mysql_query("select * from tournaments")or die(mysql_error()); ?> <table cellpadding="2" cellspacing="2"> <tr><td>Tournament Id</td> <td> Tournament Name</td> </tr> <?php while($row=mysql_fetch_assoc($res)) { ?> <tr><td><?php print $row['tournament_id']; ?></td><td><a href='createtournament.php?con=<?php print $row['tournament_id']; ?>'><?php print $row['tournament_name']; ?></a></td></tr> <?php } ?> </table> </div> </form> </div> </div> This gives me my tournaments with a link on them to create that tournament (the link for the moment can be removed) So now I want to add this table below each of the tournament names... $participants = mysql_query("select * from tournament_participants")or die(mysql_query()); echo "<table><tr><th>Tournament Id</th><th>Team id</th><th>Participant id</th></tr>"; while($row=mysql_fetch_assoc($participants)) { echo "<tr><td align='right'>".$row['tournament_id']."</td><td align='right'>".$row['team_id']."</td><td align='right'>".$row['participant_id']."</td></tr>"; } echo "</table>"; So basically this gives me... Tournament ID Tournament Name 1 Abc 2 EFG And in the next table Tournament ID Team ID Participant ID 1 157 233 1 201 444 2 89 111 2 225 232 And so on.... I'd like to print this differently so I see... Tournament ID 1 Participants ID 233 444 2 111 232 And if I click on the tournament ID I want it to show or hide the participants.... Make sense? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/270946-dynamic-tables/#findComment-1393839 Share on other sites More sharing options...
Muddy_Funster Posted November 20, 2012 Share Posted November 20, 2012 How can you be sure, using the current queries, that your participants are actualy and always accurately attached to the correct tournament? (that's something of a toung twister) Have you any experiance with javascript or JQueryUI? Quote Link to comment https://forums.phpfreaks.com/topic/270946-dynamic-tables/#findComment-1393840 Share on other sites More sharing options...
Staggan Posted November 20, 2012 Author Share Posted November 20, 2012 A little experience with javascript and very little with JQueryUI... The current query would change to the following: $participants = mysql_query("select * from tournament_participants where tournament_id = $row['tournament_id'] ")or die(mysql_query()); Although that itself did not work I had to write the tournament_id to a var first and include the var in the query Quote Link to comment https://forums.phpfreaks.com/topic/270946-dynamic-tables/#findComment-1393841 Share on other sites More sharing options...
PFMaBiSmAd Posted November 20, 2012 Share Posted November 20, 2012 You would execute ONE JOIN'ed query that gets the rows you want in the order that you want them. You would JOIN the two tables using the tournament_id. Quote Link to comment https://forums.phpfreaks.com/topic/270946-dynamic-tables/#findComment-1393843 Share on other sites More sharing options...
Staggan Posted November 20, 2012 Author Share Posted November 20, 2012 ? For each tournament_id I need a list of participants, I can do this, but what I cannot do is figure out how to display it and how to add a click to hide / show the participants in a tournament Quote Link to comment https://forums.phpfreaks.com/topic/270946-dynamic-tables/#findComment-1393848 Share on other sites More sharing options...
PFMaBiSmAd Posted November 20, 2012 Share Posted November 20, 2012 (edited) To output a new heading every time it changes, you would remember the previous heading and detect when the value changes - $query = " ... your query statement that gets the rows you want in the order that you want them"; $result = mysql_query($query); $last_heading = null; // 'remember' the last heading value. initialize to a value that will never appear in the data while($row = mysql_fetch_assoc($result)){ // detect if the heading changed if($last_heading != $row['group']){ // heading changed or is the first one if($last_heading != null){ // not the first one, (optionally) close out the previous section here... echo "</dl>\n"; } // start a new section here... echo "<dl>\n<dt>Group {$row['group']}</dt>\n"; $last_heading = $row['group']; // remember the new heading } // output the data under each heading echo "<dd>Member {$row['member']}</dd>\n"; } // (optionally) close out the last section here... if($last_heading != null){ echo "</dl>\n"; } Edited November 20, 2012 by PFMaBiSmAd Quote Link to comment https://forums.phpfreaks.com/topic/270946-dynamic-tables/#findComment-1393850 Share on other sites More sharing options...
Staggan Posted November 20, 2012 Author Share Posted November 20, 2012 (edited) I am not sure I follow this example or suggestion.... This is the resultant layout I want... Group A Member A 1 Member A 2 Group B Member B 1 Member B 2 And I want to be able to click a group and hide / show the members of that group.... I think my formatting here is not helping.. the members should be indented to the actual groups.... I don't see how your example gives me this.... maybe I am missing something... Edited November 20, 2012 by Staggan Quote Link to comment https://forums.phpfreaks.com/topic/270946-dynamic-tables/#findComment-1393852 Share on other sites More sharing options...
PFMaBiSmAd Posted November 20, 2012 Share Posted November 20, 2012 The code I posted does produce the exact layout you mentioned. The only thing that would need to be added to it is what is needed to show/hide the information (there are probably 10,000 javascript show/hide examples posted on the Internet.) You should NOT execute a query inside of a loop of another query. It is extremely inefficient (the kind of thing that gets your account suspended on a live server.) Quote Link to comment https://forums.phpfreaks.com/topic/270946-dynamic-tables/#findComment-1393861 Share on other sites More sharing options...
Staggan Posted November 20, 2012 Author Share Posted November 20, 2012 I am confused then... I will take a look through it tonight and see if I can make sense of it.... and will surely post more questions to understand it, hehe.. I agree about queries within loops, but I am very new to MySQL and still trying to figure out the language.... Thanks Quote Link to comment https://forums.phpfreaks.com/topic/270946-dynamic-tables/#findComment-1393933 Share on other sites More sharing options...
Staggan Posted November 21, 2012 Author Share Posted November 21, 2012 Hello I rewrote my code to the following: $result = mysql_query ("SELECT * FROM tournaments t, tournament_participants t1 WHERE t.tournament_id = t1.tournament_id")or die(mysql_query()); $last_heading = null; // 'remember' the last heading value. initialize to a value that will never appear in the data while ($row = mysql_fetch_assoc($result)){ // detect if the heading changed if($last_heading != $row['tournament_name']){ // heading changed or is the first one if($last_heading != null){// not the first one, (optionally) close out the previous section here... echo "</dl>\n"; } echo "<dl>\n<dt>Tournament {$row['tournament_name']}</dt>\n";$last_heading = $row['tournament_name']; } echo "<dd>Member {$row['team_id']}</dd>\n"; } if($last_heading != null){ echo "</dl>\n"; } It worked perfectly and now makes sense I had not heard of definition lists before either I will now look at hide / show options Your help was much appreciated Quote Link to comment https://forums.phpfreaks.com/topic/270946-dynamic-tables/#findComment-1394053 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.