How to create next/previous page

Discussion in 'PHP' started by myfafa, May 29, 2008.

  1. #1
    hi

    The out put of this code is more than 10 records, i want to create a numeric page (1 , 2, ...)or next/previous page to show these records, any idea?

    ----------------------------
    <?php
    $rs['create'] = "true";
    $counter=1;
    if(Util::executeQuery("SELECT ID,name FROM category ORDER BY name",$rs) > 0){
    foreach ($rs as $row){
    echo "<tr align='right'>";
    echo "<td><span class='style2'>$counter</span></td>";
    echo "<td><span class='value'>".$row['name']."</span></td>";

    echo "<td align='center'><span><a href ='Categories.php?remove=".$row['ID']."'><img border ='0' src='../images/remove.gif' alt=\"delete from db!\"/></a></span></td>";
    echo "</tr>\n";

    if($counter == 10){
    break;
    } $counter ++;
    }
    }else{ ?>
    <tr>
    <td>&nbsp;</td>
    <td colspan="2"><span class="value">no category was found!</span></td>
    <td>&nbsp;</td>
    </tr>
    <? }?>
    -----------------------------------

    thanks alot
     
    myfafa, May 29, 2008 IP
  2. kmap

    kmap Well-Known Member

    Messages:
    2,215
    Likes Received:
    29
    Best Answers:
    2
    Trophy Points:
    135
    #2
    <?php
    $rs['create'] = "true";
    $counter=1;
    if(Util::executeQuery("SELECT ID,name FROM category ORDER BY name",$rs) > 0){
    foreach ($rs as $row){
    $counter ++;
    }
    }
    $page=$_GET["page"];
    $up=(10*$page)-1;
    $down=$up-10;
    $cou=0;
    if(Util::executeQuery("SELECT ID,name FROM category ORDER BY name LIMIT ".$down.",".$up,$rs) > 0){
    foreach ($rs as $row){
    echo "<tr align='right'>";
    echo "<td><span class='style2'>$counter</span></td>";
    echo "<td><span class='value'>".$row['name']."</span></td>";
    echo "<td align='center'><span><a href ='Categories.php?remove=".$row['ID']."'><img border ='0' src='../images/remove.gif' alt=\"delete from db!\"/></a></span></td>";
    echo "</tr>\n";

    $cou++;
    }
    }else{ ?>
    <tr>
    <td>&nbsp;</td>
    <td colspan="2"><span class="value">no category was found!</span></td>
    <td>&nbsp;</td>
    </tr>
    <? }
    //Page links

    $totalPages = ceil(($counter/10));
    if($totalPages > 267){
    $totalPages = 267;
    }
    paging($page,$totalPages);
    function paging($page,$totalPages)
    {
    $printPages = 7;
    $i = 1;
    if($totalPages < $printPages){

    $printPages = $totalPages;

    }



    if($page > 1){

    echo "<a href='nameofyourpage.php?page=1'> << First | </a>";

    }



    if($page-3 <= 0){

    $currentPage = 1;

    }elseif($page > 3 && $page + 3 < $totalPages){

    $currentPage = $page-3;

    }else{

    $currentPage = $totalPages-($printPages-1);

    }







    while($i <= $printPages){

    echo "<a href='nameofyourpage.php?page=".$currentPage."'> ".$currentPage." | </a>";

    $currentPage++;

    $i++;

    }



    if($page < $totalPages){

    echo "<a href='nameofyourpage.php?page=".$totalPages."'> Last >> </a>";

    }



    } //end of function




    echo "<br>Page: ".$page." OF ".$totalPages;

    ?>
     
    kmap, May 29, 2008 IP
  3. myfafa

    myfafa Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hi dear kmap, thank for reply
    i used your script and add just these lines too, but this error has occur:

    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-1,9' at line 1

    ------------------------------
    <?php
    $rs['create'] = "true";
    $counter=1;
    if(Util::executeQuery("SELECT ID,name FROM category ORDER BY name",$rs) > 0){
    foreach ($rs as $row){
    $counter ++;
    }
    }
    $page="1";
    if (isset($_GET['page']))

    $page=$_GET["page"];
    $up=(10*$page)-1;
    $down=$up-10;
    $cou=0;
    if(Util::executeQuery("SELECT ID,name FROM category ORDER BY name LIMIT ".$down.",".$up,$rs) > 0){
    foreach ($rs as $row){
    echo "<tr align='right'>";
    echo "<td><span class='style2'>$counter</span></td>";
    echo "<td><span class='value'>".$row['name']."</span></td>";
    echo "<td align='center'><span><a href ='Categories.php?remove=".$row['ID']."'><img border ='0' src='../images/remove.gif' alt=\"delete from db!\"/></a></span></td>";
    echo "</tr>\n";

    $cou++;
    }
    }else{ ?>
    <tr>
    <td>&nbsp;</td>
    <td colspan="2"><span class="value">no category was found!</span></td>
    <td>&nbsp;</td>
    </tr>
    <? }
    //Page links

    $totalPages = ceil(($counter/10));
    if($totalPages > 267){
    $totalPages = 267;
    }
    paging($page,$totalPages);
    function paging($page,$totalPages)
    {
    $printPages = 7;
    $i = 1;
    if($totalPages < $printPages){
    $printPages = $totalPages;

    }
    if($page > 1){

    echo "<a href='nameofyourpage.php?page=1'> << First | </a>";
    }
    if($page-3 <= 0){

    $currentPage = 1;

    }elseif($page > 3 && $page + 3 < $totalPages){

    $currentPage = $page-3;

    }else{

    $currentPage = $totalPages-($printPages-1);

    }

    while($i <= $printPages){

    echo "<a href='nameofyourpage.php?page=".$currentPage."'> ".$currentPage." | </a>";

    $currentPage++;

    $i++;

    }

    if($page < $totalPages){

    echo "<a href='nameofyourpage.php?page=".$totalPages."'> Last >> </a>";

    }


    } //end of function
    echo "<br>Page: ".$page." OF ".$totalPages;
    ?>
    ---------------------------
     
    myfafa, May 29, 2008 IP