tags:

views:

62

answers:

3

Hello everyone, wondered if you can help me with a php/query question.

I want to format the first row differently to the rest of the rows (always 20 returned).

My query looks like this right now:

<?php
$nt=array();
$query="SELECT * FROM  table WHERE field=item ORDER BY date DESC LIMIT 20";
$rt=mysql_query($query);
echo mysql_error();

while($nt=mysql_fetch_array($rt)){    
echo "<p class='columnitem'><a href='/item/".$nt[id]."'>".$nt[Title]."</a></p>";
}
?>

Which obviously formats all the rows the same, help please!

+2  A: 

Simplest solutions usually work the best:

$row=1;
while($nt=mysql_fetch_array($rt)){                              
if($row==1){
    echo "<p class='firstrow'><a href='/item/".$nt[id]."'>".$nt[Title]."</a></p>";
} else {
    echo "<p class='columnitem'><a href='/item/".$nt[id]."'>".$nt[Title]."</a></p>";
}
$row=$row+1;
}
quosoo
beautiful, thank you very much indeed!
bluedaniel
A: 
<?php
$nt=array();
$query="SELECT * FROM  table WHERE field=item ORDER BY date DESC LIMIT 20";
$rt=mysql_query($query);
echo mysql_error();

if ($nt=mysql_fetch_array($rt)) {   
    ... do first line ...
}
while($nt=mysql_fetch_array($rt)){                              
echo "<p class='columnitem'><a href='/item/".$nt[id]."'>".$nt[Title]."</a></p>";
}
?>
DigitalRoss
A: 

You could do this from the MySQL side too:

$nt=array();
$query="SET @rownum = 0; SELECT (@rownum:=@rownum+1) as rownum,table.* FROM  table WHERE field=item ORDER BY date DESC LIMIT 20";
$rt=mysql_query($query);
echo mysql_error();

while($nt=mysql_fetch_array($rt)){                              
echo "<p class='" . ($nt['rownum'] == 1 ? 'firstrow' : 'columnitem') . "'><a href='/item/".$nt[id]."'>".$nt[Title]."</a></p>";
}
?>

It's a bit of a hack, though, because you have to remember to set the @rownum variable manually. Here's wishing some future version of MySQL gets something like SQL Server's ROW_NUMBER() function...

Ken Keenan