Jump to content

[SOLVED] Loops Inside of a Loop Possible?


Zepo.

Recommended Posts

I cant figure out why the second loop doesnt work......

 

$groups=mysql_query("SELECT id,name,logo,active,priority,created FROM groups WHERE active='1' ORDER BY priority");
while(list($gid,$gname,$glogo,$gactive,$gpriority,$gcreated)=mysql_fetch_row($groups)){

if(isset($glogo)){
$headlogo="<img src='$glogo' border='0'>";
}else{
$headlogo="";
}

$out[body].="<center><br /><br />$headlogo<br /><br />
                <table width='96%' cellpadding='1' cellspacing='1' bgcolor='#000000' border='0'>
                 <tr>
                  <td width='37%' style='background: #E4E4E4 url($config[bg]) repeat-x;' colspan='6'><SPAN class='headertitle'>$gname</SPAN></td>
                 </tr>
                 <tr>
                  <td width='37%' style='background: #E4E4E4 url($config[bg2]) repeat-x;'><SPAN class='headertitle'> Title</SPAN></td>
                  <td width='37%' style='background: #E4E4E4 url($config[bg2]) repeat-x;'><SPAN class='headertitle'> Game</SPAN></td>
                  <td width='8%' style='background: #E4E4E4 url($config[bg2]) repeat-x;'><center><SPAN class='headertitle'>Join</SPAN></td>
                  <td width='8%' style='background: #E4E4E4 url($config[bg2]) repeat-x;'><center><SPAN class='headertitle'>Rules</SPAN></td>
                  <td width='8%' style='background: #E4E4E4 url($config[bg2]) repeat-x;'><center><SPAN class='headertitle'>Count</SPAN></td>
                  <td width='3%' style='background: #E4E4E4 url($config[bg2]) repeat-x;'><center><SPAN class='headertitle'></SPAN></td>
                 </tr>";

$ladders=mysql_query("SELECT id,name,game,gamelink,group,isteam,active FROM ladders WHERE group='$gid' ORDER BY id");
while(list($id,$name,$game,$gamelink,$group,$isteam,$active)=mysql_fetch_row($ladders)){

$totalteams=mysql_query("SELECT COUNT(*) FROM ladder_$id ");
$totalteams=mysql_fetch_array($totalteams);
$totalteams="$totalteams[0]";

if ($active == 0) {
$light = "<img src='./images/ineligible.png' border='0' alt='Ladder is inactive!'>";
$link = "$name";
}else{
$light = "<img src='./images/eligible.png' border='0' alt'Ladder is active!'>";
$link = "<a href='$config[scripturl]/standings.php?ladder[id]=$id' class='content'>$name</a>";
}



$out[body].="
<tr>
     <td width='30%' style='background: $config[altcolora] repeat-x;'><SPAN class='content'> <a href='$config[scripturl]/standings.php?ladder[id]=$id' class='content'>$name</a></td>
     <td width='37%' style='background: $config[altcolora] repeat-x;'><SPAN class='content'> <a href='$gamelink' class='content'>$game</a></td>
     <td width='8%' style='background: $config[altcolora] repeat-x;'><SPAN class='content'><center><a href='./join.php?ladder=$id'>Join!</a></center></td>
     <td width='8%' style='background: $config[altcolora] repeat-x;'><SPAN class='content'><center><a href='./rules.php?rules[ladderid]=$id'>Rules</a></center></td>
     <td width='8%' style='background: $config[altcolora] repeat-x;'><SPAN class='content'><center>$totalteams</center></td>
     <td width='3%' style='background: $config[altcolora] repeat-x;'><SPAN class='content'><center>$light</center></td>
     </tr>";
}
}

Link to comment
Share on other sites

When you say it "doesn't work" can you be more specific. What exactly is happening and what exactly do you expect to happen?

 

Also, running looping queries in that manner is VERY inefficient and can bring your server to a crawl. Do one query instead grabbing all the data you need.

Link to comment
Share on other sites

I mean that the second loop doesnt pull anything. I tried removing the WHERE group='$gid' but it didnt do any justice.

And how did you determine that? Just because nothing is displayed on the page tells you nothing about what was or was not returned in the query.

 

You are not checking your queries to check for errors. Try this:

 

$query = "SELECT id,name,game,gamelink,group,isteam,active FROM ladders WHERE group='$gid' ORDER BY id";
$ladders=mysql_query($query) or die (mysql_error());

 

 

Well, I still think you shoud pull only 1 query instead of doing looping queries. This one should do for the first two queries:

SELECT g.id, g.name, g.logo, g.active, g.priority, g.created,
       l.game, l.gamelink, l.isteam, l.active
FROM groups g
LEFT JOIN ladders l ON g.id = l.group
WHERE g.active='1'
ORDER BY g.priority, g.id

 

Although I would still incorporate the total teams query as well. ANd with a little rewriting you will make the script run exponentially faster.

 

 

Link to comment
Share on other sites

Ok, then have you cheked to see how many records, if any, are returned with that query? This is simple debugging techniques. Just take one step at a time and verify what should or should not be happening at every step.

 

Make this modification.

<?php

$query = "SELECT id,name,game,gamelink,group,isteam,active FROM ladders WHERE group='$gid' ORDER BY id";
$ladders=mysql_query($query);

echo "The query: <br />$query<br />returned " . mysql_num_rows($ladders) . " results<br />";

while(list($id,$name,$game,$gamelink,$group,$isteam,$active)=mysql_fetch_row($ladders)){

?>

 

Now check the queries and their results to see if they make sense.

Link to comment
Share on other sites

Instead of echo'ing the query and how many results it returned, why not test the value of mysql_errno() and display mysql_error() if an error occurred?  That would be much more descriptive of your problem.

 

Anyways, I'm willing to bet it's because you have a column named group, which is also a MySQL reserved word.  Enclose your column and table names in back ticks.

 

P.S.  Try pasting your query directly into phpMyAdmin and see what you get.  As mjdamato hinted at, this is just simple debugging.  You can not debug your application if you can't get it to display meaningful information to you.

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.