PDA

View Full Version : Returning the current position of an array.


-saturn-
Thu 10th May '01, 4:04pm
How can I get the current index of an array when going through it with a while loop like this?


$birth=$DB_site->query("SELECT username,userid FROM user WHERE birthday='$d' ORDER by username");
while ($birthday = mysql_fetch_array($birth)) {
$membername = $birthday[username];
$memberid = $birthday[userid];
$birthdays .= $membername." ".$last;
}


What I want to do is the $last variable to get the "," value for every element of the array except for the last one which should be "".

tubedogg
Thu 10th May '01, 10:14pm
$birth=$DB_site->query("SELECT username,userid FROM user WHERE birthday='$d' ORDER BY username");
$i = 1;
while ($birthday = $DB_site->fetch_array($birth)) {
$i++;
$membername = $birthday[username];
$memberid = $birthday[userid];
if ($i < $DB_site->num_rows($birth)) {
$last = ",";
} else {
$last = "";
}
$birthdays .= $membername."&nbsp;".$last;
}

-saturn-
Thu 10th May '01, 11:58pm
Thank you tubedogg. :)

Only one correction. $i should start with a zero value. ;)