Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Update to include full code.
I am pulling records from a database and then displaying them 5 per page like this:
$num_rec_per_page=5;
if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; };
$start_from = ($page-1) * $num_rec_per_page;
$school = $_REQUEST['school'];
$sql = "SELECT * FROM `data` WHERE `school` LIKE '%$school%'";
$rs_result = mysql_query($sql);
$total_records = mysql_num_rows($rs_result); //count number of records
$total_pages = ceil($total_records / $num_rec_per_page);
for ($i=1; $i<=$total_pages; $i++) {
echo "<a href='search.php?school=".urlencode($school)."&page=".$i."'>".$i."</a>";
};
This gives me a long list like this for each page:
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
What I would like to do is something like:
<Prev 1 2 3 4....53 54 55 Next>
Use LIMIT and OFFSET clauses in your SELECT query to implement pagination.
Here's the reference:
PHP Limit Data Selections From MySQL
So your code should be like this:
$num_rec_per_page = 5;
if(isset($_GET["page"])){
$page = $_GET["page"];
}else{
$page=1;
}
$offset = ($page - 1) * $num_rec_per_page;
$school = $_GET['school'];
$sql = "SELECT * FROM `data` WHERE `school` LIKE '%" . $school. "%' LIMIT 5 OFFSET " . $offset;
$rs_result = mysql_query($sql);
$total_records = mysql_num_rows($rs_result); //count number of records
$total_pages = ceil($total_records / $num_rec_per_page);
for ($i=1; $i<=$total_pages; $i++){
if($i != 1){
$prev_page = $i - 1;
echo "<a href='search.php?school=".urlencode($school)."&page=" . $prev_page ."'><prev</a>";
}
echo " <a href='search.php?school=".urlencode($school)."&page=".$i."'>".$i."</a>";
if($i != $total_pages){
$next_prev = $i + 1;
echo " <a href='search.php?school=".urlencode($school)."&page=" . $next_page ."'>next></a>";
}
}
Sidenote: Don't use mysql_* functions, they are deprecated as of PHP 5.5 and are removed altogether in PHP 7.0. Use mysqli or pdo instead. And this is why you shouldn't use mysql_* functions.
Related
I want to add dropdown in pagination so that if there is more then 10 pages it will be stored in dropdown so that any one can select any page from it.
Here is my php code which show this output:
1 2 3 4 5 6 7 8 9 10 11........... 100
$sql = "SELECT * FROM `new_data`";
$rs_result = mysqli_query($con,$sql);
$total_records = mysqli_num_rows($rs_result); //count number of records
$total_pages = ceil($total_records / $num_rec_per_page);
echo "<a href='View.php?page=1'>".'|<'."</a> "; // Goto 1st page
for ($i=1; $i<=$total_pages; $i++) {
echo " <a href='View.php?page=".$i."'>".$i."</a> ";
};
echo " <a href='View.php?page=$total_pages'>".'>|'."</a> ";//Goto last page
Output i want :
1 2 3 4 5 6 7 8 9 10 dropdown(contains all pages 11 to 100)
$sql = "SELECT * FROM `new_data`";
$rs_result = mysqli_query($con,$sql);
$total_records = mysqli_num_rows($rs_result); //count number of records
$total_pages = ceil($total_records / $num_rec_per_page);
echo "<a href='View.php?page=1'>".'|<'."</a> "; // Goto 1st page
$array = [];
for ($i=1; $i<=$total_pages; $i++) {
$array[$i] = "<a href='View.php?page=".$i."'>".$i."</a>";
};
echo " <a href='View.php?page=$total_pages'>".'>|'."</a> ";//Goto last page
Add all the page informations in array and then later populate the dropdown with an array I have changed the for loop code only ... you just popultae your dropdown with the $array;
I done it with php using your php code. Here is my code:
$sql = "SELECT * FROM `new_data`";
$rs_result = mysqli_query($con,$sql); //run the query
$total_records = mysqli_num_rows($rs_result); //count number of records
$total_pages = ceil($total_records / $num_rec_per_page);
echo "<a href='View.php?page=1'>".'FIRST <<'."</a> "; // Goto 1st page
for ($i=1; $i<=$total_pages; $i++)
{
if($total_pages<=10)
{
echo " <a style='color:#333;' href='View.php?page=".$i."'>".$i."</a> ";
}
else
{
for ($i=1; $i<=10; $i++)
{
echo " <a style='color:#333;' href="View.php?page=".$i."'>".$i."</a> ";
};
echo "<select class='mySelectBox' onchange='location = this.options[this.selectedIndex].value;'>";
for ($i=11; $i<=$total_pages; $i++)
{
echo "<option value= View.php?page=".$i.">".$i."</option>";
};
echo "</select>";
}
};
echo " <a href='View.php?page=$total_pages'>".'........>> LAST'."</a> "; // Goto last page
I have one page where I echo 10 items from MySql DB and has pagination for next page. The problem is that there are 1000+ rows in database.. so you can guess how many pages are in pagination links. How can I limit their numbers for example to show
1 2 3 4 5 ... N
when user change to page 2
2 3 4 5 6 ... N
and so on?
Or something else.. the main goal is just to hide every single page number.
<?php
if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; };
$start_from = ($page-1) * 10;
$result = $pdo->prepare("SELECT t.* FROM images t order by randorder ASC LIMIT $start_from, 10");
$result->execute();
for($i=0; $row = $result->fetch(); $i++)
{
echo 'echo data';
}
echo '<div class="pagination" >';
$result = $pdo->prepare("SELECT COUNT(image_id) FROM images");
$result->execute();
$row = $result->fetch();
$total_records = $row[0];
$total_pages = ceil($total_records / 10);
for ($i=1; $i<=$total_pages; $i++) {
echo "<a href='?page=".$i."'";
if($page==$i)
{
echo "class=active";
}
echo ">";
echo "".$i."</a> ";
};
echo '</div>';
?>
I'm having trouble with the following code from a tutorial, it seems very simple but there must be something I'm missing. There is nothing at all where my numbered page menu should be, can anyone see why?
if (isset($_GET["page"])) { // get page number for query
$page = $_GET["page"];
}
else {
$page = 1; // no page number? set it
};
$start_from = ($page-1) * 20;
$query = "SELECT * FROM posts";
$query.= " WHERE isstart = 'y' AND iscomplete = 'n' ORDER BY date DESC LIMIT $start_from, 20";
$start_from, 20"; // use LIMIT (and options) to make sure only 20 are displayed
$result = mysql_query($query);
$query_count = "SELECT COUNT(post_ID) FROM posts WHERE isstart = 'y'";
$count_result = mysql_query($query_count);
$count_results = mysql_fetch_row($count_result);
$total_posts = $count_results[0];
$total_pages = ceil($total_posts / 20); // get total pages needed for page menu
for ($i=1; $i<=$total_pages; $i++) { // set the page numbers
$pagelink = "Page: <a href='index_test.php?page=".$i."'>".$i."</a>"; // make the page menu
};
$top_body_text = '<p align="left">'.$pagelink.'</p>';
Currently my statement echo'ing $pagelink creates noting.
First thing, there is a error on this line
$start_from, 20";
It should be
$start_from = 20;
and lastly, you need to echo '$pagelink' inside the for loop in order to see each pages number listed out. Example below:
if (isset($_GET["page"]))
{ // get page number for query
$page = $_GET["page"];
}
else {
$page = 1; // no page number? set it
};
$start_f = 20; // use LIMIT (and options) to make sure only 20 are displayed
$start_from = ($page-1) * $start_f;
$query = "SELECT * FROM posts";
$query.= " WHERE isstart = 'y' AND iscomplete = 'n' ORDER BY date DESC LIMIT $start_from, 20";
$result = mysql_query($query);
$query_count = "SELECT COUNT(post_ID) FROM posts WHERE isstart = 'y'";
$count_result = mysql_query($query_count);
$count_results = mysql_fetch_row($count_result);
$total_posts = $count_results[0];
$total_pages = ceil($total_posts / 20); // get total pages needed for page menu
for ($i=1; $i<=$total_pages; $i++)
{ // set the page numbers
echo $pagelink = "Page: <a href='index_test.php?page=".$i."'>".$i."</a>"; ///Echo here
};
$top_body_text = '<p align="left">'.$pagelink.'</p>';
Remove this line -- it's not doing anything:
$start_from, 20"; // use LIMIT (and options) to make sure only 20 are displayed
You'd already defined $start_from here:
$start_from = ($page-1) * 20;
Then check out your results and see if you are all set.
I have this code which splits the mysql results into 10 results per page. but I want to edit it so that it limits links based on what the current page is. so on pages 1-10 it only shows page links 1-10 on pages 11-20 it only shows page links 11-20 and so on. how could I achieve this? Thanks.
if (isset($_GET["page"]))
{
$page = $_GET["page"];
}
else
{
$page=1;
};
$start_from = ($page-1) * 10;
$message = "SELECT * FROM document WHERE email='$_SESSION[email]' ORDER BY id DESC LIMIT $start_from , 10";
// echo results
// make page links for results
$sql = "SELECT id FROM document WHERE email = '$_SESSION[email]'";
$rs_result = $db->query($sql);
$total_records = $rs_result->num_rows;
$total_pages = ceil($total_records / 10);
if($rs_result->num_rows >10) {
$page = "<p class = 'page'>";
for ($i=1; $i<=$total_pages; $i++)
{
$page .="<a href='results.html?page=".$i."'>".$i."</a> ";
}
$page .="</p>";
echo $page;
}
Do this
"SELECT * FROM document WHERE email='$_SESSION[email]' ORDER BY id DESC LIMIT 10 OFFSET $start_from";
Tell me if it works.
I have a MySQL query
SELECT * FROM 'redirect'
WHERE 'user_id'= \''.$_SESSION['user_id'].' \'
ORDER BY 'timestamp'`
I want to paginate 10 results per page. How Can I do it?
Here is a nice starting point:
<?php
// insert your mysql connection code here
$perPage = 10;
$page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1;
$startAt = $perPage * ($page - 1);
$query = "SELECT COUNT(*) as total FROM redirect
WHERE user_id = '".$_SESSION['user_id']."'";
$r = mysql_fetch_assoc(mysql_query($query));
$totalPages = ceil($r['total'] / $perPage);
$links = "";
for ($i = 1; $i <= $totalPages; $i++) {
$links .= ($i != $page )
? "<a href='index.php?page=$i'>Page $i</a> "
: "$page ";
}
$r = mysql_query($query);
$query = "SELECT * FROM 'redirect'
WHERE 'user_id'= \''.$_SESSION['user_id'].' \'
ORDER BY 'timestamp' LIMIT $startAt, $perPage";
$r = mysql_query($query);
// display results here the way you want
echo $links; // show links to other pages
Use LIMIT.
SELECT *
FROM redirect
WHERE user_id = '35251'
ORDER BY timestamp
LIMIT 40, 10
40 is how many records to skip, 10 is how many to display.
There are also a few problems with your PHP. You use backticks (not single quotes) to surround table and column names. And you shouldn't use string concatenation to build your query.
Here is my code
which contains next and Previous button
<?php
$limit = 3; //set Number of entries to show in a page.
// Look for a GET variable page if not found default is 1.
if (isset($_GET["page"])) {
$page = $_GET["page"];
}
else { $page=1;
}
//determine the sql LIMIT starting number for the results on the displaying page
$page_index = ($page-1) * $limit; // 0
$All_Users=mysqli_query($con,"select * from users limit $page_index, $limit");
while($row=mysqli_fetch_array($All_Users))
{
//show data in table or where you want..
}
$all_data=mysqli_query($con,"select count(*) from users");
$user_count = mysqli_fetch_row($all_data); // say total count 9
$total_records = $user_count[0]; //9
$total_pages = ceil($total_records / $limit); // 9/3= 3
if($page >= 2){
echo "<a href='blog.php?page=".($page-1)."' class='btn
customBtn2'>Previous</a>";
}
if($page<$total_pages) {
echo "<a href='blog.php?page=".($page+1)."' class='btn customBtn2'>NEXT</a>";
}
?>
Use the LIMIT clausule of the query to limit the amount of results you retrieve from the database.
See: http://dev.mysql.com/doc/refman/5.1/en/select.html