Forum Moderators: coopster

Message Too Old, No Replies

newbie needs help

paging problem

         

Nuit

6:47 am on Apr 14, 2004 (gmt 0)

10+ Year Member



Hi everyone, im having a problem with paging in php. I've tried some scripts but they don't work... so im posting my entire script here and i hope somebody can help. What i need is to display 50 products per page. The total number of products is 200.

<html>
<?php

$db = mysql_connect("localhost","root","");
mysql_select_db("project",$db);
$result = mysql_query("SELECT * FROM products",$db);

echo "<table border=2>";
echo "<tr><td>&nbsp;</td><td><b>Category</b> <td><b>Id</b><td><b>Description</b><td><b>Price</b> <td><b>Quantity</b></tr>";
while ($myrow = mysql_fetch_array($result))
{echo "<TR><TD><INPUT TYPE=CHECKBOX Value='".$myrow["Id"]."'><TD>".$myrow["cat"]. "<TD>".$myrow["Id"]."<TD>".$myrow["desc"]."<TD>" ."$".$myrow["price"]."<TD><input type='text' size=2 maxlength=2 value='0'>";

}
echo "</table>";
?>
<p><a href="home.php"><img src="garlic.jpg" width="50" height="46" border="0"></a>
<strong><a href="home.php">Home</a></strong> </p>
</html>
<style type="text/css">
<!--
body {
background-color: #FFFF99;
}
body,td,th {
color: #333333;
}
-->
</style>
<title>Products</title>
<body>
<div align="center"></div>
<FORM METHOD=GET ACTION="http://localhost/~calista/project/viewcart.php" TARGET="" >
<div align="center"><FONT SIZE="+1" COLOR="#000000" FACE="Times Roman">
<INPUT TYPE=SUBMIT VALUE="Put these in my cart!">
</FONT> </div>
</FORM>
</body>
</html>

[edited by: jatar_k at 4:04 pm (utc) on April 14, 2004]
[edit reason] fixed sidescroll [/edit]

tomda

7:46 am on Apr 14, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You need to use the rowstart variable and use the LIMIT in your MySql request. By the way, put your style sheet and title tag and header tag at the top of the script (above the php). It is done on the fly, so there may be some errors but at least try to understand how it works...


<html>
<style type="text/css">
<!--
body {
background-color: #FFFF99;
}
body,td,th {
color: #333333;
}
-->
</style>
<title>Products</title>
<body>

<?php
$qty = 50; //the number of item per page
if(isset($_GET["rowstart"])) {$rowstart = $_GET["rowstart"];} else {$rowstart = "0";} //check is rowstart is not NULL, if NULL then rowstart is equal to 0 otherwise rowstart is equal to its current value

$db = mysql_connect("localhost","root","");
mysql_select_db("project",$db);
$result = mysql_query("SELECT * FROM products LIMIT $rowstart,$halfb",$qty); // set a limit, that is starting from rowstart and show qty images (here 50)
$numrows=mysql_num_rows($result); // Count the total No of rows

echo "<table border=2>";
echo "<tr><td>&nbsp;</td><td><b>Category</b><td> <b>Id</b><td><b>Description</b><td><b>Price</b> <td><b>Quantity</b></tr>";
while ($myrow = mysql_fetch_array($result))
{echo "<TR><TD><INPUT TYPE=CHECKBOX Value='".$myrow["Id"]."'><TD>".$myrow["cat"]. "<TD>".$myrow["Id"]."<TD>".$myrow["desc"]."<TD>" ."$".$myrow["price"]."<TD><input type='text' size=2 maxlength=2 value='0'>";

}
echo "</table>";
?>
<p><a href="home.php"><img src="garlic.jpg" width="50" height="46" border="0"></a>
<strong><a href="home.php">Home</a></strong> </p>
</html>

<div align="center"></div>
<FORM METHOD=GET ACTION="http://localhost/~calista/project/viewcart.php" TARGET="" >
<div align="center"><FONT SIZE="+1" COLOR="#000000" FACE="Times Roman">
<INPUT TYPE=SUBMIT VALUE="Put these in my cart!">
</FONT> </div>
</FORM>

$rowstart_prev = $rowstart-$qty;
$rowstart_next = $rowstart+$qty;

if ($rowstart > $qty) {
echo "<< <a href='http://localhost/~calista/project/viewcart.php?rowstart=".$rowstart_prev."'>Previous</a>";}
//Check if rowstart is bigger than qty, if yes it means that there is a previous page

if ($rowstart_next > $numrows) { } else {
echo "<a href='http://localhost/~calista/project/viewcart.php?rowstart=".$rowstart_next."'>Next >></a>";}
// check if rowstart_next is bigger than the total numbers of rows, if yes then there are no next page, else there is.

</body>
</html>


Again it is on the fly, to test... Also google with "rowstart tutorial" should help.

Tomda

[edited by: jatar_k at 4:05 pm (utc) on April 14, 2004]
[edit reason] fixed sidescroll [/edit]

Nuit

8:14 am on Apr 14, 2004 (gmt 0)

10+ Year Member



thanks! il go try it now :)

Nuit

9:08 am on Apr 14, 2004 (gmt 0)

10+ Year Member



erm sorry have a question,
$result = mysql_query("SELECT * FROM products LIMIT $rowstart,$halfb",$qty); // set a limit, that is starting from rowstart and show qty images (here 50)

how do u set the limit? like this: Limit_0,50?

coopster

12:35 pm on Apr 14, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member




The
LIMIT
clause can be used to constrain the number of rows returned by the
SELECT [dev.mysql.com] statement.
LIMIT
takes one or two numeric arguments, which must be integer
constants. With two arguments, the first argument specifies the offset of the first row
to return, and the second specifies the maximum number of rows to return.
The offset of the initial row is 0 (not 1):
SELECT * FROM table LIMIT 5,10; # Retrieve rows 6-15
With one argument, the value specifies the number of rows to return from the
beginning of the result set:
SELECT * FROM table LIMIT 5; # Retrieve first 5 rows
In other words, LIMIT n is equivalent to LIMIT 0,n.

I'm not sure where the $halfb variable comes in here...? Perhaps inadvertently left over by tomda as he was laying it out for you? Tracking his line of thought, I would think the query should read as follows...

$result = mysql_query("SELECT * FROM products LIMIT $rowstart,$qty");