Hi I have a system where I only want to display the last 300 records from MYSQL, normally i would just write the query like this LIMIT 300
the problem i have is i am using a pagination system which writes the query like this.
$tableName="masterip_details";
$targetpage ="raw_data.php";
$limit = 30;
$query = "SELECT COUNT(*) as num FROM $tableName where type='6' AND country_code='GB'";
$total_pages = mysql_fetch_array(mysql_query($query));
$total_pages = $total_pages[num];
$stages = 3;
$page = mysql_escape_string($_GET['page']);
if($page){
$start = ($page - 1) * $limit;
}else{
$start = 0;
}
// Get page data
$query1 = "SELECT * FROM $tableName where type='6' AND country_code='GB' LIMIT $start, $limit";
$result = mysql_query($query1);
The problem is because it uses the limit to calculate the start and finish page numbers i am not sure if i can limit the number of rows to return whilst using the pagination.
select * from (SELECT * FROM $tableName where type='6' AND country_code='GB' order by AUTO_INCERMENT_ID DESC LIMIT 300) as a order by AUTO_INCERMENT_ID ASC LIMIT $start, $limit
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>";
}
}
Is there any way to count and split results without doing 2 query,
im using a query something like this:
$result = mysqli_query($con,"SELECT * from articles WHERE category = '$category'");
$row = mysqli_fetch_row($result);
$rows = $row[0];
$page_rows = 20;
$last = ceil($rows/$page_rows);
$pagenum = 1;
$limit = 'LIMIT ' .($pagenum - 1) * $page_rows .',' .$page_rows;
$result2 = mysqli_query($con,"SELECT * FROM articles order by id desc $limit");
while($row = mysqli_fetch_array($result2)) {
$id = $row['id'];
}
this is working but i dont like that it has 2 queries, any better idea? thank you
$pagenum = 1;
$rows_on_page = 20;
$start = (($pagenum - 1) * $rows_on_page);
$end = ($pagenum * $rows_on_page);
$result = mysqli_query($con, "SELECT * from articles WHERE category = '$category' ORDER BY id DESC LIMIT $start, $end");
while ($row = mysqli_fetch_array($con,$result) {
... do stuff with articles ...
$pagenum++;
}
The while loop will protect you from going past the end of the records.
try this query,it will return count of records and pagination (title in query is a field name, change it based on your table):
SELECT aa.countt, title FROM articles , (SELECT COUNT(*) AS countt FROM articles WHERE category = '$category' ) AS aa ORDER BY id LIMIT 5,10
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'm trying to Order by Desending and want to limit 30 in Query
PHP CODE
$page = $_POST['page'];
$cur_page = $page;
$page -= 1;
$per_page = 30;
$previous_btn = true;
$next_btn = true;
$first_btn = true;
$last_btn = true;
$start = $page * $per_page;
$query_pag_data = "SELECT * from titles LIMIT $start, $per_page ORDER BY id DESC";
ERROR : MySql ErrorYou have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ORDER BY id DESC' at line 1
PS: i'm using pagination... so limiting 30 result like this
correct syntax is:
SELECT * from titles
ORDER BY id DESC
LIMIT $start, $per_page
LIMIT at the end of the query.
change positions or LIMIT and ORDER BY, like:
$query_pag_data = "SELECT * from titles ORDER BY id DESC LIMIT $start, $per_page";
You have to put the limit at the end of the query for proper syntax:
$query_pag_data = "SELECT * from titles ORDER BY id DESC LIMIT $start, $per_page";
You need to put ORDER BY statement first before LIMIT
Correct syntax is as follows:
SELECT * FROM *table_name* WHERE *condition* ORBER BY *field_name* LIMIT *limit*;
LIMIT statement should always come at the end of the query.
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