help with pagination please

Discussion in 'PHP' started by Kyriakos, Apr 17, 2008.

  1. #1
    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>&nbsp;<%ELSE%><%=page%>&nbsp;<% 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
     
    Kyriakos, Apr 17, 2008 IP
  2. m0nkeymafia

    m0nkeymafia Well-Known Member

    Messages:
    399
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    125
    #2
    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):
     
    m0nkeymafia, Apr 18, 2008 IP
  3. Kyriakos

    Kyriakos Active Member

    Messages:
    155
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #3
    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 .= "&nbsp;<b>$i</b>"; ?><?php }Else{ ?><?php $Nav .= "&nbsp;<a href=\"showproducts.php?pg=" . $i . "\">$i</a>";
    }
    }
    Echo "" . $Nav;
    ?>
    PHP:
     
    Kyriakos, Apr 18, 2008 IP
  4. Kyriakos

    Kyriakos Active Member

    Messages:
    155
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #4
    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?
     
    Kyriakos, Apr 18, 2008 IP
  5. m0nkeymafia

    m0nkeymafia Well-Known Member

    Messages:
    399
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    125
    #5
    Thats basically what I said :)
     
    m0nkeymafia, Apr 18, 2008 IP