PDA

View Full Version : Display a specific amount of entries


crazyfish
March 18th, 2006, 20:48
I setup a small car inventory page. http://www.myerstowing.com/inventory/index.php Right now it displays all the entries that are in the inventory. I would like it to display only 5 entries, they can be random. But I have no clue how to do that.

<?
if($cat) $where = "AND cat='$cat'";
$qr1 = mysql_query("SELECT * FROM product WHERE status=1 $where ORDER BY id DESC");
while( $row = mysql_fetch_object($qr1) ){
?>
<table cellpadding=5 cellspacing=0 width=100%>
<tr>
<td align=left width=25%>
<?
$handle=opendir('./uploads/');
while (false!==($file = readdir($handle))) {
if ($file != "." && $file != "..") {
if( strstr($file,"x_".$row->id.".") ){
?>
<a href="<?=$siteurl?>/product.php?id=<?=$row->id?>" class="nav">
<img src="<?=$siteurl?>/uploads/<?=$file?>" border=0 width="70" height="59">
</a>
<?
}
}
}
?>
</td>
<td align=left><div align=left>
<strong><a href="<?=$siteurl?>/product.php?id=<?=$row->id?>" class="nav"><?=stripslashes($row->name)?></a></strong><br>
<?=stripslashes($row->summary)?>

How do I display only 5 entries?

Thanks in advance.

the_pm
March 18th, 2006, 21:03
What about adding LIMIT 0, 5 to the end of mysql_query?

$qr1 = mysql_query("SELECT * FROM product WHERE status=1 $where ORDER BY id DESC LIMIT 0, 5");

Hell, I just pulled that from some other code. To be honest, I'm not entirely sure it's relevent here :lol:

About time I learned PHP, eh?

Tyler
March 18th, 2006, 21:04
For the first five you can add LIMIT 0,5 to your SQL query :)


Darn Paul beat me :ko: :lol:

the_pm
March 18th, 2006, 21:05
Hah! I guessed, and I was right! :dance: :banana2:

crazyfish
March 18th, 2006, 21:21
What about adding LIMIT 0, 5 to the end of mysql_query?

$qr1 = mysql_query("SELECT * FROM product WHERE status=1 $where ORDER BY id DESC LIMIT 0, 5");

Hell, I just pulled that from some other code. To be honest, I'm not entirely sure it's relevent here :lol:

About time I learned PHP, eh?

Perfect. That was way easier then I thought it would be.

Thanks Everyone.