Link to another page in php pagination? - php

I have a code with query for in php paging
$page = (isset($_GET['page'])) ? $_GET['page'] : 1;
$startPoint = $page - 1;
$sql="SELECT * FROM ` admin_crmf_poc_event_history`
where $condition
order by event_date asc
LIMIT $startPoint,30";
$result = mysql_query($sql);
and for creating link to next page, i use
Prev
Next
but I give link index.php which shows the whole values from the start. dont know how to give link of next page so the rest of values are shown. Please help??

try it like this
$page = (isset($_GET['page']) && (int)$_GET['page']>0) ? (int)$_GET['page'] : 1);
$startPoint = ($page*30) - 30;
$sql="SELECT * FROM ` admin_crmf_poc_event_history`
where $condition
order by event_date asc
LIMIT $startPoint,30";
$result = mysql_query($sql);
<?php if($page>1){?>Prev<?php } ?>
Next
so what I did is first I added (int) before your $_GET['page'] to cast the $_GET value to int, second thing is I multiplied $page by how many rows per page you want then subtracted rows per page so, if you are at page 1 your start point will be 1*30-30=0 at page 2 it will be 2*30-30=30 etc... Then all you have to do with page links is subtract 1 for previous page and add 1 for next page.

Related

PHP Pagination issues and errors

I'm learning PHP at the moment.
I want to make my HTML page pagination.
Here is the code I have:
I'm trying to make 6 videos only per page. And to do that I'm going to search how many ID's I have it means I have that same value of videos so I can make X pages.
At the moment, I'm having this error:
Notice: Object of class mysqli_result could not be converted to int in C:\xampp\htdocs\Site\index.php on line 20
$stmt3 = $db->prepare ("SELECT COUNT(ID_Video) as total FROM Videos");
$stmt3->execute();
$result2 = $stmt3->get_result();
$result2->fetch_assoc();
// Remember to round it up always!
$VideosPerPage = 6;
$totalPages = ceil($result2 / $VideosPerPage);
// Check that the page number is set.
if(!isset($_GET['page'])){
$_GET['page'] = 0;
}else{
// Convert the page number to an integer
$_GET['page'] = (int)$_GET['page'];
}
// If the page number is less than 1, make it 1.
if($_GET['page'] < 1){
$_GET['page'] = 1;
// Check that the page is below the last page
}else if($_GET['page'] > $totalPages){
$_GET['page'] = $totalPages;
}
You are seeing this error because $result2 is PDO result- not an integer. You need to assign the fetched value from the result in a variable. You can use fetchColumn() as you are getting one column. Your first few lines could look like following:
$result2 = $stmt3->get_result();
$totalVideos = $result2->fetchColumn(); // will get the number from single column
// Remember to round it up always!
$VideosPerPage = 6;
$totalPages = ceil($totalVideos / $VideosPerPage);

PHP/MySQLi Page Numbers and Final query issues

I am trying to create a page number function that displays 9 results per page from my forum_replies.sql table. My PHP code so far will only display page 1, page 2. Page 1 has 9 query's but page two has none... but there's 22 rows of data that should be fetched, so at least 2 pages should show!
Here's my code!
if(isset($_GET["p"]) && is_numeric($_GET["p"]) && $_GET["p"] > 1) {
$currentPage = $_GET["p"];
$limiter = $currentPage * 9;
} else {
$currentPage = 1;
$limiter = 0;
}
$finalQuery = "SELECT * FROM forum_replies WHERE thread_id = '1' ORDER BY id ASC LIMIT " . $limiter . ",9";
Figured out that the isset at the top.. $limiter works like this
0,9 = page 1.. correct
18,9 = page 3.. how do I get page 2 (9,9) and so on.. cause it's completely skipping 9,9!
How about calling echo $db->error; after each query? You can also echo the querys and try them in phpMyAdmin to find out what's wrong. I can't spot a mistake in the finalQuery, at least not syntax-wise.

Php pagination from Mysql Select * Where; If click on next page number, then displays all rows from Mysql

In input field user enters condition - what data to get from mysql. For example, user wants to get mysql rows where month is October (or 10).
Here I get user input $date_month = $_POST['date_month'];
Then mysql statement and code for pagination
try {
$sql = $db->prepare("SELECT * FROM 2_1_journal WHERE RecordMonth = ? ");
$sql->execute(array($date_month));
foreach ($sql as $i => $row) {
}
$number_of_fetched_rows = $i;
$number_of_results_per_page = 100;
$total_pages = ceil($number_of_fetched_rows / $number_of_results_per_page);
if (isset($_GET['page']) && is_numeric($_GET['page'])) {
$show_page = $_GET['page'];
if ($show_page > 0 && $show_page <= $total_pages) {
$start = ($show_page -1) * $number_of_results_per_page;
$end = $start + $number_of_results_per_page;
}
else {
$start = 0;
$end = $number_of_results_per_page;
}
}
else {
$start = 0;
$end = $number_of_results_per_page;
}
for ($page_i = 1; $page_i <= $total_pages; $page_i++) {
echo "<a href='__filter_mysql_data.php?page=$page_i'>| $page_i |</a> ";
}
}
So, user enters month (10), script displays all rows where month is 10; displays page No 1. But if user click on other page number, script displays all data from mysql (all months, not only 10)
What I see - when click on other page number, page reloads, that means that values of php variables "dissapears". As understand after page reload $date_month value is not set (has lost/"dissapears") ? How to keep the value? Or may be some better solution for pagination?
Update.
Behavior is following:
1) in input field set month 10 (October);
2) click on button and get displayed data from mysql where month is 10 (October); so far is ok
3) click on page number 2 and get displayed all data from mysql (all months, not only 10 (October))
Tried to use LIMIT however it does not help.
Possibly problem is related with this code
for ($page_i = 1; $page_i <= $total_pages; $page_i++) {
echo "<a href='__filter_mysql_data.php?page=$page_i'>| $page_i |</a> ";
}
When click on $page_i for not understandable (for me) reasons get displayed all (not filtered) data from mysql. If I use LIMIT also get displayed not filtered results from mysql....
Use LIMIT in your query, have a look at the following example , you can try something like :
SELECT
*
FROM
2_1_journal
WHERE
RecordMonth = ?
LIMIT [start_row],[number_of_rows_per_page]
So for example lets say you have 2 pages with 10 rows per page then the LIMIT for the 2 queries (one per page) will be:
1) LIMIT 0,10
2) LIMIT 10,10
You should change your query like this:
SELECT * FROM 2_1_journal WHERE RecordMonth = ? LIMIT = $number_of_results_per_page OFFSET ($page_number*$number_of_results_per_page);
Here you need to calculate the page number also and you should use the limit and offset concept of mysql.

How to hide the next button when no pages to dispaly

Can someone help me please? I am sure it is easy for you guys. I am battling to find a solution on how to hide the next link when there are no pages to display my code is as follows:
if (!isset($_GET['page']) or !is_numeric($_GET['page'])) {
$page = 0;
} else {
$page = (int)$_GET['page'];
}
$pages_query=mysql_query ("SELECT COUNT * FROM hardware");
$result = mysql_query("SELECT * FROM hardware LIMIT $page, 3");
echo 'Next<p>';
$prev = $page - 3;
//only print a "Previous" link if a "Next" was clicked
if ($prev >= 0) {
echo 'Previous';
}
You can use mysql_num_rows($result) to get the number of records in hardware:
$result = mysql_query("SELECT * FROM hardware LIMIT $page, 3");
$record_count = mysql_num_rows($result);
if ($record_count > 1)
echo 'Next';
in your if statement check if the $page is greater than 0 then according to the outcome of the value of $page write your code. you can use another if statement in the first if statement and make it detect the situation and decide what to do. The other thing is if the user clicked next then the user is on the second page so your previous should appear if $prev is higher than 1 it should make it
something along the lines of:
$itemsPerPage = 3;
$sql = "SELECT * FROM hardware";
$result = mysql_query($sql);
$count = mysql_num_rows($result);
$pageCount = $count/$itemsPerPage;
if($pageCount > $page) { //Are there more pages worth of items stored, than we're currently looking at?
echo 'next';
}
You want to be using OFFSET in your SQL syntax, as well as LIMIT.
LIMIT limits the number of rows returned.
OFFSET tells it to start a number of rows into the result set.
You need to limit to the number of items you want on a page. and offset by that number*page.
Hopes this helps.

PHP Paging in Mysql_Query

So basically, this is how my paging query looks like at the moment:
$limit = mysql_escape_string($_GET['pagenumber']);
if (empty($limit)){
$limit = "1";
}
$page = $limit * 10;
$flim1 = $page / 10;
$flim = $page - 10;
At the end of my query, I have this:
... LIMIT $flim, $page
Which should be from 1, 10 if the pagenumber is 1 and 10, 20 if the page number is 2. It works on the first page perfectly, but when I get to the second, there were 20 results, although when I echoed $flim and $page, they were 10 and 20. I cannot understand why!
I've heard about some double-query for paging. If you know a simple way to do this that is kind of like this one, please post a link. Will my method work?
Please see specification of LIMIT there is LIMIT offset,count
So in your situation it would be LIMIT $flim,10
Full code:
$page = mysql_escape_string($_GET['pagenumber']);
if (empty($limit)){
$page = "1";
}
$items_per_page=10;
$offset = ($page-1)*$_items_per_page;
Then LIMIT $offset,$items_per_page
Additionally you would need count for all items to not get above max page

Categories