im creating pagination(it is not complete, but only most important part i think) for my website, everything is working good but it shows posts from the oldest it means oldest are on top ... but i need the newest posts on top on first page of my pagination
this is my pagination script
include('mysql.php');
$tbl_name="posts";
$adjacents = 3;
/*
First get total number of rows in data table.
*/
$query = "SELECT COUNT(*) as num FROM posts";
$total_pages = mysql_fetch_array(mysql_query($query));
$total_pages = $total_pages[num];
/* Setup vars for query. */
$targetpage = "index.php";
$limit = 6;
$page = $_GET['page'];
if($page)
$start = ($page - 1) * $limit;
else
$start = 3;
/* Get data. */
$sql = "SELECT * FROM posts LIMIT $start, $limit";
$result = mysql_query($sql);
/* Setup page vars for display. */
if ($page == 0) $page = 1;
$prev = $page - 1;
$next = $page + 1;
$lastpage = ceil($total_pages/$limit);
$lpm1 = $lastpage - 1;
When i use
$result = mysql_safe_query('SELECT * FROM posts ORDER BY date DESC');
it will show posts from the newest to oldest but pagination will fail
Im still learning php so for every advice or solution really thanks
Related
I am having a problem with putting order by acctname limit 1,3. It is showing the same results from the 1st page to the last page.
<?php
$_payrolldate = '2019-10-15';
$pagenum = 1;
$record_limit_per_page = 24;
$offset = ($pagenum-1) * $record_limit_per_page;
$sql_count = mysqli_query($db,"SELECT COUNT(*) As total_records FROM `tbl_payroll_charges` WHERE payroll_date='$_payrolldate'");
$total_records = mysqli_fetch_array($sql_count);
$total_records = $total_records['total_records'];
$total_no_of_pages = ceil($total_records / $record_limit_per_page);
for ($pagenum = 1; $pagenum <= $total_no_of_pages; $pagenum++) {
$sqlEmp = "SELECT
tbl_payroll_charges.acctname,
tbl_payroll_charges.payroll_date,
tbl_payroll_charges.branch,
tbl_payroll_charges.date_happened,
tbl_payroll_charges.personal_charges,
tbl_payroll_charges.inventory_charges,
tbl_payroll_charges.raw_material_charges,
tbl_payroll_charges.infraction_charges,
tbl_payroll_charges.other_charges,
tbl_payroll_charges.total_charges
FROM tbl_payroll_charges
WHERE payroll_date='$_payrolldate'
order by acctname ASC
LIMIT $pagenum, $record_limit_per_page";
$empResult = mysqli_query($db, $sqlEmp);
$final_total = 0;
while($listemp = mysqli_fetch_array($empResult))
{
echo $listemp['acctname']."<br>";
}
}
The problem with the above code is that it is showing same results on all pages. This code generates 37 pages, each page returns 24 rows, 24 rows fit in one legal size paper. When I removed the "order by acctname" it works well.
the print layout before sending to the printer
The Final Image for Printing
So to make a final answer, This is the final code and thanks to smashrain for making me realize that i am stoopid hahaha anyway i am trying to make my own crystal report using php and pure css only.
<?php
$_payrolldate = '2019-10-15';
$pagenum = 1;
$record_limit_per_page = 24;
$sql_count = mysqli_query($db,"SELECT COUNT(*) As total_records FROM `tbl_payroll_charges` WHERE payroll_date='$_payrolldate'");
$total_records = mysqli_fetch_array($sql_count);
$total_records = $total_records['total_records'];
$total_no_of_pages = ceil($total_records / $record_limit_per_page);
for ($pagenum = 1; $pagenum <= $total_no_of_pages; $pagenum++) {
$offset = ($pagenum-1) * $record_limit_per_page;
$sqlEmp = "SELECT
tbl_payroll_charges.acctname,
tbl_payroll_charges.payroll_date,
tbl_payroll_charges.branch,
tbl_payroll_charges.date_happened,
tbl_payroll_charges.personal_charges,
tbl_payroll_charges.inventory_charges,
tbl_payroll_charges.raw_material_charges,
tbl_payroll_charges.infraction_charges,
tbl_payroll_charges.other_charges,
tbl_payroll_charges.total_charges
FROM tbl_payroll_charges
WHERE payroll_date='$_payrolldate'
order by acctname ASC
LIMIT $offset, $record_limit_per_page";
$empResult = mysqli_query($db, $sqlEmp);
$final_total = 0;
while($listemp = mysqli_fetch_array($empResult))
{
echo $listemp['acctname']."<br>";
}
}
I created php script following the tutorial, but it has a mistake. It displays in the last page information which is in the previous page - it's because $perpage. How can I display only data which wasn't display yet.
EXAMPLE - If I set $perpage to 3 and I have 7 records (named 1,2,3,4,5,6,7) on page one is 1,2,3 on page two is 4,5,6 and on the last page is 5,6,7 (I want to display only record 7)
$query = mysql_query("SELECT ID FROM clanek");
$count = mysql_num_rows($query);
$perpage = 3; // řádků na stránku
$pages_count = ceil($count / $perpage); //celkem stránek zaokrohleno
$page = isset($_GET['page']) ? $_GET['page'] : 1;
$is_first = $page == 1; //první stránka
$is_last = $page == $pages_count;
$prev = max(1, $page - 1);
$next = min($pages_count , $page + 1); /
$data = mysql_query("SELECT DATE_FORMAT(datum_pridani_c,'%d.%m.%Y %H:%i:%s')as datumcas,nazev,kratky_popis,ID FROM clanek ORDER BY datum_pridani_c DESC LIMIT ".($page - 1).", ".$perpage); /
while ($zaznam = mysql_fetch_array($data)) {
//some info her
}
if($pages_count>0) {
if(!$is_first) {
echo '<a class="predchozistranka" href="index.php?page='.$prev.'">Předchozí</a>';
}
echo '<span class="stranka">Stránka '.$page.' / '.$pages_count.'</span>';
if(!$is_last) {
echo '<a class="dalsistranka" href="index.php?page='.$next.'">Další</a>';
}
}
$data = mysql_query("SELECT DATE_FORMAT(datum_pridani_c,'%d.%m.%Y %H:%i:%s')as datumcas,nazev,kratky_popis,ID FROM clanek ORDER BY datum_pridani_c DESC LIMIT ".($page - 1).", ".$perpage);
It has been a while since I have used MySQL but I fired up my Workbench just now and I see that the syntax for LIMIT is LIMIT offset,rowcount. The doc says so too http://dev.mysql.com/doc/refman/5.0/en/select.html
For your query to work then, instead of ($page - 1), $perpage, it should be ($page - 1)*$perpage, $perpage
$query = mysql_query("SELECT ID FROM clanek");
$count = mysql_num_rows($query);
Irrelevant but the above code is highly inefficient for getting the total number of rows. It would be better if you use SELECT count(id) FROM clanek
OK i have a basic php pagination script, which has a basic next and previous button. Now this works fine until i add a distinct clause. Please see code below.
$query = "SELECT COUNT(*) as num FROM $tableName WHERE engine='$type' AND manufacturer='$man' AND '$year' BETWEEN start_year AND end_year";
$total_pages = mysql_fetch_array(mysql_query($query));
$total_pages = $total_pages['num'];
echo "$total_pages";
$stages = 3;
$page = mysql_escape_string($_GET['page']);
if($page){
$start = ($page - 1) * $limit;
}else{
$start = 0;
}
// Get page data
$query1 = "SELECT Distinct model_group from $tableName WHERE engine='$type' AND manufacturer='$man' AND '$year' BETWEEN start_year AND end_year LIMIT $start, $limit";
$result = mysql_query($query1);
// Initial page num setup
if ($page == 0){$page = 1;}
$prev = $page - 1;
$next = $page + 1;
$lastpage = ceil($total_pages/$limit);
$LastPagem1 = $lastpage - 1;
Now if i change the query in query 1 to be
$query = "SELECT * From
the code works fine, below is the code for my next and previous buttons.
Previous Button
if ($page > 1){
echo "<a href='$targetpage?page=$prev&type=$type&manufacturer=$manufacturer&year=$year'><div class='previous'><img src='images/PrevButton1.fw.png' width='108' height='58' style='border: none;'/></span></a>";
}else{
echo "<span class='disabled'><div class='previous'>Previous</span></span>"; }
Next Button
if ($page < $lastpage){
echo "<a href='$targetpage?page=$next&type=$type&manufacturer=$manufacturer&year=$year'><div class='next'><img src='images/MoreButton1.fw.png' width='108' height='58' style='border: none;'/></span></a>";
}else{
echo "<span class='disabled'><div class='next'>More</span></span>";
}
IS there anyway i can include the distinct value in to the count query? as i think it returns a different value to the second query.
i think you have to use a
SELECT COUNT(DISTINCT *)
in your first statement
a better way in mysql is to fix your paging problem with the SQL_CALC_FOUND_ROWS option in your select:
SELECT SQL_CALC_FOUND_ROWS * FROM mysql.user LIMIT 1
your result will be 1 row... but with
SELECT FOUND_ROWS();
you become the count of your result without the limit!
I am displaying some records from the database. Currently I am displaying the top twenty records but I want to show all records with twenty records per page.
How will I use the paging in php to display all records?'
Any Idea about this?
Something like this might work:
$posts_per_page = 10;
$page = isset($_GET['page']) ? intval($_GET['page']) : 0;
$query = sprintf("SELECT * FROM <table> LIMIT %d, %d",
$page * $post_per_page,
$posts_per_page);
$result = mysql_query($query);
The above query will select 10 records with a offset based on the current page.
You will also have to get the total number of pages based on the $posts_per_page:
$query = "SELECT COUNT(*) c FROM <table>";
You will have to keep track of the total number of posts divided with $posts_per_page and also the current page, then you can display a prev and next link:
if ($current_page > 0) {
echo '<a href="index.php?page=' . ($current_page - 1) . '>prev</a>';
}
if ($current_page < $max_pages - 1) {
echo '<a href="index.php?page=' . ($current_page + 1) . '>next</a>';
}
The basic idea is to send a page variable, for example $page, to your script and then customize your MySQL query to retrieve only that page's results.
Here is an example so you can see what I mean:
$page = $_GET['page'];
$resultsPerPage = 20;
$start = ($page - 1) * $resultsPerPage;
$query = "SELECT `results` FROM `table` WHERE ... ORDER BY `date` LIMIT $start, $resultsPerPage"
You can see how if $page = 1, then $start = 0 and this query will retrieve the 20 results, offset at 0. For $page = 2, $start = 20 so the query will retrieve the results from 20 - 40, which should show on the second page.
This is assuming your first page is 1. If you want the first page to be 0, then you need to change $start:
$start = $page * $resultsPerPage;
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