views:

120

answers:

2

Here's what I've got so far-

$awards_sql_1 = mysql_query('SELECT * FROM categories WHERE section_id = 1') or die(mysql_error());
$awards_sql_2 = mysql_query('SELECT * FROM categories WHERE section_id = 2') or die(mysql_error());
$awards_sql_3 = mysql_query('SELECT * FROM categories WHERE section_id = 3') or die(mysql_error());
$awards_sql_4 = mysql_query('SELECT * FROM categories WHERE section_id = 4') or die(mysql_error());

$loop = 1;

while($row_sections = mysql_fetch_array($sections_query)) {
    $category = 1;
    echo "<h3>" . $row_sections['section_name'] . " (Loop# $loop)</h3>";

    while($categories = mysql_fetch_array(${"awards_sql_{$loop}"})) {
        ${"winners_sql_{$loop}"} = mysql_query("SELECT * FROM 2009_RKR_bestof WHERE section = $loop && category = $category ORDER BY result_level ASC") or die(mysql_error());
        echo "<h4><strong>{$categories['category_name']}</strong></h4>";
        echo "<ul class=\"winners\">";
>>          while($winners = mysql_fetch_array(${"winners_sql_{$loop}"})) {
                switch ($winners['result_level']) {
                    case 1: $result_level = "Platinum"; break;
                    case 2: $result_level = "Gold"; break;
                    case 3: $result_level = "Silver"; break;
                }
                if (isset($winners['url'])) { $anchor = "<a href=\"http://{$winners['url']}\" target=\"_blank\">"; $close = "</a>"; }
                echo "<li>$anchor{$winners['winner']}$close ($result_level)</li>";

                unset($anchor);
                unset($close);
            }
        echo "</ul>";
        $category++;
    }
    $loop++;
}

The result of this can be scene here- http://www.theroanoker.com/bestof/index1.php

Where I'm getting stumped, is I'm getting this thing to loop through correctly, my loop counter ($loop) is working, but when it gets time to spit out the actual reward recipients after the first loop through winners, it's only producing the category titles, the list-items are not getting looped out.

I added a little pointer to where I think the problem begins or centers around (>>).

My guess is I need to maybe unset a var somewhere, but I don't know, I can't see it.

Can someone help please? Thanks!

A: 

My guess would be that it is a data problem. It isn't having trouble reading the titles, only the winners. If it iterated once, I would check the data, and ensure that winners_sql_2 - winnders_sql_4 are getting actual data. Perhaps add an echo winners_sql_2 line, to output the contents of the query, and ensure the query is framed correctly.

Daryl
+3  A: 

I'm with KM - you're displaying a single page and with your loops, you've got a LOT of queries happening at once - what if 1,000 people hit that page at the same time? ouch...

Maybe consider a larger query (with some repeated data) and loop through it once?

For example:

SELECT 
  section_name, 
  category_name,
  result_level,
  url,
  winner
FROM 2009_RKR_bestof
INNER JOIN categories ON 2009_RKR_bestof.category = categories.id
INNER JOIN sections ON 2009_RKR_bestof.section = sections.id
ORDER BY section_name,category_name ASC

In your loop, you can do checks to determine if you're in a new section (category/whatever):

//pseudo-code
$current_section = "";
while($stuff = mysql_fetch_array($sql))
{
  if ($current_section == "")
  {
    $current_section = $stuff["section_name"];
  }
  if ($current_section == $stuff["section_name"])
  {
    //keep going in your loop
  }
  else
  {
    //we've gotten to a new section - so close your html and start a new section
  }
}

You get the idea..

JayTee
Kind of I guess. I'm not that experienced, but I just ran that query of yours in SQLyog so I see what it's doing. I'll just have to rework my loops I guess...I think I understand.
Marty
Definitely the best way to do this. @Marty - if you have any other questions, you know where to ask them!
John Rasch
I'm pretty much lost now when it comes to the comment "keep going in your loop"
Marty
would that be a place to do something like:while (in_array()) {?
Marty
"keep going in your loop" was a way to indicate that you'd do your display logic, echo statements, etc - probably a bad choice of words.My overall intent was to show that you can get everything done in a single query rather than a whole bunch of queries to get the same effect.
JayTee