Link to home
Start Free TrialLog in
Avatar of yogeshbansal1985
yogeshbansal1985

asked on

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
Avatar of elliottwebsites
elliottwebsites

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.
Yes, I wrote a tutorial :P
https://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
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

Avatar of yogeshbansal1985

ASKER


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...
@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
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

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

i m also trying sir.
untitled.PNG
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.
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
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

or i need to write

<?php
echo $_GET[limit];
?>
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.
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

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

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.
ASKER CERTIFIED SOLUTION
Avatar of Lukasz Chmielewski
Lukasz Chmielewski
Flag of Poland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thks sir, its done.
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.