0

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>
3
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by liamfriel
0

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>';

Edited by liamfriel: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.