Question : PHP MySQL  mysql_fetch_assoc get first and last elements

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.

Answer : PHP MySQL  mysql_fetch_assoc get first and last elements

$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;
Random Solutions  
 
programming4us programming4us