Jump to content

paging using php


madhuramasamy

Recommended Posts

I am trying to have pages in my website but i am having troble with that. please can someone tell me what is the error in my code.

 

<?php

include "basic.php";

include "header.php";

include "dbname.php";

$rowsPerPage = 50;

$pageNum = 1;

if(isset($_GET['page']))

{

    $pageNum = $_GET['page'];

}

$offset = ($pageNum - 1) * $rowsPerPage;

$query="select * from Chem_Inventory LIMIT $offset, $rowsPerPage";

$result=mysql_query($query);

print "<h3><p><center>Microlab Chemical Inventory</center></p></h3><br>";       

print "<table><tr><td><b>Inventory #</td><td><b>Chemical ID</td><td><b>Chemical Name</td><td><b>Size</td><td><b>Owner</td><td><b>Sponsor</td><td><b>Date IN</td><td><b>Date Out</td></tr>";     

while ($sel = mysql_fetch_array($result)){

if ($sel[Chemical_ID] >= 1000 && $sel[Chemical_ID] < 2000){ $tbl = "Chemicals_acids";}

if ($sel[Chemical_ID] >= 2000 && $sel[Chemical_ID] < 3000){ $tbl = "Chemicals_solvents";}

if ($sel[Chemical_ID] >= 3000 && $sel[Chemical_ID] < 4000){ $tbl = "Chemicals_metals";}

if ($sel[Chemical_ID] >= 4000 && $sel[Chemical_ID] < 5000){ $tbl = "Chemicals_gases";}

if ($sel[Chemical_ID] >= 5000 && $sel[Chemical_ID] < 6000){ $tbl = "Chemicals_waste";}

$query="select Chemical_Name from $tbl where Chemical_ID = '$sel[Chemical_ID]'";

$chems1 = mysql_query($query);

$chems = mysql_fetch_array($chems1);     

print "<tr><td>$sel[iD]</td><td>$sel[Chemical_ID]</td><td>$chems[Chemical_Name]</td><td>$sel</td><td>$sel[Owner]</td><td>$sel[sponsor]</td><td>$sel[Date_In]</td><td><a href=\"http://microlab.auburn.edu/chemicals.php?ID=$sel[iD]&what=update\">$sel[Date_Out]</a></td></tr>";}

print "</table>\n";

$query   = "SELECT COUNT(ID) AS numrows FROM Chem_Inventory";

$result  = mysql_query($query);

$row     = mysql_fetch_array($result);

$numrows = $row['numrows'];

$maxPage = ceil($numrows/$rowsPerPage);

self = $_SERVER['PHP_SELF'];

$nav  = '';

 

for($page = 1; $page <= $maxPage; $page++)

{

   if ($page == $pageNum)

   {

      $nav .= " $page ";

   }

   else

   {

      $nav .= " <a href=\"$self?page=$page\">$page</a> ";

   }

}

 

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  = ' ';

   $first = ' ';

}

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 = ' ';

   $last = ' ';

}

echo $first . $prev . $nav . $next . $last;

 

?>

Link to comment
https://forums.phpfreaks.com/topic/205764-paging-using-php/
Share on other sites

i changed that but still i am getting error -Parse error: parse error, unexpected T_VARIABLE in on line 10 in the following code

 

Thanks in advance..

 

code :

 

$rowsPerPage = 50;

$pageNum = 1;

if(isset($_GET['page']))

{

    $pageNum = $_GET['page'];}

$offset = ($pageNum - 1) * $rowsPerPage;

$query="select * from Chem_Inventory LIMIT $offset, $rowsPerPage";

$result=mysql_query($query);

print "<h3><p><center>Microlab Chemical Inventory</center></p></h3><br>";       

print "<table><tr><td><b>Inventory #</td><td><b>Chemical ID</td><td><b>Chemical Name</td><td><b>Size</td><td><b>Owner</td><td><b>Sponsor</td><td><b>Date IN</td><td><b>Date Out</td></tr>";     

while ($sel = mysql_fetch_array($result)){

print "<tr><td>$sel[iD]</td><td>$sel[Chemical_ID]</td><td>$sel</td><td>$sel[Owner]</td><td>$sel[sponsor]</td><td>$sel[Date_In]</td><td>$sel[Date_Out]</td></tr>";}

print "</table>\n";

$query   = "SELECT COUNT(ID) AS numrows FROM Chem_Inventory";

$result  = mysql_query($query) or die('Error, query failed');

$row     = mysql_fetch_array($result);

$numrows = $row['numrows'];

$maxPage = ceil($numrows/$rowsPerPage);

$self = $_SERVER['PHP_SELF'];

$nav  = '';

for($page = 1; $page <= $maxPage; $page++)

{

   if ($page == $pageNum)

   {

      $nav .= " $page ";

   }

   else

   {

      $nav .= " <a href=\"$self?page=$page\">$page</a> ";

   }

}

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  = ' ';

   $first = ' ';

}

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 = ' ';

   $last = ' ';

}

echo $first . $prev . $nav . $next . $last;

?>

 

 

/code

 

 

Link to comment
https://forums.phpfreaks.com/topic/205764-paging-using-php/#findComment-1076813
Share on other sites

Sorry about that.... in this code line 10 i am getting that error

 

 


<?php
include "basic.php";
include "header.php";
include "dbname.php";
dbconnect("MICROLAB","MICROLAB","Charles1","web6.duc.auburn.edu");
$rowsPerPage = 50;
$pageNum = 1;
if(isset($_GET['page']))
{
    $pageNum = $_GET['page'];
}
$offset = ($pageNum - 1) * $rowsPerPage;
$query="select * from Chem_Inventory LIMIT $offset, $rowsPerPage";
$result=mysql_query($query);
print "<h3><p><center>Microlab Chemical Inventory</center></p></h3><br>";         
print "<table><tr><td><b>Inventory #</td><td><b>Chemical ID</td><td><b>Chemical Name</td><td><b>Size</td><td><b>Owner</td><td><b>Sponsor</td><td><b>Date IN</td><td><b>Date Out</td></tr>";      
while ($sel = mysql_fetch_array($result)){
print "<tr><td>$sel[iD]</td><td>$sel[Chemical_ID]</td><td>$sel[size]</td><td>$sel[Owner]</td><td>$sel[sponsor]</td><td>$sel[Date_In]</td><td>$sel[Date_Out]</td></tr>";}
print "</table>\n";
$query   = "SELECT COUNT(ID) AS numrows FROM Chem_Inventory";
$result  = mysql_query($query) or die('Error, query failed');
$row     = mysql_fetch_array($result);
$numrows = $row['numrows'];
$maxPage = ceil($numrows/$rowsPerPage);
$self = $_SERVER['PHP_SELF'];
$nav  = '';
for($page = 1; $page <= $maxPage; $page++)
{
   if ($page == $pageNum)
   {
      $nav .= " $page "; 
   }
   else
   {
      $nav .= " <a href=\"$self?page=$page\">$page</a> ";
   } 
}
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  = ' '; 
   $first = ' '; 
}
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 = ' '; 
   $last = ' '; 
}
echo $first . $prev . $nav . $next . $last;
?>

Link to comment
https://forums.phpfreaks.com/topic/205764-paging-using-php/#findComment-1076822
Share on other sites

this is the header file

<?php
/*
********************************************************
*** This script from MySQL/PHP Database Applications ***
***         by Jay Greenspan and Brad Bulger         ***
***                                                  ***
***   You are free to resuse the material in this    ***
***   script in any manner you see fit. There is     ***
***   no need to ask for permission or provide       ***
***   credit.                                        ***
********************************************************
*/

/*
Application: Guestbook2K
Described in: Chapter 8
Name: header.php
Purpose: This file is called at the beginning of all the pages in this
example. It includes the standard set of functions used by all our examples,
connects to a MySQL database, and defines functions used by this example.

*/

//include "../functions/charset.php";
//include "../functions/basic.php";
//dbconnect("guestbook2k");

// global variables

//$limit = 2;

// function definitions

// void print_entry (array row [, string preserve] [, string ...])
// Prints out a guestbook entry. The first argument is expected be a row 
// from a MySQL result set, but could be any valid array. The second
// argument is passed to cleanup_text() and controls the stripping-out
// of HTML tags.  The names of the fields to be printed are passed
// in as a variable number of arguments following the first two.
function print_entry($row,$preserve="")
{

// walk through any arguments passed in after the first two
$numargs = func_num_args();
for ($i = 2; $i < $numargs; $i++)
{
	$field = func_get_arg($i);

	// This will transform a label string to a valid database 
	// field name - e.g., "Last Name" becomes "last_name"
	//$dbfield = str_replace(" ", "_", strtolower($field));

	$dbvalue = cleanup_text($row[$field],$preserve);
	$name = ucwords($field);
	print " <tr>\n";
	print "  <td valign=top align=right><b>$name:</b></td>\n";
	print "  <td valign=top align=left>$dbvalue</td>\n";
	print " </tr>\n\n";
}
}

// void print_input_fields([string name [, string ...]])
// Print out a variable number of arguments, presumed to be the names
// of currently defined global variables, as HTML form input fields.
function print_input_fields()
{
$fields = func_get_args();
while (list(,$field) = each($fields))
{
	print " <tr>\n";
	print "  <td valign=top align=right><b>".ucfirst($field).":</b></td>\n";
	print "  <td valign=top align=left><input type=text name=$field size=40 value=\"".$GLOBALS[$field]."\"></td>\n";
	print " </tr>\n\n";
}
}

// string create_entry (string name, string location, string email, string url
// 	, string comments)
// Validate and insert a guestbook entry into the database. If all validation 
// tests are passed, prints out a confirmation; if not, prints out error 
// messages. Returns an empty string or relevant error messages.
function create_entry($name,$location,$email,$url,$comments)
{
// remove all HTML tags, and escape any other special characters
$name = cleanup_text($name);
$location = cleanup_text($location);
$email = cleanup_text($email);
$url = cleanup_text($url);
$comments = cleanup_text($comments);

// start out with an empty error message. as validation tests fail,
// add errors to it.
$errmsg = "";
if (empty($name))
{
	$errmsg .= "<li>you have to put in a name, at least!\n";
}

// do a very simple check on the format of the email address
// supplied by the user. an email address is required.
if (empty($email) || !eregi("^[A-Za-z0-9\_-]+@[A-Za-z0-9\_-]+.[A-Za-z0-9\_-]+.*", $email))
{
	$errmsg .= "<li>$email doesn't look like a valid email address\n";
}
else
{
	// if the format is OK, check to see if this user has already
	// signed the guestbook. multiple entries are not allowed.
	$query = "select * from guestbook where email = '$email'";
	$result = safe_query($query);
	if (mysql_num_rows($result) > 0)
	{
		$errmsg .=
		"<li>$email has already signed this guestbook.\n";
	}
}

// perform a very simple check on the format of the url supplied
// by the user (if any)
if (!empty($url) && !eregi("^http://[A-Za-z0-9\%\?\_\:\~\/\.-]+$",$url))
{
	$errmsg .= "<li>$url doesn't look like a valid URL\n";
}

if (empty($errmsg))
{
	$query = "insert into guestbook "
	." (name,location,email,url,comments,remote_addr) values "
	."('$name', '$location', '$email', '$url', '$comments','$REMOTE_ADDR')"
	;
	safe_query($query);

	print "<h2>Thanks, $name!!</h2>\n";
}
else
{
	print <<<EOQ
<p>
<font color=red>
<b>
<ul>
$errmsg
</ul>
Please try again
</p>
EOQ;
}
return $errmsg;
}

// int select_entries ([int offset])
// Select a set of entries from the guestbook. The number of entries selected
// is determined by the value of the global variable limit.  The offset 
// argument determines where to start - the default value is zero, meaning
// the first record. Entries are retrieved in descending order of the date
// they were created. Return the mysql data set identifier for the rows
// retrieved.
function select_entries ($offset=0)
{
global $limit;

if (empty($offset)) { $offset = 0; }

$query = "select *
	, date_format(created,'%e %M, %Y %h:%i %p') as entry_date
	from guestbook
	order by created desc
	limit $offset, $limit
";
$result = safe_query($query);

return $result;

}

// void nav ([int offset [, string this_script]])
// Print out navigational links for moving through entries in the guestbook.
// The first argument indicates where to start - the default value is zero,
// meaning the first (most recent) record. The second argument is the name
// of the script to use in the link - if empty, the value of the predefined
// global variable PHP_SELF will be used - this will be the name of the file 
// being displayed to the user (i.e., if bla.php includes display.php, which 
// includes bottom.php, which calls this function, PHP_SELF will be "bla.php").
function nav ($offset=0,$this_script="")
{
global $limit;
global $PHP_SELF;

if (empty($this_script)) { $this_script = $PHP_SELF; }
if (empty($offset)) { $offset = 0; }

// get the total number of entries in the guest book -
// we need this to know if we can go forward from where we are
$result = safe_query("select count(*) from guestbook");
list($total_rows) = mysql_fetch_array($result);

print "<p>\n";
if ($offset > 0) 
{ 
	// if we're not on the first record, we can always go backwards
	print "<a href=\"$this_script?offset=".($offset-$limit)."\"><<Previous Entries</a>   ";
}
if ($offset+$limit < $total_rows)
{
	// offset + limit gives us the maximum record number 
	// that we could have displayed on this page. if it's
	// less than the total number of entries, that means
	// there are more entries to see, and we can go forward
	print "<a href=\"$this_script?offset=".($offset+$limit)."\">Next Entries>></a>   ";
}
print "</p>\n";
}

?>

Link to comment
https://forums.phpfreaks.com/topic/205764-paging-using-php/#findComment-1076845
Share on other sites

this is the basic file

 

<?php
/*
********************************************************
*** This script from MySQL/PHP Database Applications ***
***         by Jay Greenspan and Brad Bulger         ***
***                                                  ***
***   You are free to resuse the material in this    ***
***   script in any manner you see fit. There is     ***
***   no need to ask for permission or provide       ***
***   credit.                                        ***
********************************************************
*/

// void authenticate ([string realm] [, string error message]])

// Send a WWW-Authenticate header, to perform HTTP authentication.
// The first argument is the text that will appear in the pop-up
// form presented to the user. The second argument is the text
// that will appear on the 401 error page if the user hits the
// 'Cancel' button in the pop-up form.

// This code only works if PHP is running as an Apache module.

function authenticate ($realm="Secure Area"
,$errmsg="Please enter a username and password"
)
{
Header("WWW-Authenticate: Basic realm=\"$realm\"");
Header("HTTP/1.0 401 Unauthorized");
die($errmsg);
}


// void db_authenticate([string table [, string realm [, string error message [, string username field name [, string password field name]]]])

// Uses HTTP authentication to get a user name and password, and then
// verifies that against a database table. The first argument is the
// name of the table to use - the default is mysql.users.  The second
// and third arguments are passed in to the authenticate() function.
// The fourth and fifth arguments are the names of the fields in the
// database table - default values are 'username' and 'password'.

// This code only works if PHP is running as an Apache module.

function db_authenticate($table="mysql.users",$realm="Secure Area"
,$errmsg="Please enter a username and password"
,$user_field=""
,$password_field=""
)
{
global $PHP_AUTH_USER;
global $PHP_AUTH_PW;

if (empty($PHP_AUTH_USER))
{
	authenticate($realm,$errmsg.": header");
}
else
{
	if (empty($user_field)) { $user_field = "username"; }
	if (empty($password_field)) { $password_field = "password"; }

	$query = "select $user_field from $table 
		where $password_field = password(lower('$PHP_AUTH_PW')) 
		and $user_field = lower('$PHP_AUTH_USER')
	";
	$result = safe_query($query);
	if ($result) { list($valid_user) = mysql_fetch_row($result); }
	if (!$result || empty($valid_user))
	{ 
		authenticate($realm,$errmsg.": query");
	}
}
#print "<h5>Editing as $PHP_AUTH_USER</h5>\n";
}

// string cleanup_text ([string value [, string preserve [, string allowed_tags]]])

// This function uses the PHP function htmlspecialchars() to convert
// special HTML characters in the first argument (&,",',<, and >) to their 
// equivalent HTML entities. If the optional second argument is empty,
// any HTML tags in the first argument will be removed. The optional
// third argument lets you specify specific tags to be spared from
// this cleansing. The format for the argument is "<tag1><tag2>".

function cleanup_text ($value = "", $preserve="", $allowed_tags="")
{
if (empty($preserve)) 
{ 
	$value = strip_tags($value, $allowed_tags);
}
$value = htmlspecialchars($value);
return $value;
}

// string get_attlist ([array attributes [,array default attributes]])

// This function will take an associative array and format as a string
// that looks like 'name1="value1" name2="value2"', as is used by HTML tags.
// Values for keys in the first argument will override values for the
// same keys in the second argument. (For example, if $atts is (color=>red)
// and $defaults is (color=>black, size=3), the resulting output will
// be 'color="red" size="3"'.)

function get_attlist ($atts="",$defaults="")
{
$localatts = array();
$attlist = "";

if (is_array($defaults)) { $localatts = $defaults; }
if (is_array($atts)) { $localatts = array_merge($localatts, $atts); }

while (list($name,$value) = each($localatts))
{
	if ($value == "") { $attlist .= "$name "; }
	else { $attlist .= "$name=\"$value\" "; }
}
return $attlist;
}

// string make_page_title ([string title])

// This function will clean up a string to make it suitable for use
// as the value of an HTML <TITLE> tag, removing any HTML tags and
// replacing any HTML entities with their literal character equivalents.

function make_page_title ($title="")
{
$title = cleanup_text($title);
$trans = array_flip(get_html_translation_table(HTML_ENTITIES));
$title = strtr($title, $trans); 
return $title;
}

// string money ([mixed value])

// This function will format the first argument as a standard US dollars
// value, rounding any decimal value two decimal places for cents 
// and prepending a dollar sign to the returned string.

function money($val=0)
{
return "$".number_format($val,2);
}

// array states(void)

// This function will return an associative array of US states,
// with the two-letter abbreviation as the key and the full name
// as the value.

function states()
{
$states['AL'] = "Alabama";
$states['AK'] = "Alaska";
$states['AS'] = "American Samoa";
$states['AZ'] = "Arizona";
$states['AR'] = "Arkansas";
$states['CA'] = "California";
$states['CO'] = "Colorado";
$states['CT'] = "Connecticut";
$states['DE'] = "Delaware";
$states['DC'] = "District Of Columbia";
$states['FM'] = "Federated States Of Micronesia";
$states['FL'] = "Florida";
$states['GA'] = "Georgia";
$states['GU'] = "Guam";
$states['HI'] = "Hawaii";
$states['ID'] = "Idaho";
$states['IL'] = "Illinois";
$states['IN'] = "Indiana";
$states['IA'] = "Iowa";
$states['KS'] = "Kansas";
$states['KY'] = "Kentucky";
$states['LA'] = "Louisiana";
$states['ME'] = "Maine";
$states['MH'] = "Marshall Islands";
$states['MD'] = "Maryland";
$states['MA'] = "Massachusetts";
$states['MI'] = "Michigan";
$states['MN'] = "Minnesota";
$states['MS'] = "Mississippi";
$states['MO'] = "Missouri";
$states['MT'] = "Montana";
$states['NE'] = "Nebraska";
$states['NV'] = "Nevada";
$states['NH'] = "New Hampshire";
$states['NJ'] = "New Jersey";
$states['NM'] = "New Mexico";
$states['NY'] = "New York";
$states['NC'] = "North Carolina";
$states['ND'] = "North Dakota";
$states['MP'] = "Northern Mariana Islands";
$states['OH'] = "Ohio";
$states['OK'] = "Oklahoma";
$states['OR'] = "Oregon";
$states['PW'] = "Palau";
$states['PA'] = "Pennsylvania";
$states['PR'] = "Puerto Rico";
$states['RI'] = "Rhode Island";
$states['SC'] = "South Carolina";
$states['SD'] = "South Dakota";
$states['TN'] = "Tennessee";
$states['TX'] = "Texas";
$states['UT'] = "Utah";
$states['VT'] = "Vermont";
$states['VI'] = "Virgin Islands";
$states['VA'] = "Virginia";
$states['WA'] = "Washington";
$states['WV'] = "West Virginia";
$states['WI'] = "Wisconsin";
$states['WY'] = "Wyoming";
$states['AE'] = "Armed Forces Africa";
$states['AA'] = "Armed Forces Americas";
$states['AE'] = "Armed Forces Canada";
$states['AE'] = "Armed Forces Europe";
$states['AE'] = "Armed Forces Middle East";
$states['AP'] = "Armed Forces Pacific";

return $states;
}

// string get_local_ref ([string ref])

// This function will transform a local reference (such as "edit.php")
// to a local reference that begins with the root of the current
// script as defined by the Apache variable SCRIPT_NAME (such as
// "/book/guestbook/view.php"). It is used by the secure_url()
// and regular_url() functions to create an absolute URL out of
// a local reference.

// This behavior of this function if run under a server other than Apache
// is not known. It's likely to work, though, as SCRIPT_NAME is part of
// the CGI 1.1 specification.

function get_local_ref($ref="")
{
global $SCRIPT_NAME;

if (substr($ref,0,1) != "/")
{
	$ref = substr($SCRIPT_NAME,0,strrpos($SCRIPT_NAME,"/")+1).$ref;
}
return $ref;
}

// string secure_url ([string ref])

// This function will transform a local URL into an absolute URL pointing
// to a secure server running on the same domain, as defined by the global
// Apache variable HTTP_HOST. (Note: the server we are using runs on 
// non-standard ports, thus the need to change the port numbers.)

function secure_url($ref="")
{
global $HTTP_HOST;

$url = "https://".$HTTP_HOST.get_local_ref($ref);
$url = str_replace("8080","444",$url);
return $url;
}

// string regular_url ([string ref])

// This function will transform a local URL into an absolute URL pointing
// to a normal server running on the same domain, as defined by the global
// Apache variable HTTP_HOST. (Note: the server we are using runs on 
// non-standard ports, thus the need to change the port numbers.)

function regular_url($ref="")
{
global $HTTP_HOST;

$url = "http://".$HTTP_HOST.get_local_ref($ref);
$url = str_replace("444","8080",$url);
return $url;
}

include("db.php");
include("html.php");
include("forms.php");
include("tables.php");

?>

Link to comment
https://forums.phpfreaks.com/topic/205764-paging-using-php/#findComment-1076848
Share on other sites

OK I think this *SHOULD* work...

<?php
include "basic.php";
include "header.php";
include "dbname.php";
dbconnect("MICROLAB","MICROLAB","Charles1","web6.duc.auburn.edu");
$rowsPerPage = 50;
$pageNum = isset($_GET['page']) ? (int)$_GET['page'] : 1;

$offset = ($pageNum - 1) * $rowsPerPage;
$query="select * from Chem_Inventory LIMIT $offset, $rowsPerPage";
$result=mysql_query($query);
print "<h3><p><center>Microlab Chemical Inventory</center></p></h3><br>";        
print "<table><tr><td><b>Inventory #</td><td><b>Chemical ID</td><td><b>Chemical Name</td><td><b>Size</td><td><b>Owner</td><td><b>Sponsor</td><td><b>Date IN</td><td><b>Date Out</td></tr>";      

while ($sel = mysql_fetch_array($result)){
   print "<tr><td>{$sel['ID']}</td><td>{$sel['Chemical_ID']}</td><td>{$sel['Size']}</td><td>{$sel['Owner']}</td><td>{$sel['Sponsor']}</td><td>{$sel['Date_In']}</td><td>{$sel['Date_Out']}</td></tr>";
}
print "</table>\n";
$query   = "SELECT COUNT(ID) AS numrows FROM Chem_Inventory";
$result  = mysql_query($query) or die('Error, query failed');
$row     = mysql_fetch_array($result);
$numrows = $row['numrows'];
$maxPage = ceil($numrows/$rowsPerPage);
$self = $_SERVER['PHP_SELF'];
$nav  = '';
for($page = 1; $page <= $maxPage; $page++)
{
   if ($page == $pageNum)
   {
      $nav .= " $page ";
   }
   else
   {
      $nav .= " <a href=\"$self?page=$page\">$page</a> ";
   }
}
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  = ' ';
   $first = ' ';
}
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 = ' ';
   $last = ' ';
}
echo $first . $prev . $nav . $next . $last;
?>

Link to comment
https://forums.phpfreaks.com/topic/205764-paging-using-php/#findComment-1076854
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.