how to split mysql in pages in php

Discussion in 'PHP' started by tastro, Jan 16, 2009.

  1. #1
    hi,

    please if someone could teach me this one:
    the samplest (shortest code) way to make pages NEXT and PREVIOUS.

    thank you, tastro
     
    tastro, Jan 16, 2009 IP
  2. crath

    crath Well-Known Member

    Messages:
    661
    Likes Received:
    33
    Best Answers:
    0
    Trophy Points:
    100
    #2
    give each page an ID, and each link has the current id +1 or -1, depending on the direction they want to navigate.

    your very vague so if that isn't what you were looking for, give more detail
     
    crath, Jan 16, 2009 IP
  3. cont911

    cont911 Peon

    Messages:
    50
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #3
    cont911, Jan 16, 2009 IP
  4. tastro

    tastro Member

    Messages:
    68
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #4
    could someone post an example in php code please ? thank you...
     
    tastro, Jan 23, 2009 IP
  5. mrmaf

    mrmaf Peon

    Messages:
    41
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Dear Tastro, Here is the Code that i write just for you, change it according to your database table column names, if you want to understand this code more clearly then Please click to my signature (nice-tutorials.blogspot.com) and then go to Pagination in php Post.

    <?php
    require_once('conn.php');
    // how many rows to show per page
    $rowsPerPage = 15;
    // by default we show first page
    $pageNum = 1;
    // if $_GET['page'] defined, use it as page number
    if(isset($_GET['page']))
    {
    $pageNum = $_GET['page'];
    }
    // counting the offset
    $offset = ($pageNum - 1) * $rowsPerPage;
    $query = "select * from student" .
    " LIMIT $offset, $rowsPerPage";
    //print $query;
    $result=mysql_query($query);
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Pagination in php</title>
    <style type="text/css">
    .fontclass
    {font-family:Verdana;
    font-size:12px;
    }
    </style>
    </head>
    <body>
    <table width="100%" cellpadding="2" cellspacing="2" class="fontclass">
    <tr>
    <td width="12%">
    <strong>Roll No</strong></td>
    <td width="16%">
    <strong>First Name</strong></td>
    <td width="20%">
    <strong>Last Name</strong></td>
    <td colspan="2">
    <strong>Father Name</strong></td>
    </tr>
    <?php
    while($rows=mysql_fetch_array($result))
    {
    ?>
    <tr>
    <td width="12%">
    <?php echo $rows['rollno'];?></td>
    <td width="16%">
    <?php echo $rows['fname'];?></td>
    <td width="20%">
    <?php echo $rows['lname'];?></td>
    <td width="12%">
    <?php echo $rows['fathername'];?></td>
    <td width="40%"><strong><a href="delete-rec.php?rollno=<?php echo $rows['rollno'];?>">Delete</a></strong></td>
    </tr>
    <?php
    }
    ?>
    </table>
    <?php
    $query = "SELECT COUNT(rollno) AS numrows FROM student";
    $result = mysql_query($query) or die('Error, query failed');
    $row = mysql_fetch_array($result, MYSQL_ASSOC);
    $numrows = $row['numrows'];
    // how many pages we have when using paging?
    $maxPage = ceil($numrows/$rowsPerPage);
    // print the link to access each page
    $self = $_SERVER['PHP_SELF'];
    // creating previous and next link
    // the first and last page
    if ($pageNum > 1)
    {
    $page = $pageNum - 1;
    $prev = " <a href=\"$self?page=$page\">[Prev]</a> ";
    $first = " <a href=\"$self?page=1\">[First Page]</a> ";
    }
    else
    {
    $prev = '&nbsp;'; // we're on page one, don't print previous link
    $first = '&nbsp;'; // nor the first page link
    }
    if ($pageNum < $maxPage)
    {
    $page = $pageNum + 1;
    $next = " <a href=\"$self?page=$page\">[Next]</a> ";
    $last = " <a href=\"$self?page=$maxPage\">[Last Page]</a> ";
    }
    else
    {
    $next = '&nbsp;'; // we're on the last page, don't print next link
    $last = '&nbsp;'; // nor the last page link
    }
    // print the navigation link
    echo $first . $prev .
    " Showing page $pageNum of <B>$maxPage</B> pages " . $next . $last;
    ?>


    </body>

    </html>
     
    mrmaf, Jan 23, 2009 IP
  6. olddocks

    olddocks Notable Member

    Messages:
    3,275
    Likes Received:
    165
    Best Answers:
    0
    Trophy Points:
    215
    #6
    olddocks, Jan 24, 2009 IP
  7. Dennis M.

    Dennis M. Active Member

    Messages:
    119
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    58
    #7
    Here is a simple script you can take a look at. Will do exactly what you're looking for. Just a quick thing I whipped up, about 10 minutes. Tested, so it works :) Just need to edit to your needs (kind of a hack job) if you need more specific help feel free to PM me and I'll write up a quick script for you.

    <?php
    // Connect to DB
    mysql_connect("localhost","USER","PASS");
    mysql_select_db("USER_DATABASE");
    
    // Define results per page
    $perPage = 1;
    
    // Define page number in URL
    $num = $_GET['no'];
    
    // Define link starting point.
    $linkno = 1;
    
    // If no number given, default it.
    if(!isset($num) OR !is_numeric($num)){
    	$num = 1;
    }
    
    // Multiply to see where we're at to show results :)
    $currentLimit = $perPage*($num-1);
    
    // Get how many pages we'll have total (for links)
    $numPages = mysql_num_rows(mysql_query("SELECT * FROM pagination"))/$perPage;
    
    $query = mysql_query("SELECT * FROM pagination ORDER BY id ASC LIMIT ".mysql_escape_string($currentLimit).",".mysql_escape_string($perPage).";");
    
    // Fetch all the info
    while($row = mysql_fetch_array($query)){
    	$title = stripslashes($row['name']);
    	$article = stripslashes($row['content']);
    ?>
    <html>
    <head>
    <title>Pagination Test</title>
    </head>
    <body>
    <h1><?php echo($title); ?></h1><br />
    <p><?php echo($article); ?></p><br /><hr>
    <?php
    }
    print "<p align=\"right\">";
    if($perPage <= 2){
    	while($links < round($numPages)){
    ?>
    <a href="?no=<?php echo($linkno); ?>"><?php echo($linkno); ?></a>
    <?php
    		$links++;
    		$linkno++;
    	}
    } else {
    	while($links <= round($numPages)){
    ?>
    <a href="?no=<?php echo($linkno); ?>"><?php echo($linkno); ?></a>
    <?php
    		$links++;
    		$linkno++;
    	}
    }
    ?></p>
    </body>
    </html>
    PHP:
    Regards,
    Dennis M.
     
    Dennis M., Jan 24, 2009 IP
  8. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120