PAGINATION

below is a drop down showing values 10, 50, 100. these are the records that can be displayed in a page.

though i know how to do pagination very well. i just want to know if i want to provide a drop down asking the user to select the number of results shown on a single page, then how can i do??

any hint is really appreciated.
pagination.PNG
yogeshbansal1985Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

elliottwebsitesCommented:
If your using mysql then use the LIMIT feature.
Do your normal page 1, 2, 3 etc
And just have:
<?php
$resultsperpage = 50;
$page = 0;

$limit1 = $page * $resultsperpage;
mysql_query("SELECT * FROM table WHERE x=y ORDER BY z LIMIT $page, $resultsperpage");

Open in new window


This is untested but should be something along those lines.
Lukasz ChmielewskiCommented:
Yes, I wrote a tutorial :P
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/A_4742-Simple-PHP-pagination.html

...but what you need is to pass the varible selected from the dropdown to the new page in $_GET as the mentioned LIMIT
Lukasz ChmielewskiCommented:
I guess that would get you cloder to what you want... if I did not miss anything

<?php
$limit = $_GET[limit];
// some additional coding
?>

<select name="limit">
  <option value="10" <?php if($resultsperpage == 10) echo"selected=\"selected\""; ?> >10</option>
  <option value="50" <?php if($resultsperpage == 50) echo"selected=\"selected\""; ?>>50</option>
  <option value="100" <?php if($resultsperpage == 100) echo"selected=\"selected\""; ?>>100</option>
</select>

<?php
// limit the results as you want it
mysql_query("SELECT * FROM table WHERE ... LIMIT $page, $limit");
?>

Open in new window

Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

yogeshbansal1985Author Commented:

yes Roads_Roads sir, this is wt i wanted. i had already the pagination code which i read in ur article. this is nice. i wanted to do with drop down. now i m trying...
yogeshbansal1985Author Commented:
@ROAD_ROAD

yes ur code is good and workable.

i was using this simple code which is also very good and working and now i add ur lines of code into it.
now i need a little help of urs.
plz see the snapshot below. i want to display 50 records in a page when selected 50 from the dropdown.
<?php  //pagination
mysql_connect("localhost","root","");
mysql_select_db("joins");
$page=$_GET['p'];
$no_of_rows=3;
if($page==""){
	$page=0;	
}
$start=$page*$no_of_rows;
$query="select * from books limit $start,$no_of_rows";
$result=mysql_query($query);
$query="select * from books";
$result2=mysql_query($query);
$total_rows=mysql_num_rows($result2)."<br>";
$no_of_pages=$total_rows/$no_of_rows;
while($row=mysql_fetch_assoc($result)){
	echo $row['book_name']."<br>";	
}
$pages="";
for($i=0;$i<=$no_of_pages;$i++){
	 $pages .='<a href="pagination.php?p='.$i.'">'.intval($i+1) .'</a>  |';	
}
echo "<br>".$pages;
?>

/// here i added ur code

<br /><br />	
<select name="limit">
  <option value="10" <?php if($resultsperpage == 10) echo"selected=\"selected\""; ?> >10</option>
  <option value="50" <?php if($resultsperpage == 50) echo"selected=\"selected\""; ?>>50</option>
  <option value="100" <?php if($resultsperpage == 100) echo"selected=\"selected\""; ?>>100</option>
</select>

Open in new window

untitled.PNG
Lukasz ChmielewskiCommented:
A bit messy but try this:

<?php  //pagination
mysql_connect("localhost","root","");
mysql_select_db("joins");
$page=$_GET['p'];
$no_of_rows=$_GET[limit];
if($page==""){
	$page=0;	
}
$start=$page*$no_of_rows;
$query="select * from books limit $start,$no_of_rows";
$result=mysql_query($query);
$query="select * from books";
$result2=mysql_query($query);
$total_rows=mysql_num_rows($result2)."<br>";
$no_of_pages=$total_rows/$no_of_rows;
while($row=mysql_fetch_assoc($result)){
	echo $row['book_name']."<br>";	
}
$pages="";
for($i=0;$i<=$no_of_pages;$i++){
	 $pages .='<a href="pagination.php?p='.$i.'">'.intval($i+1) .'</a>  |';	
}
echo "<br>".$pages;
?>

/// here i added ur code

<br /><br />	
<select name="limit">
  <option value="10" <?php if($resultsperpage == 10) echo"selected=\"selected\""; ?> >10</option>
  <option value="50" <?php if($resultsperpage == 50) echo"selected=\"selected\""; ?>>50</option>
  <option value="100" <?php if($resultsperpage == 100) echo"selected=\"selected\""; ?>>100</option>
</select>

Open in new window

yogeshbansal1985Author Commented:
sir this is the error coming when i tried wth ur code.

i m also trying sir.
untitled.PNG
Lukasz ChmielewskiCommented:
Let me get back to it in a couple of hours - if no other expert would help you during that time. It;s terribly late out here.
scifo_dkCommented:
Hi yogeshbansal1985.

You asked me to take a look at this question aswell.

Once again, PHP academy has a tutorial to help you regarding this matter:
http://www.youtube.com/watch?v=wC0uc_TkdR0&feature=mfu_in_order&list=UL

He uses the hardcoded "$per_page"-variable to define how many pages you want to show, and you just need to replace the hardcoded value, with a $_GET statement to fetch the number from your dropdown box.

Hope this helps.

// Scifo_dk
yogeshbansal1985Author Commented:
is it the correct way to fetch the number from the drop down box??
<?php
echo $_GET['limit'];
?>

<select name="limit">
  <option value="10" <?php if($resultsperpage == 10) echo"selected=\"selected\""; ?> >10</option>
  <option value="50" <?php if($resultsperpage == 50) echo"selected=\"selected\""; ?>>50</option>
  <option value="100" <?php if($resultsperpage == 100) echo"selected=\"selected\""; ?>>100</option>
</select>

Open in new window

yogeshbansal1985Author Commented:
or i need to write

<?php
echo $_GET[limit];
?>
scifo_dkCommented:
No, you need to get the limit value, before you show the user the results. So...

When the user clicks the button, that shows him the results, you should place the dropbox, beside that button. So the user choses how many posts he wants to see, and click the button to show the results.

The get function works like this. Example from the page where the user chooses:
<form action="results.php" method="get">
<select name="limit"> 
  <option value="10" <?php if($resultsperpage == 10) echo"selected=\"selected\""; ?> >10</option> 
  <option value="50" <?php if($resultsperpage == 50) echo"selected=\"selected\""; ?>>50</option> 
  <option value="100" <?php if($resultsperpage == 100) echo"selected=\"selected\""; ?>>100</option> 
</select> 
<input type="submit" />
</form> 

Open in new window


And then fetch it on the results page with get-function, and put it into the $per_page variable:
$per_page = $_GET["limit"]; 

Open in new window


If you want the user to be able to change the limit amount, on the results page, you can add the first code, and label the submit button "update" or "refresh".

I'm not so good at explaining this, hope it's understandable.
Lukasz ChmielewskiCommented:
Try this:

<?php  //pagination
mysql_connect("localhost","root","");
mysql_select_db("joins");

$page=$_GET['p'];
$resultsperpage=$_GET[limit];

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

$start = $page * $resultsperpage;

$query="select * from books limit $start,$resultsperpage";
$result=mysql_query($query);

$query="select * from books";
$result2=mysql_query($query);

$total_rows=mysql_num_rows($result2)."<br>";
$no_of_pages=ceil($total_rows/$no_of_rows);

while($row=mysql_fetch_assoc($result)){
	echo $row['book_name']."<br>";	
}
$pages="";
for($i=0;$i<=$no_of_pages;$i++){
	 $pages .='<a href="pagination.php?p='.$i.'&limit='.$resultperpage.'">'.intval($i+1) .'</a>  |';	
}
echo "<br>".$pages;
?>


<br /><br />	
<form action="pagination.php" method="get">
<select name="limit"> 
  <option value="10" <?php if($resultsperpage == 10) echo"selected=\"selected\""; ?> >10</option> 
  <option value="50" <?php if($resultsperpage == 50) echo"selected=\"selected\""; ?>>50</option> 
  <option value="100" <?php if($resultsperpage == 100) echo"selected=\"selected\""; ?>>100</option> 
</select> 
<input type="submit" />
</form> 

Open in new window

Lukasz ChmielewskiCommented:
ok, a little correction

<?php  //pagination
mysql_connect("localhost","root","");
mysql_select_db("joins");

$page=$_GET['p'];
$resultsperpage=$_GET[limit];

if(empty($page)){$page=0;}
if(empty($resultsperpage)){$resultsperpage=10;}

$start = $page * $resultsperpage;

$query="select * from books limit $start,$resultsperpage";
$result=mysql_query($query);

$query="select * from books";
$result2=mysql_query($query);

$total_rows=mysql_num_rows($result2)."<br>";
$no_of_pages=ceil($total_rows/$resultsperpage);

while($row=mysql_fetch_assoc($result)){
	echo $row['book_name']."<br>";	
}
$pages="";
for($i=0;$i<=$no_of_pages;$i++){
	 $pages .='<a href="pagination.php?p='.$i.'">'.intval($i+1) .'</a>  |';	
}
echo "<br>".$pages;
?>


<br /><br />	
<form action="pagination.php" method="get">
<select name="limit"> 
  <option value="10" <?php if($resultsperpage == 10) echo"selected=\"selected\""; ?> >10</option> 
  <option value="50" <?php if($resultsperpage == 50) echo"selected=\"selected\""; ?>>50</option> 
  <option value="100" <?php if($resultsperpage == 100) echo"selected=\"selected\""; ?>>100</option> 
</select> 
<input type="submit" />
</form> 

Open in new window

yogeshbansal1985Author Commented:
its working fine.. sir can't we do without a submit button.

the request should go as soon as we change the option from drop down box.
Lukasz ChmielewskiCommented:
Try to use jQuery javascript framework for that, I've included it - with necessary functions - in the head section of the page

<!DOCTYPE html>
<html>
<head>
  <script src="http://code.jquery.com/jquery-1.5.js"></script>
  <script type="text/javascript">
  $(document).ready(function(){
      $('select[name=limit]').change(function(){
        $('#paginationform').submit();
      });  
  });
  
  </script>
</head>
<body>

<?php  //pagination
mysql_connect("localhost","root","");
mysql_select_db("joins");

$page=$_GET['p'];
$resultsperpage=$_GET[limit];

if(empty($page)){$page=0;}
if(empty($resultsperpage)){$resultsperpage=10;}

$start = $page * $resultsperpage;

$query="select * from books limit $start,$resultsperpage";
$result=mysql_query($query);

$query="select * from books";
$result2=mysql_query($query);

$total_rows=mysql_num_rows($result2)."<br>";
$no_of_pages=ceil($total_rows/$resultsperpage);

while($row=mysql_fetch_assoc($result)){
	echo $row['book_name']."<br>";	
}
$pages="";
for($i=0;$i<=$no_of_pages;$i++){
	 $pages .='<a href="pagination.php?p='.$i.'">'.intval($i+1) .'</a>  |';	
}
echo "<br>".$pages;
?>


<br /><br />	
<form action="pagination.php" method="get" id="paginationform">
<select name="limit"> 
  <option value="10" <?php if($resultsperpage == 10) echo"selected=\"selected\""; ?> >10</option> 
  <option value="50" <?php if($resultsperpage == 50) echo"selected=\"selected\""; ?>>50</option> 
  <option value="100" <?php if($resultsperpage == 100) echo"selected=\"selected\""; ?>>100</option> 
</select> 
</form> 

</body>
</html>

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
yogeshbansal1985Author Commented:
thks sir, its done.
scifo_dkCommented:
According to this:
http://www.codingforums.com/showthread.php?t=64200

You might be able to add an OnChange event to refresh the dropdown box like this:
<select name="limit" onChange="this.form.submit()">

Open in new window


I have not tryed this myself though.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.