Board index   FAQ   Search  
Register  Login
Board index php forum :: Database mySQL & php coding

Go to next and previous record

Codes here !

Moderators: macek, egami, gesf

Go to next and previous record

Postby wmdrumaizi » Tue May 06, 2003 2:52 am

Hi guys,

I'm doing php search page. I got the result page and it worked fine. I have set 10 record to be previewed on a page. Could someone tell me how to navigate to next and previous recordset i.e. record 1 to 10, 11 to 20 and so on
This is my code:
Code: Select all
$resultID = mysql_query("SELECT nama, id, org, phone, email FROM maintable WHERE nama LIKE '%$nama%' AND org LIKE '%$org%' AND keyword LIKE '%$keyword%' AND bcc LIKE '%$bcc%' ORDER by nama ASC LIMIT 10", $linkID);


Thanks in advance
User avatar
wmdrumaizi
New php-forum User
New php-forum User
 
Posts: 40
Joined: Thu Jan 23, 2003 5:15 pm
Location: Malaysia

Postby mike » Tue May 06, 2003 5:13 am

limit has 2 expressions: limit num1,num2

num1 = starting record
num2 = records after num1

eg:
Code: Select all
$resultID = mysql_query("SELECT * FROM maintable WHERE nama LIKE '%$nama%' ORDER by nama ASC LIMIT 11,10", $linkID);


will show all records from 11 to 21 (11 + 10). So if you want to show your records by 10 on each page you have to do the code like that:

Code: Select all
$resultID = mysql_query("SELECT * FROM maintable WHERE nama LIKE '%$nama%' ORDER by nama ASC LIMIT $start,9", $linkID);


where $start should be 1....11....21....31 (if I remember right) etc. But remember before you do that you should first check how many records you have so you can calculate number of links leading to pages with the next ten records.

eg: if you have 34 records you want 4 pages presenting 10 .. 10 .. 10 and 4 records respectively.
User avatar
mike
New php-forum User
New php-forum User
 
Posts: 73
Joined: Sun May 04, 2003 4:26 am
Location: Athens

Hi

Postby gesf » Mon Jun 09, 2003 9:14 pm

Maybe this helps...
here goes:

Code: Select all
<?php

$result = mysql_query("SELECT * FROM table");

$show = 10; // Define the number of showed pages
$total = mysql_num_rows($result); // return total rows
$pages = ceil($total / $show); // return total pages

if(!isset($page)) { $page = 0; } /* if there´s no action for a page ($page = 0) then show the fisrt page*/

$begin = $page * $show;
$result = mysql_query("SELECT * FROM table LIMIT $begin, $show");

// -- your news script, guestbook, whatever --------

while($sql = mysql_fetch_array($result)) {
  echo " Put your code here! ";
}

//---- link generator ---------

if($page> 0) {
  $less = $page - 1;
  $url = "$PHP_SELF?page=$less";
  echo "<a href="$url">previous</a>"; /* link to the previous page*/
}
for($i=0;$i<$pages;$i++) { /* loop generator with link to the pages*/
  $url = "$PHP_SELF?page=$i";
  echo " | <a href="$url">$i</a>";
}
if($page< $pages) {
  $more = $page + 1;
  $url = "$PHP_SELF?page=$more";
  echo " | <a href="$url">Next</a>"; /* link to the next page*/
}

?>


Sorry for my english :p
User avatar
gesf
Moderator
Moderator
 
Posts: 1718
Joined: Sun Dec 29, 2002 5:03 am
Location: Portugal / Sweden

Postby wmdrumaizi » Thu Jun 12, 2003 7:16 pm

Thanks for your concern,
Could you please explain to me what $PHP_SELF means?

I was confused because when I click 'Previous' or 'Next' button, it brought me to: http://localhost/php/php.exe/researcher ... php?page=0
And the problem is my page became slightly slower to load and the image was not completely loaded.

I was wondering where did "php/php.exe" come?

Actually my directory is:
http://localhost/researchers/result.php?page=0

As far as I concern C:\php\php.exe is folder where I put my php.exe file.

Any advise would be appreciated
User avatar
wmdrumaizi
New php-forum User
New php-forum User
 
Posts: 40
Joined: Thu Jan 23, 2003 5:15 pm
Location: Malaysia

Postby WiZARD » Fri Jun 13, 2003 1:28 am

$PHP_SELF means what page POST or GETS no self.
for example:
Code: Select all
<A HREF="<?=$PHP_SELF;?>"Link</A>
User avatar
WiZARD
Moderator
Moderator
 
Posts: 1257
Joined: Thu Jun 20, 2002 10:14 pm
Location: Ukraine, Crimea, Simferopol

Postby Joan Garnet » Sun Jun 15, 2003 8:18 am

I would use
$_SERVER ["PHP_SELF"],
I think that the other is deprecated.
User avatar
Joan Garnet
Moderator
Moderator
 
Posts: 387
Joined: Sat Aug 03, 2002 2:56 am
Location: Mars

Postby WiZARD » Sun Jun 15, 2003 11:16 pm

Joan Garnet wrote:I would use
$_SERVER ["PHP_SELF"],
I think that the other is deprecated.

it was just example :wink:
User avatar
WiZARD
Moderator
Moderator
 
Posts: 1257
Joined: Thu Jun 20, 2002 10:14 pm
Location: Ukraine, Crimea, Simferopol

Postby Joel » Tue Jun 17, 2003 9:10 pm

If you want, I can post the function I use for my Previous, page number and next links.
Joel
New php-forum User
New php-forum User
 
Posts: 193
Joined: Sat Mar 29, 2003 11:57 pm
Location: Auckland, New Zealand

Postby wmdrumaizi » Wed Jun 18, 2003 2:15 am

Yes Joel, I want it, thanks for your contribution
User avatar
wmdrumaizi
New php-forum User
New php-forum User
 
Posts: 40
Joined: Thu Jan 23, 2003 5:15 pm
Location: Malaysia

Postby Joel » Wed Jun 18, 2003 8:55 pm

Code: Select all
function MultiPage($allrows, $numrows, $limit=10, $offset=0)
{
/*
   $allrows = Number of rows received by full Data query using mysql_num_rows
   $numrows = Number of rows received by this pages dataquery
   $limit = Max number of rows you want displaed on page
   $offset = To show what row to start query too
   Author: Joel Pinkham
*/
         ///////////////////////////////////MULTI-PAGE///////////////////
         echo "<p align=\"right\"> \n";
               
         ///////////////PREVIOUS////////////
         if ($offset>0) // bypass PREV link if offset is 0
         {
            $prevoffset=$offset-$limit;
            @print "<a href=\"$PHP_SELF?search=$search&from=$prevoffset&limit=$limit\"><< Previous</a> & \n";
         }
         
         
         //Calculate number of pages
         $pages=intval($allrows/$limit);
         
         //If there is a remainder another page is added on
         if ($allrows%$limit)
         {
            $pages++;
         }
         
         ///////////////PAGES////////////
         if ($allrows>$limit)
         {
            $pagenum = intval(($offset/$limit)+1);
            
            for ($i=1;$i<=$pages;$i++) { //Loop to print page numbers
               $newoffset=$limit*($i-1);
               if ($i==$pagenum)
               {
                  print "$i   \n";
               }
               else
               {
                  @print "<a href=\"$PHP_SELF?search=$search&from=$newoffset&limit=$limit\">$i</a> & \n";
               }
            }
         }
         ///////////////NEXT////////////
         if (($offset+$numrows)<$allrows) //Is this supposed to be the last page?
         {
            $newoffset=$offset+$limit;
            @print "<a href=\"$PHP_SELF?search=$search&from=$newoffset&limit=$limit\">Next >></a>\n";
         }
         
         echo "</p> \n";
}


It is quite dodgily documented as i just did it for myself. If you have any questions about any part of it (especially the $numrows and $allrows parts) just post back up.
Joel
New php-forum User
New php-forum User
 
Posts: 193
Joined: Sat Mar 29, 2003 11:57 pm
Location: Auckland, New Zealand


Return to mySQL & php coding

Who is online

Users browsing this forum: No registered users and 3 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.