We help IT Professionals succeed at work.

PHP MySQL  mysql_fetch_assoc get first and last elements

onetoomany
onetoomany used Ask the Experts™
on
Not sure how to identify the first and last elements of the result... I need to append some html attributes. Currently I have this:

$getfiles = mysql_query("SELECT * FROM tblfiles WHERE fkey = '$thiskey' ORDER BY fkey ASC ") or die (mysql_error());
while($row = mysql_fetch_assoc($getfiles))
      {
echo '<p>'.$row['filename'].'</p>;
        }

I need the output to be something like this for say results of 4 rows:

<p class="first">File 1</p>
<p>File 2</p>
<p>File 3</p>
<p class="last">File 4</p>

Thanks.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
$table = '';
$getfiles = mysql_query("SELECT * FROM tblfiles WHERE fkey = '$thiskey' ORDER BY fkey ASC ") or die (mysql_error());
while($row = mysql_fetch_assoc($getfiles))
{
    $table .= '<p>'.$row['filename'].'</p>';
}
$table = preg_replace('/^<p>/', '<p class="first">', $table);
$table = preg_replace('/<p>([^<]*)</p>$/', '<p class="last">\1</p>', $table);

echo $table;

Author

Commented:
thanks.