hi, i have a page with some products and when i want to paginate records if they are more than 8. when i had the site with asp my asp code was this: <% Set Con = Server.CreateObject( "ADODB.Connection" ) Con.Open "storeDSN","username","password" %> <% if pg = "" then pg = 1 Set prodRS = Server.CreateObject( "ADODB.Recordset" ) prodRS.ActiveConnection = Con prodRS.CursorType = adOpenStatic prodRS.PageSize = 6 sqlstring = "SELECT * FROM products WHERE status=1 ORDER BY price" RS.Open sqlString RS.AbsolutePage = pg %> <table width="98%" height="22" border="0" align="center" cellpadding="0" cellspacing="2" bgcolor="#f7ffff"> <% WHILE NOT RS.EOF AND rowCount < prodRS.PageSize rowCount = rowCount + 1 %> <tr> <td height="18"><%=RS("product_id")%></td> <td><%=RS("product_name")%></td> <td><%=RS("price")%></td> </tr> <% RS.MoveNext WEND %> </table> <% if RS.PageCount > 1 then %> Page: <% FOR page = 1 to RS.PageCount IF i <> cINT(pg) THEN %> <a href="products.asp?pg=<%=page%>"> <%=i%></a> <%ELSE%><%=page%> <% END IF%> <% NEXT %> <% END IF %> HTML: now the php code is this <?php $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("storedb", $con); $result = mysql_query("SELECT * FROM products WHERE status=1 ORDER BY price"); ?> PHP: all are working fine but i don't know how to paginate. can anybody help me? thanks in advance
Use MySQL LIMIT to limit the number of rows returned.... i.e. $page = 1 $pagesize = 8; $query = "ETC ..... LIMIT ".(($page - 1) * $pagesize).", $pagesize"; Code (markup): This will only bring back the rows you need. Then just provide links to page 1 through to N i.e. for ($i = 1; $i <= $numpages; $i++) { echo "<a href='/mypage.php?page=$i'>$i </a>"; } Code (markup):
i found a php script in google and this is my php code now: <?php mysql_connect("localhost","root","") or die("Unable to connect to SQL server"); mysql_select_db("storedb") or die("Unable to SELECT DB"); $Limit = 8; $result=mysql_query("SELECT * FROM products WHERE status=1 ORDER BY pricer") or die(mysql_error()); $NumberOfResults=mysql_num_rows($result); $NumberOfPages=ceil($NumberOfResults/$Limit); $result=mysql_query("SELECT * FROM products WHERE status=1 ORDER BY price LIMIT " . ($pg-1)*$Limit . ",$Limit") or die(mysql_error()); ?> PHP: <html table with php> <?php //Create and print the Navigation bar $Nav=""; For($i = 1 ; $i <= $NumberOfPages ; $i++) { If($i == $pg) { ?> <?php $Nav .= " <b>$i</b>"; ?><?php }Else{ ?><?php $Nav .= " <a href=\"showproducts.php?pg=" . $i . "\">$i</a>"; } } Echo "" . $Nav; ?> PHP:
i have a litle problem now. when records (rows) are 8 or less it's displaying the number of the page "1". i want to display nothing when records are 8 or less. can you help me?