How to reset counter if id number changes

i’m trying to create an thumbnail gallery and am having trouble with the links

say i have 20 records in database, 10 with id #1 and 10 with id #2

I need to loop through each row with id#1 and increment the counter. When the id number changes, the counter is reset back to 1 and starts incrementing again

At the moment it creates the thumbnails but the counter goes from 1 - 20 and i need it to go from 1-10 and then 1-10 again

while (list($eventname,$festivalid,$image,$thumb,$description,$username) = $result->fetchrow()) {
	$eventname = strtolower(str_replace(' ','-',$eventname));
	if ($count % NUMCOLS == 0) echo "<tr>\
";  # new row
	echo "<td><div class='img-shadow'><a href='image.php?id=$festivalid&amp;festival=$eventname&amp;pagenum=$counter'><img src='users/$username/albums/$festivalid/thumbs/$thumb' alt='$description' title='$description' style='border:none' /></a></div></td>\
";
	$count++;
	$counter++;

	if ($count % NUMCOLS == 0) echo "</tr>\
";  # end row
}

Why do you have 10 records with the same id#?

Anyway…
Using your code…

    if ($count % NUMCOLS == 0) echo "<tr>\
";  # new row

transforms into:

if ($count % NUMCOLS == 0) { 
  echo "<tr>\
";  # new row
  $count = 0;
}

a forum can have 10 threads, each thread would have the same forum id

a thread can have 10 posts, each post would have the same thread id

a customer can have 10 orders, each order would have the same customer id

et cetera, eh

:slight_smile:

Though, looking at it further… what you described to want isn’t what you have - your code is defined by a constant (NUMCOLS). If you want to guarantee NUMCOLS items per row, and group all elements together, then your code works. If you want to group by FestivalID…

Taking your code and once again transforming it:


$count = 0;
$festid = 1;
while (list($eventname,$festivalid,$image,$thumb,$description,$username) = $result->fetchrow()) {
    $eventname = strtolower(str_replace(' ','-',$eventname));
    if ($festivalid != $festid) {
       for(;$count < NUMCOLS; $count++) {
          echo "<td>&nbsp;</td>";
       }
       echo "</tr>";
       $festid = $festivalid;
    }
    if ($count % NUMCOLS == 0)  {
       echo "<tr>\
";  # new row
       $count = 0;
    }
    echo "<td><div class='img-shadow'><a href='image.php?id=$festivalid&amp;festival=$eventname&amp;pagenum=$counter'><img src='users/$username/albums/$festivalid/thumbs/$thumb' alt='$description' title='$description' style='border:none' /></a></div></td>\
";
    $count++;
    $counter++;
    if ($count % NUMCOLS == 0) echo "</tr>\
";  # end row
} 

I dont understand the purpose of $counter, so i’m assuming your original use of it was as intended.

A forum can have 10 threats, each thread would have the same forum id - but a different threadid… etc.

I’m making a thumbnail image gallery and the images can be for a category which has it’s own id so i’d had 10 images for one category, 10 for another category etc.

But to display each image i wanto to have each category numbered from 1 to 10

What i want to reset is the $counter variable so if the $festivalid changes (after the 11th loop) the counter is reset back to 1

The NUMCOLS is the number of columns the table for the gallery has

define ("NUMCOLS",5);

This will probably help you see what’s going on

$count = 0;

if($page == 1) {
	$counter= 1;
}
else {
	$counter= ((($entries_per_page) * ($page-1)) + 1);
}

echo "<table border='0' id='gallery'>";
while (list($eventname,$festivalid,$image,$thumb,$description,$username) = $result->fetchrow()) {
	$eventname = strtolower(str_replace(' ','-',$eventname));
	if ($count % NUMCOLS == 0) echo "<tr>\
";  # new row
	echo "<td><div class='img-shadow'><a href='image.php?id=$festivalid&amp;festival=$eventname&amp;pagenum=$counter'><img src='users/$username/albums/$festivalid/thumbs/$thumb' alt='$description' title='$description' style='border:none' /></a></div></td>\
";
	$s = $festivalid;
	$count++;
	if($festivalid = $s {
		$counter++;
	}
	else {
		$counter = 1;
	}
	if ($count % NUMCOLS == 0) echo "</tr>\
";  # end row
}

# end row if not already ended

if ($count % NUMCOLS != 0) {
   while ($count++ % NUMCOLS) echo "<td>&nbsp;</td>";
   echo "</tr>\
";
}
echo "</table>";

So you’re predefining that every festivalID will have exactly 10 (no more, no less) loop iterations, and your data is sorted?

if ($counter == 10) { $counter = 0; }

(And yes, I always wrap my if-then clauses, even if it’s just one command)

Red Flag:::

   if($festivalid = $s {         $counter++;     }

Not only are you missing the close-parenthesis, you’ve used a declarative =, rather than a comparitive ==.

I think the piece of information i’m missing to help you properly at this point is the contents of $result. What boundries are you putting on your database result? Are you fetching only the results from 1 festivalid, or multiple? It seems like you’re trying to use PHP to filter your SQL, when refining the query would achieve the same effect.

You will have to write like below inside the loop, before you echo the result;
if($count=10)
{
$count=1;
}

Except you would want to use a comparative rather than a declarative…