Hello everyone, i working on a website and i wanna grab data from a table in my database . specifically the last 4 rows that are entered as the database is updated automatically by users. i wanna be able to display each row separately in different div tags. i'm getting a bit of trouble. As i think the data from the table has to be stored in an array and i'm a bit weak in that area. here's what i've done thus far. an example of my div out put is shown below the php tags

<?php
//Perform database query 
$results = mysql_query("SELECT Title FROM News ORDER BY Newsid DESC", $db);
if (!$results) {
	die("Database query failed: " . mysql_error());
		}
 while($row = mysql_fetch_array($results))
{   
   if($row[6] == 0)
	{
	   if(strlen($row[4]) > 600)
		{
		  $brief = substr($row[4], 0, 600) . '...';
		} 
	else
	{
	$brief = $row[4];
	}
} 
else
{
  $brief = $row[5];
}

}					
?>



<div>
<img src="images/image01.jpg" alt="alt text" title="description "/>
<h3><?php  echo $row[?] ?></h3>
<p>	</p>
<a href="http://www.google.com">Link #1</a>
</div>

Recommended Answers

All 2 Replies

if you want the last 4, you should order by DESC and add LIMIT 4 into the query

Simply Change:

$results = mysql_query("SELECT Title FROM News ORDER BY Newsid DESC", $db);

To:

$results = mysql_query("SELECT Title FROM News ORDER BY Newsid DESC LIMIT 4", $db);

This will tell mysql to limit the query to the first 4 results making the query faster.

As for putting in the div tags just change wherever you get data to reflect that i.e.

$brief = '<div>'.substr($row[4], 0, 600) . '...<div>';

or

$brief = '<div>'.$row[4].'</div>';
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.