PDA

View Full Version : Is there a way to tell in a while loop if you're on the last iteration?


JJR512
08-09-2002, 04:12 AM
I have some code that generates a "|"-separated list of forums that users are able to see. If a user has access to the forum, it appears in the list; if he/she doesn't, it doesn't. But the way I have it now, the | character appears after every forum name. I'd like it to not appear after the last forum name. I think I need to have some way of telling if a while loop is on the last go-round, if you know what I mean...if it's the last time the loop is going to execute.

Here's what I have:
// Begin Forumlinks
$forumlinksperms=$DB_site->query("SELECT forumid,title,parentid FROM forum WHERE active='1' ORDER BY displayorder");
$forumlink = "";
while ($forumlinksperm=$DB_site->fetch_array($forumlinksperms)) {
$getperms=getpermissions($forumlinksperm[forumid]);
if ($getperms[canview]) {
if ($forumlinksperm[parentid]=='-1') {
$forumlink .= "<b>$forumlinksperm[title]</b> | ";
} else {
$forumlink .= "$forumlinksperm[title] | ";
}
}
}
// End Forumlinks

So, if I had, say, five forums, named Forum1, Forum2,...Forum5, and assuming all were accessible, you would see this:
Forum1 | Forum2 | Forum3 | Forum4 | Forum5 |
I'd like to make it so you see this:
Forum1 | Forum2 | Forum3 | Forum4 | Forum5
How can I do this?

Neo
08-09-2002, 04:41 AM
Well you could always count how many forums there are using COUNT() and then add something like this $i++

Example:

// Begin Forumlinks

$countforum=$DB_site->query("SELECT COUNT(*) AS total FROM forum WHERE active='1'");
$forumcount = $countforum['total'];

$forumlinksperms=$DB_site->query("SELECT forumid,title,parentid FROM forum WHERE active='1' ORDER BY displayorder");
$forumlink = "";
$i=0;
while ($forumlinksperm=$DB_site->fetch_array($forumlinksperms)) {
$i++;
$getperms=getpermissions($forumlinksperm[forumid]);
if ($getperms[canview]) {
if ($forumlinksperm[parentid]=='-1') {
$forumlink .= "<b>$forumlinksperm[title]</b> | ";
} elseif ($forumcount == $i) {
$forumlink .= "$forumlinksperm[title]";
} else {
$forumlink .= "$forumlinksperm[title] | ";
}
}
}
// End Forumlinks


It adds a extra query.. but this might work.

Sparkz
08-09-2002, 07:10 AM
Or you can simply remove the last three chars of the string since they will always be " | "...

Neo
08-09-2002, 07:26 AM
whatever works best I really have no clue since me != sleep ><

Admin
08-09-2002, 07:28 AM
Err, very bad way Neo. If anything you should just use $DB_site->num_rows() to get the number of records.

But like sparkz said, use substr() once you're done:
// Begin Forumlinks
$forumlinksperms=$DB_site->query("SELECT forumid,title,parentid FROM forum WHERE active='1' ORDER BY displayorder");
$forumlink = "";
while ($forumlinksperm=$DB_site->fetch_array($forumlinksperms)) {
$getperms=getpermissions($forumlinksperm[forumid]);
if ($getperms[canview]) {
if ($forumlinksperm[parentid]=='-1') {
$forumlink .= "<b>$forumlinksperm[title]</b> | ";
} else {
$forumlink .= "$forumlinksperm[title] | ";
}
}
}
$forumlink = substr($forumlink, 0, -3);
// End Forumlinks

JJR512
08-09-2002, 09:15 PM
FireFly, thank you so very much! I've used the substr method. Just chopping off the end was by far the simplest solution. :)

I was pretty sure ahead of time no method of counting the number of results in a query (like with $DB_site->num_rows()) woud work, because the number of results in the last query was further reduced by the if ($getperms[canview]) part. In other words, not all of the results from the query would end up being used, so in all likelihood, the highest number would never be reached.