Jump to content

Dynamic Tables


Staggan

Recommended Posts

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 by PFMaBiSmAd
Link to comment
Share on other sites

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 by Staggan
Link to comment
Share on other sites

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.)

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

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.