473,657 Members | 2,490 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

$_GET['offset'] - Page number links

89 New Member
Hello

I have a problem which i hope someone can help me with. I have a website where customers can login and their current and previous orders.

What i need now is for the customers to look through their orders. I kind of already have a script for this:

Expand|Select|Wrap|Line Numbers
  1. $recordcount = mssql_num_rows($result);
  2. $pagecount = $recordcount / $displayPerPage;
  3. $r = fmod($recordcount, $displayPerPage);
  4. if ($r > 0) {
  5.     $pagecount = floor($pagecount) + 1;
  6.     }
  7. if ($pagecount < 1) { $pagecount = 1; }
  8. if (mssql_num_rows($result) > 0) {
  9.         if (isset($_GET['offset']) && $_GET['offset'] != 0 ) {
  10.         mssql_data_seek($result, $_GET['offset']);
  11.         $rowsLeft -= $_GET['offset'];
  12.         $currentPage = ($recordcount - $rowsLeft) / $displayPerPage;
  13.         $r = fmod(($recordcount - $rowsLeft), $displayPerPage);
  14.         if ($r > 0) {
  15.             $currentPage = floor($currentPage) + 1;
  16.             }
  17.  
  18.         }
I also have a script that shows the customer how many orders that were found and on how many pages the orders are shown at. ex. 67 orders found, page 1 of 7.

[HTML]<?php print $recordcount ?> orders found, page <?php print $currentPage ?> of <?php print $pagecount ?>[/HTML]

I also have a next and previous button script:

[HTML]<?php
if (isset($_GET['offset']) && $_GET['offset'] != 0 ) {
echo "<a href=\"?offset= " . ($_GET['offset'] - $displayPerPage ) . "\">" . "Previous</a>";
}
?>

<?php
if ($rowsLeft > 0) {
echo "<a href=\"?offset= " . (@$_GET['offset'] + $displayPerPage ) . "\">" . "Next</a>";
}
?>[/HTML]

What i need is numbers with links in between the previous and next buttons that the customer can click to go that exact page of the order view. Ex.

1 2 3 4 5 6 7 Next

Can anyone help me making these number links.......... ..?

Thanks in advance...
Aug 10 '07 #1
8 5670
Atli
5,058 Recognized Expert Expert
You mean something like this?

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.     // Get the number of pages
  3.     $pageCount = 10;
  4.  
  5.     // Get the current page
  6.     $currentPage = @$_GET['currentPage'];
  7.     if(!@isset($_GET['currentPage'])) {
  8.         $currentPage = 0;
  9.     }
  10.     else if($currentPage >= $pageCount) {
  11.         $currentPage = $pageCount - 1;
  12.     }
  13.  
  14.     // Print the Prev link if needed
  15.     if($currentPage > 0) {
  16.         echo '<a href="?currentPage='. ($currentPage - 1) .'">Prev</a> ';
  17.     }
  18.  
  19.     // Print the number list
  20.     for($i = 0; $i < $pageCount; $i++) {
  21.         echo '<a href="?currentPage='. $i .'">'. ($i + 1) .'</a> ';
  22.     }
  23.  
  24.     // Print the Next link if needed
  25.     if($currentPage < $pageCount - 1) {
  26.         echo '<a href="?currentPage='. ($currentPage  + 1) .'">Next</a>';
  27.     }
  28. ?>
Aug 10 '07 #2
anonymous
98 New Member
i done this before and i got it working, i was proud.

heres the formula -

on index page, you count(*) the quatity from database and then divide by how many display per page then so if quatity = 50 and display per page is 10

there would be 5 pages -> previous [1] 2 3 4 5 next

each links would be echo by

Expand|Select|Wrap|Line Numbers
  1. $pages = 50/10;  //which is 5
  2. $i = 0;
  3. while($i <= $pages) { echo "<a href="./index.php?page=$i">"; i++ }
and then when the previous and next links are click, it is retrive as below
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $db_select_start = $_REQUEST[page]*10;
  3.  
  4. // run database query
  5. select * from data where id = $db_select_start;
  6.  
  7. while($row) 
  8. { etc.. you know this and then echo below}
  9. ?>
notice all codes are not sure to be correct, im just showing my formula, but it may not be correct after all because i did not make sure.
Aug 11 '07 #3
printline
89 New Member
To Atli

I tried putting in your code and it printed out the page numbers. But regardless which number i click on it only takes me one page forward (seems that the number links react like the next button).

Also if i have more than 10 pages it gives me a problem. Solved this by putting in this code:

[PHP]<?php
// Get the number of pages
$pageCount = $recordcount / $displayPerPage ;
// Print the number list
for($i = 0; $i < $pageCount; $i++) {
echo "<a href=\"?offset= " . (@$_GET['offset'] + $displayPerPage ) . "\">". ($i + 1) .'</a> ';
}

?>[/PHP]

But then i get all the page numbers displayed. I would like there to be only 10 numbers visible per page. 1-10 on first page and if i hit the next button once, it should be 2-11 and so on.....

Can you help with that.......?
Aug 16 '07 #4
Atli
5,058 Recognized Expert Expert
To Atli

I tried putting in your code and it printed out the page numbers. But regardless which number i click on it only takes me one page forward (seems that the number links react like the next button).
Yea sorry, a minor syntax error in my code at line 21. It should look like this.
- I've changed this in my original post, btw
Expand|Select|Wrap|Line Numbers
  1. echo '<a href="?currentPage='. $i. '">'. ($i + 1) .'</a> ';
  2.  
Also if i have more than 10 pages it gives me a problem. Solved this by putting in this code:

[PHP]<?php
// Get the number of pages
$pageCount = $recordcount / $displayPerPage ;
// Print the number list
for($i = 0; $i < $pageCount; $i++) {
echo "<a href=\"?offset= " . (@$_GET['offset'] + $displayPerPage ) . "\">". ($i + 1) .'</a> ';
}

?>[/PHP]

But then i get all the page numbers displayed. I would like there to be only 10 numbers visible per page. 1-10 on first page and if i hit the next button once, it should be 2-11 and so on.....

Can you help with that.......?
What you need to do is calculate the first and the last pages that you want to display, based on the current page.
Also, note that pages 1 through 10 are not 10 numbers, they are only 9. Four on each side of the current page.

So, that could be done like this:
Expand|Select|Wrap|Line Numbers
  1. // Get the first and last index shown
  2. $startIndex = 0;
  3. $endIndex = $pageCount - 1;
  4. if($pageCount > 10) {
  5.   // Calculate start and end index
  6.   $startIndex = $currentPage - 4;
  7.   $endIndex = $currentPage + 4;
  8.  
  9.   // Check if values are out of bounds and adjust
  10.   if ($startIndex < 0) {
  11.     $endIndex -= $startIndex;
  12.     $startIndex = 0;
  13.   }
  14.   if($endIndex > $pageCount - 1) {
  15.      $startIndex += ($pageCount - $endIndex);
  16.      $endIndex = $pageCount - 1;
  17.   }
  18. }
  19.  
And then you will have to modify your for loop to use those values. Like so:
Expand|Select|Wrap|Line Numbers
  1. for($i = $startIndex; $i <= $endIndext; $i++) {
  2.  
Aug 17 '07 #5
sharathy
2 New Member
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $que   = "select COUNT(clm_topicId) AS numrows from tbl_blogtopics";
  3. $resu  = mysql_query($que) or die('Error, query failed');
  4. $ro     = mysql_fetch_array($resu, MYSQL_ASSOC);
  5. $numrows = $ro['numrows'];
  6. $maxPage = ceil($numrows/$rowsPerPage);
  7. $self = $_SERVER['PHP_SELF'];
  8. if ($pageNum > 1)
  9. {
  10.     $page = $pageNum - 1;
  11.     $prev = " <a href=\"$self?page=$page\">[Prev]</a> ";
  12.  
  13.     $first = " <a href=\"$self?page=1\">[First Page]</a> ";
  14. }
  15. else
  16. {
  17.     $prev  = ' [Prev] ';       
  18.     $first = ' [First Page] '; 
  19.     }
  20. if ($pageNum < $maxPage)
  21. {
  22.     $page = $pageNum + 1;
  23.     $next = " <a href=\"$self?page=$page\">[Next]</a> ";
  24.  
  25.     $last = " <a href=\"$self?page=$maxPage\">[Last Page]</a> ";
  26. }
  27. else
  28. {
  29.     $next = ' [Next] ';     
  30.     $last = ' [Last Page] '; 
  31.     }
  32.     echo "<br>";
  33. echo $first.$prev."<strong style='color:#FFFFFF'>Showing&nbsp;page&nbsp;$pageNum&nbsp;of&nbsp;$maxPage&nbsp;pages&nbsp;</strong>".$next.$last;
  34. }
  35.  
  36.  

try with this
sharathy
Aug 17 '07 #6
printline
89 New Member
To Atli

I am nearly there. I can see in my url that something happens. Fx. my first page looks like this in the url: http://portal.printlin e.dk/search.php and then if i hit the next button or the page number 2 it says: http://portal.printlin e.dk/search.php?curr entPage=1
If i hit the number 4 link for example it says: http://portal.printlin e.dk/search.php?curr entPage=3

The problem is that it is still the same page that is showing (same 10 orders). It is only the url that changes.

Here is all the code i am using:

[PHP]<?php
// Now write the data lines
$recordcount = mssql_num_rows( $result);
$pagecount = $recordcount / $displayPerPage ;
$r = fmod($recordcou nt, $displayPerPage );
if ($r > 0) {
$pagecount = floor($pagecoun t) + 1;
}
if ($pagecount < 1) { $pagecount = 1; }
if (mssql_num_rows ($result) > 0) {
if (isset($_GET['offset']) && $_GET['offset'] != 0 ) {
mssql_data_seek ($result, $_GET['offset']);
$rowsLeft -= $_GET['offset'];
$currentPage = ($recordcount - $rowsLeft) / $displayPerPage ;
$r = fmod(($recordco unt - $rowsLeft), $displayPerPage );
if ($r > 0) {
$currentPage = floor($currentP age) + 1;
}

}
$i = 0;
if ($currentPage < 1) { $currentPage = 1; }

while ($jtsstatus = mssql_fetch_arr ay($result)) {
if ($i >= $displayPerPage ) {
break;
}
$i += 1;
// Get the first and last index shown
$startIndex = 0;
$endIndex = $pageCount - 1;
if($pageCount > 10) {
// Calculate start and end index
$startIndex = $currentPage - 4;
$endIndex = $currentPage + 4;

// Check if values are out of bounds and adjust
if ($startIndex < 0) {
$endIndex -= $startIndex;
$startIndex = 0;
}
if($endIndex > $pageCount - 1) {
$startIndex += ($pageCount - $endIndex);
$endIndex = $pageCount - 1;
}
}

?>[/PHP]

And

[PHP]<?php
// Get the number of pages
$pageCount = 10;

// Get the current page
$currentPage = @$_GET['currentPage'];
if(!@isset($_GE T['currentPage'])) {
$currentPage = 0;
}
else if($currentPage >= $pageCount) {
$currentPage = $pageCount - 1;
}

// Print the Prev link if needed
if($currentPage > 0) {
echo '<a href="?currentP age='. ($currentPage - 1) .'">Prev</a> ';
}

// Print the number list
for($i = 0; $i < $pageCount; $i++) {
echo '<a href="?currentP age='. $i .'">'. ($i + 1) .'</a> ';
}

// Print the Next link if needed
if($currentPage < $pageCount - 1) {
echo '<a href="?currentP age='. ($currentPage + 1) .'">Next</a>';
}
?>[/PHP]

Can you see what is wrong......???? ??????
Aug 17 '07 #7
Atli
5,058 Recognized Expert Expert
Yea, the second code snippet is still using the preset $pageCount that I used in my example (@ line 3), which is ten. You need to change this into the $pagecount that you calculated in your first snippet.

Ps. You can replace " = floor($value) + 1" with " = ceil($value)" and it will give you the same result.
Aug 17 '07 #8
printline
89 New Member
I changed what you suggested, but then i get all the page number links. For example if i have 65 pages with orders it shows me the numbers from 1 - 65 and not as intended only 1 - 10.....?
Aug 20 '07 #9

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

Similar topics

2
3408
by: carramba | last post by:
Hi! Tahnx for taking time and reading! This script should load default page and default stylesheet, but its only loads default page, you have to actualy click on the style link to load style... but I dont understand why! It started when I "compresed" script and addet $_GET in the same "if" set... but it shoudent by eny problem...
6
5439
by: user | last post by:
when i first load index.php with arguments ie: "index.php?page=x", the $_SERVERvalue is null. I then get the session id appended to each link. If i refresh the page, I get the $_SERVER I want, but if i click a link $_SERVER returns the value I want and the session id. Any ideas as to how I can get the $_SERVER to return the arguments on first page load?
1
4189
by: Michael Brennan-White | last post by:
If I submit my for using a get action the resulting page loads . If I use a post action I get an error page saying "The page cannot be found". I am calling the originating page!!! This happens in IE as well as FireFox. This code has been tested on a Win2003 server, IIS6, PHP 5.0.3, mySQL 4.1.8 and it works fine. The problem server is a Win2k server, IIS5, PHP 5.0.4, mySQL 4.1.11.
6
1951
by: ste | last post by:
Hi there, I'm just beginning to learn PHP and MySQL, but I'm finding it difficult! I wondered if someone could help me out with a problem I'm having, or at least point me in the right direction? I have setup a MySQL database which contains the following, though I would like to expand on this in the future with lots of extra fields: IMAGES TABLE: -------------------
17
2973
by: csubillr | last post by:
I have a web page that is currently using 2 frames. The left frame has buttons to select what appears in the right frame. I need to stop using frames, and was told PHP would do that. I have done some studying at w3schools.com and I think something like this would work, but I am missing something major. <?php if ($_GET == "home") include("home.php"); else if ($_GET == "info") include("info.php"); ?>
9
1555
by: paitoon | last post by:
In my search engine i set it to show result 30 result per one page.everything work fine but when i click on next page to see the result...there ae only blank i got.....what should i do ? i wrote some script that i use here also. ok the main script is here; <? //////////THis is the script that i put above the head doccument///////// $page=$_GET; if ($page=="") { $page=1;
4
2136
osward
by: osward | last post by:
I had made a table colum sortable and paging the table, following are the code // Display Event List echo "<center>"._EVENTLIST."</center><br>"; $now = Date(Y-m-d); // sort table colum if(!isset($_GET)){ $page = 1; } else {
3
1756
by: gussi | last post by:
when i used <?php @header("Location:google.co.in"); ?> .. the page is not redirecting to that page,, if i remove @ sign then it is showing worning session already started(...)in...
4
1695
by: DigitalDrew | last post by:
Just wanted to say hello to all the other newbies, and ask my first question: Your help is extremely needed, and I would appreciate anyone who can help. I am trying to make a website with SSI (server side includes), so my site doesn't take years to load. Here is my html and <?php ?> function: <body> <div id="container">
0
8385
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
8303
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,...
1
8502
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,...
1
6162
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
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4150
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...
0
4300
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1941
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.