When listing information from a database using php and mysql how would you make the first row look different to the rest?


Problem :

Basically I have articles in my database and I want to alter the way the first record displays. I want the lastest (Posted) article to be the focus and the older article just to list, (see F1.com). I need to know how to get the first of my values in the array and get it to display differently but I am not sure how to do this, I can do it so all rows display the same just not how to alter the first row. I also need to know how to tell the rest of the rows to display the same afterwards im guessing you use an if statement there and before that some kind of count for the rows.


Current code:

    $result = mysql_query("SELECT * FROM dbArticle WHERE userID='".$_SESSION["**"]."' ORDER BY timestamp DESC");

while($row = mysql_fetch_array($result))
  {
  echo "<h2 class=\"heading1\">". $row['title'] ."</h2>";
  echo "By:  ".$row['username']."  Type: ".$row['type']."  Posted: ".$row['timestamp']." 
  $body = $row['body'];
  echo "<br/><p>";
  echo substr("$body",0,260);
  echo "...<span class=\"tool\"><a class=\"blue\" href=\"index.php?pageContent=readArticle&id=".$row['id']."\">Read More</a></span></p><hr/>";
  }
mysql_close($con);

Ok I have taken Luke Dennis's code and tried to test it, but I am getting this error: Warning: Invalid argument supplied for foreach() this is the line of the foreach statment. Something that has just come to mind is that I will only want 5 or so of the older articles to display. This is what I have thats creating the error:

<? $con = mysql_connect("localhost","****","***");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("******", $con);

$result = mysql_query("SELECT * FROM dbArticle ORDER BY timestamp DESC");
$first = true;    
foreach($result as $row){
    if($first)
    {
        echo"".$row['title']."";
        echo"this is the headline";

        $first = false;
    }
    else
    {
        echo"".$row['title']."";

    }
}

?>

Do I need to add mysql_fetch_array somewhere to set the array up?



Solution :

I would just iterate through the results and apply a css class to the first entry:

$first = true;    
while ($row = mysql_fetch_assoc($result)) {
  $cssClass = '';
  if ($first) {
    $cssClass = 'highlight';
  }
  echo '<p class="' . $cssClass . '">' . $row['text'] . '</p>';
  $first = false;
}

Mysql Tutorials

Mysql Howto..

How to add foreign key to MySQL table?

How do you properly update a mysql field with NULL?

How to selectively export mysql data for a github repo

How I translate mysql database/table data to cassandra data files…?

How do I read a “,” as “
” in PHP/MySQL? [duplicate]

How to validate a date in MySQL that also has a date in alphanumeric

How to delete variabilities from MySQL database?

Mysql how to reduce rows by max datetime and group by non-uniqe id

How to get a specific combination of columns in MySQL

How to insert a value using mysql query in DB for all the rows except few

How to insert something into a table when another table holds a specific value using MySQL

How to select unique records from mysql table

How would I construct a mysql query to use multiple AND operators?

How to clone a mysql database using shell and terminal without taking mysql dump and uploading

How to have a range in a MySQL If statement?

Upgrading SQL Server 2000 to MySQL 5.1 using DTS - How solve DateTime problem?

How to display image depending of result in MySQL query, using PHP

How to embed mysql db content into 3rd party sites without providing remote access

How to convert list of nested dictionaries into string and vice versa

PHP / MySQL: How to return info if record already exists (without update)

How to get the top N values of each column in MySql

How to prevent duplicate usernames when people register? PHP/MySQL

How to use join in MySQL

GROUP BY mysql showing only one row. But want repetitions also

How to manage databases with limited amounts of data

How to echo all names of a column in all rows in php

how to upload file to mysql table using path in yii

how to select Count of Ranges from mysql table?

How to upload photo and photo's description TOGETHER in iphone app?

How to extract hour for MySQL FROM_UNIXTIME() results?