Jump to content

using end() and current with Mysql fetch array


loicdudu

Recommended Posts

Hi,

 

I am a beginner at PHP and i'm trying to use current() and end() functions to display a slightly different layout for the last row of the array. I have been trying to find the solution but couldn't. Here is the first part of the code:

while(list($underpageid, $underpageauthor, $underpagetitle, $underpagetarget, $underpagestatus, $underpagelevel_x, $underpagelevel_xx, $underpagelevel_xxx, $underpagepath) = mysql_fetch_array($underpageresult, MYSQL_NUM))

{

 

/* Here I would like to be able to say: If it's not the last row, echo "blabla", if it's the last row, echo "BLABLA", but it doesn't work: */

if (current($underpageresult) != end($underpageresult)) {

echo "blabla";

}

 

elseif (current($underpageresult) == end($underpageresult)) {

echo "BLABLA";

}

}

 

I understand $underpageresult is not an array, it is the mysql query variable. But then, how can I define the while look with the mysql_fetch_array so that I can determine which row is the last and slightly change the html layout for this last row using an if statement ?

 

 

All I get from this is that I am not "passing an array". :-\

 

Thank you very much for your help.

current()/end() dont return the values your expecting, so try this instead:

$numberOfRows = mysql_num_rows();
$rowCounter = 0;
while(list($underpageid, $underpageauthor, $underpagetitle, $underpagetarget, $underpagestatus, $underpagelevel_x, $underpagelevel_xx, $underpagelevel_xxx, $underpagepath) = mysql_fetch_array($underpageresult, MYSQL_NUM))
{

/* Here I would like to be able to say: If it's not the last row, echo "blabla", if it's the last row, echo "BLABLA", but it doesn't work: */
if ($rowCounter != $numberOfRows) {
echo "blabla";
}

elseif ($rowCounter == $numberOfRows) {
echo "BLABLA";
}
$rowCounter++;
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.