PDA

View Full Version : Break after X amount of images


Kirk Y
06-24-2006, 11:36 PM
I've got a page displaying the members' profile pictures. The problem is that all the pictures are being placed on one line, so I'm trying to figure out how to add a <tr> after 5 images have been displayed. I've seen a few gallery hacks do this, but I only partially understand the process. This is what I've got so far:

$users = $db->query_read("
SELECT *
FROM user
");
$userstotal = $db->query_read("
SELECT username
FROM user
");
$user_count = mysql_num_rows($userstotal);
while ($userinfo = $db->fetch_array($users))
{
if ($vbulletin->options['usefileavatar'])
{
$userinfo['profilepicurl'] = $vbulletin->options['profilepicurl'] . '/profilepic' . $userinfo['userid'] . '_' . $userinfo['profilepicrevision'] . '.gif';
}
else
{
$userinfo['profilepicurl'] = 'image.php?' . $vbulletin->session->vars['sessionurl'] . 'u=' . $userinfo['userid'] . "&amp;dateline=$userinfo[profilepicdateline]&amp;type=profile";
}
$username = $userinfo['username'];
$userid = $userinfo['userid'];
$userinfopic .= "<td class=\"alt2\" height=\"100\" width=\"120\" align=\"center\"><b>$username</b><br /><img src=\"" . $userinfo['profilepicurl'] . "\" alt=\"\" title=\"$userinfo[username]'s picture\" border=\"0\"";
$userinfopic .= ($userinfo['ppwidth'] AND $userinfo['ppheight']) ? " width=\"$userinfo[ppwidth]\" height=\"$userinfo[ppheight]\" " : '';
$userinfopic .= "/></td>";
}

So I use $userinfopic to display the images and I added a query to determine the number of rows. I tried using this in my template:

<if condition="$user_count > '5'">
</tr><tr>
</if>

But it's being added after userinfopic, so it's obviously not having an effect on the 6th image. If it's not obvious by now, I'm clueless as to how to proceed. If someone wouldn't mind pointing me in the right direction, it'd be much appreciated.

calorie
06-25-2006, 01:27 PM
Consider setting a counter and then use something like follows:

if ($counter % 5 == 0)
{
// do </tr><tr> thing
}

Kirk Y
06-25-2006, 05:17 PM
That's sort of what I was trying, only problem is, I'm not sure where to stick the if condition.

Paul M
06-25-2006, 06:30 PM
After the final line, before the closing bracket I would guess.

You also need the $counter - setting it to 0 before the while, and adding 1 to it inside the loop.

Kirk Y
06-25-2006, 08:38 PM
$counter would be the same as my $user_count var, would it not?

Guest190829
06-25-2006, 09:41 PM
Nope, your $user_count is the amount of rows in the database. $counter will act as a counter, and increment every time the while loop is processed.

So in your while loop you need:


$counter += 1

if ($counter % 5 == 0)
{
// do </tr><tr> thing
}


The "$counter % 5 == 0" expression means if the counter is divisible by 5, then start a new row.

Kirk Y
06-26-2006, 12:53 AM
Well it seems like I've done everything right, but I still can't get the pictures to break after the 5th. But thanks for all your collective attempts.

The code works fine, it must be something with my html, as far as I can tell. Would it be more than a </tr><tr>?