Re: display 4 at a time

From: Date: Fri, 31 May 2002 02:59:05 +0000
Subject: Re: display 4 at a time
References: 1  Groups: php.db 
Request: Send a blank email to php-db+get-19544@lists.php.net to get a copy of this message
What's wrong with the standard LIMIT?? Something like this: --------------------------------------- <? echo " <HTML><HEAD><TITLE>Product List (4 UP)</TITLE></HEAD> <FORM ACTION="this_program.php" METHOD='POST'> "; if (!isset($page)) $page = 0; $sql = " SELECT * FROM products WHERE prod_cat = $cat_ID LIMIT $page,4"; $mysql_result=mysql_query($sql,$connection); while ($row = mysql_fetch_array($mysql_result) { $prod_name = $row["prod_name"]; $prod_image = $row["prod_image"]; $prod_ID = $row["ID"]; echo "<img src=\"products/$prod_image\" border=0 height=150><br>$prod_name<br><br>"; } $page += 4; echo " <INPUT TYPE='HIDDEN' NAME='page' VALUE='$page'> <INPUT TYPE='SUBMIT' NAME='Submit' VALUE='Next 4'> </FORM> "; ?> ----------------------------------------------- Or is this not what you are after. HTH. Good luck.. Dan Jen Swofford wrote:
Time for a slightly less-newbie question than before from me. Believe me, I've RTF'nM and I'm *not* getting this. :( So I am currently displaying all products from one product category: $sql2="select * FROM products WHERE prod_cat = $cat_ID"; $mysql_result=mysql_query($sql2,$connection); $num_rows=mysql_num_rows($mysql_result); if ($num_rows==0){ echo "no results"; } else { while ($row=mysql_fetch_array($mysql_result)){ $prod_name=$row["prod_name"]; $prod_image=$row["prod_image"]; $prod_ID=$row["ID"]; echo "<img src=\"products/$prod_image\" border=0 height=150><br>$prod_name<br><br>"; } } What I'm trying to do is have the ability to page through the database just 4 products at a time. (This is a regular online catalog and I'm just trying to have a "next 4 products" link on the category page.) I know how to display only 4 products. What I can't figure out is to remember where I was in displaying them. I'm thinking "array" here... but the arrays we typically get are of the fields in the database, not the rows. I want an array that stores all the rows, not all the fields. What am I missing? The logic I've come up with is to do this: $sql2="select * FROM products WHERE prod_cat = $cat_ID"; $mysql_result=mysql_query($sql2,$connection); $num_rows=mysql_num_rows($mysql_result); if ($num_rows==0){ echo "no results"; } else { while ($row=mysql_fetch_array($mysql_result)){ // 1) create an array of all the product_id's in the result (one from each row) // 2) then select product_id 4 at a time and display them on the page
		//    ... but that'd be a buttload of db hits
} } I can't even quite figure out the logic here. If anybody is interested in simply pointing me in the right direction, like say, "Jen, don't you know about mysql_fetch_jen's_type_of_array?!" then perhaps I can figure it out from there. Thank you very much in advance. Jen Swofford jen@littleduck.com


« previous php.db (#19544) next »