Posted on 2006-04-03
Last Modified: 2008-12-20
I wonder if anybody would be kind enough to adapt my code to have the following pagination.  My code so far is below.  Many thanks.


            $query = "select vacid, Title, ShortDescription, Location, Salary, Category, Contact, Display, Archive, JobType, Datestamp,  date_format( datestamp,  '%d %M %y'  )  as date from tbl_Vacancies where JobType like '%Permanent%' and ExpirationDate > '$today' order by date desc";
      $result = mysql_query($query);
       echo "<table width='100%' border='0' cellspacing='0' cellpadding='0'>";
       echo "<tr>";
     echo "<td width='5%' class='sectiontableheader' align='left'>Job&nbsp;ID&nbsp;</td>";
       echo "<td width='18%' class='sectiontableheader' align='left'>Date Posted</td>";
       echo "<td width='47%' class='sectiontableheader' align='left'>Job Title</td>";
       echo "<td width='30%' class='sectiontableheader' align='left'>Company</td>";
       echo "</tr>";
       $color1 = "#f9f9f9";  
     $color2 = "#ffffff";
       $row_count = 0;
     while($row = mysql_fetch_array($result)){
       $row_color = ($row_count % 2) ? $color1 : $color2;
       echo "<tr class='sectiontableentry'>";
       echo "<td bgcolor='$row_color' width='5%' align='left'>$row[vacid]</td>";
       echo "<td bgcolor='$row_color' width='18%' align='left'>$row[Datestamp]</td>";
       echo "<td bgcolor='$row_color' width='47%' align='left'><b><a href='../vacancies/viewvacancy.php?id=$row[vacid]' class='vaclink'>$row[Title]</a></b></td>";
       echo "<td bgcolor='$row_color' width='50%' align='left'>$row[Company]</td>";
       echo "</tr>";
              echo "</table>";
Question by:hutmasterx
    LVL 20

    Assisted Solution

    I'll give you a few rough pointers on how to ge there:

    make it so the pages reflect links like this:


    modify this by adding a LIMIT to the end of the query.
    $page_size = 20;
    $page_bump = ($_GET['page'] - 1) * $page_size;
    $query = "select vacid, Title, ShortDescription, Location, Salary, Category, Contact, Display, Archive, JobType, Datestamp,  date_format( datestamp,  '%d %M %y'  )  as date from tbl_Vacancies where JobType like '%Permanent%' and ExpirationDate > '$today' order by date desc LIMIT $page_bump, $page_size";

    for more info see

    this would give you static pagination and work for a system of 100 records with pages of 20.

    to adapt the system to appropriately handle the right number of pages...
    instead of statically generating the links for the pages (as in my code above), use the following:

    $get_count_result = mysql_query("SELECT count(*) as mycount FROM from tbl_Vacancies where JobType like '%Permanent%' and ExpirationDate > '$today'");
    $get_count_obj = mysql_fetch_object($get_count_result);
    $total_count = $get_count_obj->mycount;
    $page_size = 20;
    $count = 0;
    $page = 0;
    while ($count < $total_count) {
     $count = $count + $page_size;
     echo '<A HREF="/mypage.php?page=' . $page . '">$page</A>';

    hope that tells you where to go from here.
    LVL 16

    Accepted Solution

    here is a sample code taken from some tutorial sites, hope u can follow and understand

    <title>Implementing Paging with next and prev</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

    include '../library/config.php';
    include '../library/opendb.php';

    // how many rows to show per page
    $rowsPerPage = 20;

    // by default we show first page
    $pageNum = 1;

    // if $_GET['page'] defined, use it as page number
        $pageNum = $_GET['page'];

    // counting the offset
    $offset = ($pageNum - 1) * $rowsPerPage;

    $query  = "SELECT val FROM randoms LIMIT $offset, $rowsPerPage";
    $result = mysql_query($query) or die('Error, query failed');

    // print the random numbers
    while($row = mysql_fetch_array($result))
        echo $row['val'] . '<br>';
    echo '<br>';

    // how many rows we have in database
    $query   = "SELECT COUNT(val) AS numrows FROM randoms";
    $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'];
    $nav = '';
    for($page = 1; $page <= $maxPage; $page++)
        if ($page == $pageNum)
            $nav .= " $page ";   // no need to create a link to current page
            $nav .= " <a href=\"$self?page=$page\">$page</a> ";

    // creating previous and next link
    // plus the link to go straight to
    // 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> ";
        $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> ";
        $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 . $nav . $next . $last;

    // and close the database connection
    include '../library/closedb.php';

    this code is taken from site, there u'll see full explanation to the above code

    Author Comment

    Many thanks :-)

    Featured Post

    Courses: Start Training Online With Pros, Today

    Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

    Join & Write a Comment

    Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
    This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now