473,766 Members | 2,060 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

pagination error

232 New Member
this is my search page on which i am getting two parameters from previous page but the problem is that as soon as i click any other next pages my sql query fails as it doesnt get these two parameters
kindly help
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.     session_start();   
  3.  
  4. ?>
  5. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  6. <html xmlns="http://www.w3.org/1999/xhtml">
  7. <head>
  8.    <title></title>
  9.    <meta name="generator" content="Microsoft FrontPage 5.0" />
  10.    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  11.    <meta name="description" content="" />
  12.    <meta name="keywords" content="" />
  13.    <meta name="author" content="" />
  14. <link rel="stylesheet" href="../include/style.css" type="text/css" />
  15. </head>
  16. <body bgcolor="#FFFFFF" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
  17.  
  18. <?php require("../include/header.php"); ?>
  19. <?php require("../include/left.php"); ?>
  20.  
  21.  <div class="heading"> 
  22.     welcome all
  23.    </div>
  24.      <div class="text"> 
  25.     <?php
  26.     //Include the PS_Pagination class
  27.     include('../include/ps_pagination.php');
  28.  
  29.     //Connect to mysql db
  30.     $conn = mysql_connect('localhost','root','');
  31.     if(!$conn) die("Failed to connect to database!");
  32.     $status = mysql_select_db('ncpul1', $conn);
  33.     if(!$status) die("Failed to select database!");
  34. $colv=$_POST["colv"];
  35. $coln=$_POST["coln"];
  36. echo "Search result for $coln------>$colv<br>";
  37.     $sql="SELECT * FROM institute WHERE ".$coln." like '%".$colv."%' order by centrecode asc";
  38.  
  39.     /*
  40.      * Create a PS_Pagination object
  41.      * 
  42.      * $conn = MySQL connection object
  43.      * $sql = SQl Query to paginate
  44.      * 10 = Number of rows per page
  45.      * 5 = Number of links
  46.      * "param1=valu1&param2=value2" = You can append your own parameters to paginations links
  47.      */
  48.     $pager = new PS_Pagination($conn, $sql, 10, 5, "param1=valu1&param2=value2");
  49.  
  50.     /*
  51.      * Enable debugging if you want o view query errors
  52.     */
  53.     $pager->setDebug(true);
  54.  
  55.     /*
  56.      * The paginate() function returns a mysql result set
  57.      * or false if no rows are returned by the query
  58.     */
  59.     $rs = $pager->paginate();
  60.     if(!$rs) die(mysql_error());
  61.     while($row = mysql_fetch_assoc($rs)) {
  62.         echo $row['address'],"<br />\n";
  63.     }
  64.     echo "<br />\n";
  65.     echo "<br />\n";
  66.     echo"<div align='center'>";
  67.     //Display the full navigation in one go
  68.     echo $pager->renderFullNav();
  69.  
  70.     echo "<br />\n";
  71.     echo"</div>";
  72.  
  73. ?>
  74.    </div>
  75.  
  76. <?php require("../include/right.php"); ?><?php require("../include/footer.php"); ?>
  77.  
  78. </body>
  79. </html>
  80.  
this is pagination file code(ps_paginat ion.php)
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. /**
  3.  * PHPSense Pagination Class
  4.  *
  5.  * PHP tutorials and scripts
  6.  *
  7.  * @package        PHPSense
  8.  * @author        Jatinder Singh Thind
  9.  * @copyright    Copyright (c) 2006, Jatinder Singh Thind
  10.  * @link        http://www.phpsense.com
  11.  */
  12.  
  13. // ------------------------------------------------------------------------
  14.  
  15.  
  16. class PS_Pagination {
  17.     var $php_self;
  18.     var $rows_per_page = 10; //Number of records to display per page
  19.     var $total_rows = 0; //Total number of rows returned by the query
  20.     var $links_per_page = 5; //Number of links to display per page
  21.     var $append = ""; //Paremeters to append to pagination links
  22.     var $sql = "";
  23.     var $debug = false;
  24.     var $conn = false;
  25.     var $page = 1;
  26.     var $max_pages = 0;
  27.     var $offset = 0;
  28.  
  29.     /**
  30.      * Constructor
  31.      *
  32.      * @param resource $connection Mysql connection link
  33.      * @param string $sql SQL query to paginate. Example : SELECT * FROM users
  34.      * @param integer $rows_per_page Number of records to display per page. Defaults to 10
  35.      * @param integer $links_per_page Number of links to display per page. Defaults to 5
  36.      * @param string $append Parameters to be appended to pagination links 
  37.      */
  38.  
  39.     function PS_Pagination($connection, $sql, $rows_per_page = 10, $links_per_page = 5, $append = "") {
  40.         $this->conn = $connection;
  41.         $this->sql = $sql;
  42.         $this->rows_per_page = (int)$rows_per_page;
  43.         if (intval($links_per_page ) > 0) {
  44.             $this->links_per_page = (int)$links_per_page;
  45.         } else {
  46.             $this->links_per_page = 5;
  47.         }
  48.         $this->append = $append;
  49.         $this->php_self = htmlspecialchars($_SERVER['PHP_SELF'] );
  50.         if (isset($_GET['page'] )) {
  51.             $this->page = intval($_GET['page'] );
  52.         }
  53.     }
  54.  
  55.     /**
  56.      * Executes the SQL query and initializes internal variables
  57.      *
  58.      * @access public
  59.      * @return resource
  60.      */
  61.     function paginate() {
  62.         //Check for valid mysql connection
  63.         if (! $this->conn || ! is_resource($this->conn )) {
  64.             if ($this->debug)
  65.                 echo "MySQL connection missing<br />";
  66.             return false;
  67.         }
  68.  
  69.         //Find total number of rows
  70.         $all_rs = @mysql_query($this->sql );
  71.         if (! $all_rs) {
  72.             if ($this->debug)
  73.                 echo "SQL query failed. Check your query.<br /><br />Error Returned: " . mysql_error();
  74.             return false;
  75.         }
  76.         $this->total_rows = mysql_num_rows($all_rs );
  77.         @mysql_close($all_rs );
  78.  
  79.         //Return FALSE if no rows found
  80.         if ($this->total_rows == 0) {
  81.             if ($this->debug)
  82.                 echo "Query returned zero rows.";
  83.             return FALSE;
  84.         }
  85.  
  86.         //Max number of pages
  87.         $this->max_pages = ceil($this->total_rows / $this->rows_per_page );
  88.         if ($this->links_per_page > $this->max_pages) {
  89.             $this->links_per_page = $this->max_pages;
  90.         }
  91.  
  92.         //Check the page value just in case someone is trying to input an aribitrary value
  93.         if ($this->page > $this->max_pages || $this->page <= 0) {
  94.             $this->page = 1;
  95.         }
  96.  
  97.         //Calculate Offset
  98.         $this->offset = $this->rows_per_page * ($this->page - 1);
  99.  
  100.         //Fetch the required result set
  101.         $rs = @mysql_query($this->sql . " LIMIT {$this->offset}, {$this->rows_per_page}" );
  102.         if (! $rs) {
  103.             if ($this->debug)
  104.                 echo "Pagination query failed. Check your query.<br /><br />Error Returned: " . mysql_error();
  105.             return false;
  106.         }
  107.         return $rs;
  108.     }
  109.  
  110.     /**
  111.      * Display the link to the first page
  112.      *
  113.      * @access public
  114.      * @param string $tag Text string to be displayed as the link. Defaults to 'First'
  115.      * @return string
  116.      */
  117.     function renderFirst($tag = 'First') {
  118.         if ($this->total_rows == 0)
  119.             return FALSE;
  120.  
  121.         if ($this->page == 1) {
  122.             return "$tag ";
  123.         } else {
  124.             return '<a href="' . $this->php_self . '?page=1&' . $this->append . '">' . $tag . '</a> ';
  125.         }
  126.     }
  127.  
  128.     /**
  129.      * Display the link to the last page
  130.      *
  131.      * @access public
  132.      * @param string $tag Text string to be displayed as the link. Defaults to 'Last'
  133.      * @return string
  134.      */
  135.     function renderLast($tag = 'Last') {
  136.         if ($this->total_rows == 0)
  137.             return FALSE;
  138.  
  139.         if ($this->page == $this->max_pages) {
  140.             return $tag;
  141.         } else {
  142.             return ' <a href="' . $this->php_self . '?page=' . $this->max_pages . '&' . $this->append . '">' . $tag . '</a>';
  143.         }
  144.     }
  145.  
  146.     /**
  147.      * Display the next link
  148.      *
  149.      * @access public
  150.      * @param string $tag Text string to be displayed as the link. Defaults to '>>'
  151.      * @return string
  152.      */
  153.     function renderNext($tag = '&gt;&gt;') {
  154.         if ($this->total_rows == 0)
  155.             return FALSE;
  156.  
  157.         if ($this->page < $this->max_pages) {
  158.             return '<a href="' . $this->php_self . '?page=' . ($this->page + 1) . '&' . $this->append . '">' . $tag . '</a>';
  159.         } else {
  160.             return $tag;
  161.         }
  162.     }
  163.  
  164.     /**
  165.      * Display the previous link
  166.      *
  167.      * @access public
  168.      * @param string $tag Text string to be displayed as the link. Defaults to '<<'
  169.      * @return string
  170.      */
  171.     function renderPrev($tag = '&lt;&lt;') {
  172.         if ($this->total_rows == 0)
  173.             return FALSE;
  174.  
  175.         if ($this->page > 1) {
  176.             return ' <a href="' . $this->php_self . '?page=' . ($this->page - 1) . '&' . $this->append . '">' . $tag . '</a>';
  177.         } else {
  178.             return " $tag";
  179.         }
  180.     }
  181.  
  182.     /**
  183.      * Display the page links
  184.      *
  185.      * @access public
  186.      * @return string
  187.      */
  188.     function renderNav($prefix = '<span class="page_link">', $suffix = '</span>') {
  189.         if ($this->total_rows == 0)
  190.             return FALSE;
  191.  
  192.         $batch = ceil($this->page / $this->links_per_page );
  193.         $end = $batch * $this->links_per_page;
  194.         if ($end == $this->page) {
  195.             //$end = $end + $this->links_per_page - 1;
  196.         //$end = $end + ceil($this->links_per_page/2);
  197.         }
  198.         if ($end > $this->max_pages) {
  199.             $end = $this->max_pages;
  200.         }
  201.         $start = $end - $this->links_per_page + 1;
  202.         $links = '';
  203.  
  204.         for($i = $start; $i <= $end; $i ++) {
  205.             if ($i == $this->page) {
  206.                 $links .= $prefix . " $i " . $suffix;
  207.             } else {
  208.                 $links .= ' ' . $prefix . '<a href="' . $this->php_self . '?page=' . $i . '&' . $this->append . '">' . $i . '</a>' . $suffix . ' ';
  209.             }
  210.         }
  211.  
  212.         return $links;
  213.     }
  214.  
  215.     /**
  216.      * Display full pagination navigation
  217.      *
  218.      * @access public
  219.      * @return string
  220.      */
  221.     function renderFullNav() {
  222.         return $this->renderFirst() . '&nbsp;' . $this->renderPrev() . '&nbsp;' . $this->renderNav() . '&nbsp;' . $this->renderNext() . '&nbsp;' . $this->renderLast();
  223.     }
  224.  
  225.     /**
  226.      * Set debug mode
  227.      *
  228.      * @access public
  229.      * @param bool $debug Set to TRUE to enable debug messages
  230.      * @return void
  231.      */
  232.     function setDebug($debug) {
  233.         $this->debug = $debug;
  234.     }
  235. }
  236. ?>
Nov 11 '09 #1
2 2593
Markus
6,050 Recognized Expert Expert
Lines 34 and 35 should really be using the GET array - modify your search form to use method="get", and modify lines 34 and 35 accordingly.

If you look at the pagination class, it allows you to pass in some arbitrary data that will be appended to the links the pagination provides - this is where you should give it the GET data (coln, colv, etc.)
Nov 11 '09 #2
kkshansid
232 New Member
@Markus
thank you for your valuable information its working now
Nov 12 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

5
2405
by: leegold2 | last post by:
Hi, I have been looking around for a way to paginate php/mysql table output. There are a lot of hits when I google it. But unfortunately what I have tried either does not work or is imho just arcane code. So I am looking for a simple, clearly explained, and working pagination code snip that I can adapt, or a modular solution. I'd prefer something you have tried yourself and know it works. The simpler the better. Thanks very much.
2
2328
by: Chris H | last post by:
I am having a problem with pagination, basically the problem is happening in the "PREV / NUMBERS / NEXT" links, it appears as if the reason is becasue the increment and decrement operators aren't functioning or the $page variable isnt working in that part of the code... Below is the link to the working but broken page.. as well as the main part of my code... Hopefully someone can explain why the operators arent working or maybe see what i...
11
2782
by: ste | last post by:
Hi there, Further to my recent posts where I've received excellent help from Rik and Jerry, I've ended up with an image gallery on my website that displays images in a table, 3 images per row. This works great and opens all images in the database when I open the url mywebsite/gallery.php, or I can choose certain images (by category) by going to url's like mywebsite/gallery.php?category=landscape Although the above worked perfectly...
3
2134
by: assgar | last post by:
Hello I have a search page where you select the letter of the alphabet and the person's information who's name beginning with that letter is displayed. This function is placed on the web page to search and display the information. Everything works except one thing. The problem is if there are 3 pages page one is OK, but when you select next to go to page two the information on page two is no displaying.
1
3382
by: assgar | last post by:
Hi I was using a schroll bar to display multiple rows of dynamically created from database records. The scrolling was not displaying the data properly so I have decided to use pagination. The problem I am having is, if I select one item on page #1 and another on page #5 only the last item selected on page #5 is stored in the array. How can I store selections from multiple pages into one array?
1
6851
by: shalini jain | last post by:
Hi, I want to know how can we do pagination using XSL. There are number of tutorials available on pagination using PHP but nothing with XSL. i am really stuck with my code. Below is the code that i have written for pagination but it displays the link of all the pages at one go i.e. if i have 8 pages showing 10 results per page than it shows links for all 8 pages. Previous 1-10 11-20 21-30 31-40 41-50 51-60 61-70 71-80 Next i want to...
16
2778
by: gnawz | last post by:
I have a pagination function I am using in a file called functions.php as below<? //Pagination functions function getPagingQuery($sql, $itemPerPage = 10) { if (isset($_GET) && (int)$_GET > 0) { $page = (int)$_GET; } else { $page = 1; } // start fetching from this row number $offset = ($page - 1) * $itemPerPage; return $sql . " LIMIT $offset, $itemPerPage"; } /* Get the links to navigate between...
4
3575
by: ArizonaJohn | last post by:
Hello, The code below works great. The user enters a name into an HTML form, the code looks up a table with that name, and then that table is displayed. I am trying to use pagination with it, and the pagination almost works. The first page of the pagination works fine, but when I click on one of the links for one of the next pages, the page is blank. I have seen people mention this problem, and they have been told that a variable is...
15
1807
omerbutt
by: omerbutt | last post by:
hi i have made a simple pagination but it is creating problem , the problem is that if there are any products under the category that is clicked in the left menu then the page works fine but if it does not have any product listed under that category then the page halts no eror is diplayed and the page halts at the line $total = mysql_result($result, 0, 0) or die(mysql_error()); the working example can be seen here link here is the full...
0
9568
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9404
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10168
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10008
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9959
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8833
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7381
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5279
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.