Current location: Hot Scripts Forums » Programming Languages » PHP » Help with array in paging code


Help with array in paging code

Reply
  #1 (permalink)  
Old 11-13-09, 03:13 AM
triplebig triplebig is offline
Newbie Coder
 
Join Date: Nov 2007
Posts: 81
Thanks: 6
Thanked 0 Times in 0 Posts
Question Help with array in paging code

Ok i have this simple paging code that im currently testing and modify

this code will show 3 records per page in a table

this table have 4 columns

ID
FirstName
LastName
Age

The paging is working fine.....

the problem i'm having is in 2nd page the ID column is back to 1....and not 4

i want the id number in the id column to continue from previous page and to next page

page 1: ID: 1 2 3
page 2: ID: 4 5 6
and so on

here's my code

p/s: sorry for my english


PHP Code:


<!doctype html public "-//w3c//dtd html 3.2//en">

<html>

<head>
<title>Paging</title>
</head>

<body bgcolor="#ffffff" text="#000000" link="#0000ff" vlink="#800080" alink="#ff0000">
<?
require "connect.php";           // All database details will be included here 

$page_name="paging.php"//  If you use this code with a different page ( or file ) name then change this 
$start=$_GET['start'];
if(
strlen($start) > and !is_numeric($start)){
echo 
"Data Error";
exit;
}


$eu = ($start 0); 
$limit 3;                                 // No of records to be shown per page.
$this1 $eu $limit
$back $eu $limit
$next $eu $limit


/////////////// WE have to find out the number of records in our table. We will use this to break the pages///////
$query2=" SELECT * FROM person  ";
$result2=mysql_query($query2);
echo 
mysql_error();
$nume=mysql_num_rows($result2);
/////// The variable nume above will store the total number of records in the table////

/////////// Now let us print the table headers ////////////////
$bgcolor="#f1f1f1";
echo 
"<TABLE width=50% align=center  cellpadding=0 cellspacing=0> <tr>";
echo 
"<td  bgcolor='dfdfdf' >&nbsp;<font face='arial,verdana,helvetica' color='#000000' size='4'>ID</font></td>";
echo 
"<td  bgcolor='dfdfdf' >&nbsp;<font face='arial,verdana,helvetica' color='#000000' size='4'>FirstName</font></td>";
echo 
"<td  bgcolor='dfdfdf' >&nbsp;<font face='arial,verdana,helvetica' color='#000000' size='4'>LastName</font></td>";
echo 
"<td  bgcolor='dfdfdf'>&nbsp;<font face='arial,verdana,helvetica' color='#000000' size='4'>Age</font></td></tr>";

////////////// Now let us start executing the query with variables $eu and $limit  set at the top of the page///////////
$query=" SELECT * FROM person limit $eu$limit ";
$result=mysql_query($query);
echo 
mysql_error();
$count=1;
//////////////// Now we will display the returned records in side the rows of the table/////////
while($noticia mysql_fetch_array($result))
{
if(
$bgcolor=='#f1f1f1'){$bgcolor='#ffffff';}
else{
$bgcolor='#f1f1f1';}

echo 
"<tr >";
echo 
"<td align=left bgcolor=$bgcolor id='title'>&nbsp;<font face='Verdana' size='2'>";
echo 
$count++;
echo 
"</font></td>"
echo 
"<td align=left bgcolor=$bgcolor id='title'>&nbsp;<font face='Verdana' size='2'>$noticia[firstname]</font></td>"
echo 
"<td align=left bgcolor=$bgcolor id='title'>&nbsp;<font face='Verdana' size='2'>$noticia[lastname]</font></td>"
echo 
"<td align=left bgcolor=$bgcolor id='title'>&nbsp;<font face='Verdana' size='2'>$noticia[age]</font></td>"

echo 
"</tr>";
}
echo 
"</table>";
////////////////////////////// End of displaying the table with records ////////////////////////

/////////////////////////////// 
if($nume $limit ){ // Let us display bottom links if sufficient records are there for paging

/////////////// Start the bottom links with Prev and next link with page numbers /////////////////
echo "<table align = 'center' width='50%'><tr><td  align='left' width='30%'>";
//// if our variable $back is equal to 0 or more then only we will display the link to move back ////////
if($back >=0) { 
print 
"<a href='$page_name?start=$back'><font face='Verdana' size='2'>PREV</font></a>"

//////////////// Let us display the page links at  center. We will not display the current page as a link ///////////
echo "</td><td align=center width='30%'>";
$i=0;
$l=1;
for(
$i=0;$i $nume;$i=$i+$limit){
if(
$i <> $eu){
echo 
" <a href='$page_name?start=$i'><font face='Verdana' size='2'>$l</font></a> ";
}
else { echo 
"<font face='Verdana' size='4' color=red>$l</font>";}        /// Current page is not displayed as link and given font color red
$l=$l+1;
}


echo 
"</td><td  align='right' width='30%'>";
///////////// If we are not in the last page then Next link will be displayed. Here we check that /////
if($this1 $nume) { 
print 
"<a href='$page_name?start=$next'><font face='Verdana' size='2'>NEXT</font></a>";} 
echo 
"</td></tr></table>";

}
// end of if checking sufficient records are there to display bottom navigational link. 

include "menu.php";
?>
</body>

</html>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 11-13-09, 09:47 PM
Jcbones Jcbones is offline
Aspiring Coder
 
Join Date: Mar 2009
Location: North Carolina, USA
Posts: 516
Thanks: 5
Thanked 47 Times in 44 Posts
Try this, it is UN-TESTED so let me know if it doesn't work.
PHP Code:



<!doctype html public "-//w3c//dtd html 3.2//en">

<html>

<head>
<title>Paging</title>
</head>

<body bgcolor="#ffffff" text="#000000" link="#0000ff" vlink="#800080" alink="#ff0000">
<?
require "connect.php";           // All database details will be included here 

$page_name="paging.php"//  If you use this code with a different page ( or file ) name then change this 
$start=$_GET['start'];
if(
strlen($start) > and !is_numeric($start)){
echo 
"Data Error";
exit;
}


$eu = ($start 0); 
$limit 3;                                 // No of records to be shown per page.
$this1 $eu $limit
$back $eu $limit
$next $eu $limit


/////////////// WE have to find out the number of records in our table. We will use this to break the pages///////
$query2=" SELECT * FROM person  ";
$result2=mysql_query($query2);
echo 
mysql_error();
$nume=mysql_num_rows($result2);
/////// The variable nume above will store the total number of records in the table////

/////////// Now let us print the table headers ////////////////
$bgcolor="#f1f1f1";
echo 
"<TABLE width=50% align=center  cellpadding=0 cellspacing=0> <tr>";
echo 
"<td  bgcolor='dfdfdf' >&nbsp;<font face='arial,verdana,helvetica' color='#000000' size='4'>ID</font></td>";
echo 
"<td  bgcolor='dfdfdf' >&nbsp;<font face='arial,verdana,helvetica' color='#000000' size='4'>FirstName</font></td>";
echo 
"<td  bgcolor='dfdfdf' >&nbsp;<font face='arial,verdana,helvetica' color='#000000' size='4'>LastName</font></td>";
echo 
"<td  bgcolor='dfdfdf'>&nbsp;<font face='arial,verdana,helvetica' color='#000000' size='4'>Age</font></td></tr>";

////////////// Now let us start executing the query with variables $eu and $limit  set at the top of the page///////////
$query=" SELECT * FROM person limit $eu$limit ";
$result=mysql_query($query);
echo 
mysql_error();
$count = (isset($eu) && $eu 0) ? $eu 1;
//////////////// Now we will display the returned records in side the rows of the table/////////
while($noticia mysql_fetch_array($result))
{
if(
$bgcolor=='#f1f1f1'){$bgcolor='#ffffff';}
else{
$bgcolor='#f1f1f1';}

echo 
"<tr >";
echo 
"<td align=left bgcolor=$bgcolor id='title'>&nbsp;<font face='Verdana' size='2'>";
echo 
$count++;
echo 
"</font></td>"
echo 
"<td align=left bgcolor=$bgcolor id='title'>&nbsp;<font face='Verdana' size='2'>$noticia[firstname]</font></td>"
echo 
"<td align=left bgcolor=$bgcolor id='title'>&nbsp;<font face='Verdana' size='2'>$noticia[lastname]</font></td>"
echo 
"<td align=left bgcolor=$bgcolor id='title'>&nbsp;<font face='Verdana' size='2'>$noticia[age]</font></td>"

echo 
"</tr>";
}
echo 
"</table>";
////////////////////////////// End of displaying the table with records ////////////////////////

/////////////////////////////// 
if($nume $limit ){ // Let us display bottom links if sufficient records are there for paging

/////////////// Start the bottom links with Prev and next link with page numbers /////////////////
echo "<table align = 'center' width='50%'><tr><td  align='left' width='30%'>";
//// if our variable $back is equal to 0 or more then only we will display the link to move back ////////
if($back >=0) { 
print 
"<a href='$page_name?start=$back'><font face='Verdana' size='2'>PREV</font></a>"

//////////////// Let us display the page links at  center. We will not display the current page as a link ///////////
echo "</td><td align=center width='30%'>";
$i=0;
$l=1;
for(
$i=0;$i $nume;$i=$i+$limit){
if(
$i <> $eu){
echo 
" <a href='$page_name?start=$i'><font face='Verdana' size='2'>$l</font></a> ";
}
else { echo 
"<font face='Verdana' size='4' color=red>$l</font>";}        /// Current page is not displayed as link and given font color red
$l=$l+1;
}


echo 
"</td><td  align='right' width='30%'>";
///////////// If we are not in the last page then Next link will be displayed. Here we check that /////
if($this1 $nume) { 
print 
"<a href='$page_name?start=$next'><font face='Verdana' size='2'>NEXT</font></a>";} 
echo 
"</td></tr></table>";

}
// end of if checking sufficient records are there to display bottom navigational link. 

include "menu.php";
?>
</body>

</html>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
The Following User Says Thank You to Jcbones For This Useful Post:
triplebig (11-19-09)
  #3 (permalink)  
Old 11-14-09, 12:26 AM
triplebig triplebig is offline
Newbie Coder
 
Join Date: Nov 2007
Posts: 81
Thanks: 6
Thanked 0 Times in 0 Posts
Thank you JcBones :-)

yes its works

but the second page starts with id 3 instead of 4

its seem count start from zero

what should i really change below ..newbie here

PHP Code:

$count = (isset($eu) && $eu 0) ? $eu 1

Last edited by triplebig; 11-14-09 at 12:34 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 11-16-09, 06:53 PM
Jcbones Jcbones is offline
Aspiring Coder
 
Join Date: Mar 2009
Location: North Carolina, USA
Posts: 516
Thanks: 5
Thanked 47 Times in 44 Posts
PHP Code:

//if $eu is set, and the value is more than 0, $count = $eu + 1, else $eu = 1;
$count = (isset($eu) && $eu 0) ? $eu+1
I thought that might be the case, since we are calling the count to start off of your first limit call, and the first number in the limit will always be one number behind. Since MySQL count start with 0.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
The Following User Says Thank You to Jcbones For This Useful Post:
triplebig (11-19-09)
  #5 (permalink)  
Old 11-19-09, 08:08 PM
triplebig triplebig is offline
Newbie Coder
 
Join Date: Nov 2007
Posts: 81
Thanks: 6
Thanked 0 Times in 0 Posts
thank you...

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
when error moving code in array zodehala PHP 2 07-12-09 01:27 AM
Need captcha for html+php contact form sujata_ghosh PHP 6 03-22-09 04:46 PM
How: Create an array with variable keys ginasuit PHP 2 02-20-09 09:14 PM
How to select a transaction of an array and check it on regular basis Oskare100 PHP 0 12-28-06 06:27 PM
How to sale php code to customer without giving him code pradeep_soft PHP 4 03-12-04 01:10 PM


All times are GMT -5. The time now is 02:30 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.