tags:

views:

51

answers:

2

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.

A: 

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>';
}
eclipse31
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;
Alain Verleyen
this doesn't quite do what i'm looking for, i'm not sure i explained myself well enough
thomas