MySQL query limit part of query
I have a mysql query:
$result = mysql_query("SELECT * FROM $table WHERE cat = 'category'");
while($row = mysql_fetch_array($result)) {
echo '
<hgroup><h3>'.$row['mag'].'</h3><h4>'.$row['date'].'</h4></hgroup>
<a href="'.$row['href'].'" onclick="this.target=\'blank;\'">'.$row['title'].'</a>
';
}
This query will generally select between 2 and 5 different rows 开发者_如何转开发and display them in a list.
I want the first echoed line to only appear once and the second line should appear between 2 and 5 depending on the data in my db.
I am sure there is a simple way to do this, I've tried GROUP BY mag but this will eliminate the remaining 1-4 pieces of data I wish to display.
Not sure I understand your question, as the following solution seems too simple!
$row = mysql_fetch_array($result);
echo '<hgroup><h3>'.$row['mag'].'</h3><h4>'.$row['date'].'</h4></hgroup>
<a href="'.$row['href'].'" onclick="this.target=\'blank;\'">'.$row['title'].'</a>';
while ($row = mysql_fetch_array($result)) {
echo '<a href="'.$row['href'].'" onclick="this.target=\'blank; \'">'.$row['title'].'</a>';
}
May be this is a solution to your problem ?
$lines = '';
unset($hgroup);
$result = mysql_query("SELECT * FROM $table WHERE cat = 'category'");
while($row = mysql_fetch_array($result)) {
if (!isset($hgroup)) {
$hgroup = '<hgroup><h3>'.$row['mag'].'</h3><h4>'.$row['date'].'</h4></hgroup>';
}
$lines += '
<a href="'.$row['href'].'" onclick="this.target=\'blank;\'">'.$row['title'].'</a>
';
}
echo $hgroup.$lines;
精彩评论