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
Related
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.
We are trying to apply pagination concept on Course Category list page in our MOODLE website and we have got success in it. But while displaying it shows the same course on every page. We are able to set how much number of topics/categories should be shown on each page, and same number of categories are shown. But each page shows the same topic. Please help if somebody has applied pagination into their MOODLE website.
There is no generic way to add pagination to different pages in Moodle - there is a general '$OUTPUT->paging_bar' function, which will generate the output for a paging bar, but it is then up to your own code to decide what to do with the 'page' parameter that is then passed on to the PHP script.
Usually the code looks something like this:
$page = optional_param('page', 0, PARAM_INT);
$perpage = optional_param('perpage', 30, PARAM_INT);
...
$count = $DB->count_records_sql('Some SQL query to count the number of results');
$start = $page * $perpage;
if ($start > $count) {
$page = 0;
$start = 0;
}
$results = $DB->get_records_sql('Some SQL query to get the results', array(parameters for query), $start, $perpage); // Start at result '$start' and return '$perpage' results.
Alternatively, if this is not possible, you can get all the results and then use array_slice:
$page = optional_param('page', 0, PARAM_INT);
$perpage = optional_param('perpage', 30, PARAM_INT);
...
$results = $DB->get_records_sql('Some SQL query to get the results', array(params for query));
$start = $page * $perpage;
if ($start > count($results)) {
$page = 0;
$start = 0;
}
$results = array_slice($results, $start, $perpage, true);
The list of courses within a category are already paginated.
So I'm guessing you mean the list of categories? You can use a flexible table with pagination
http://docs.moodle.org/dev/lib/tablelib.php
You can also display one section of a course by going to course -> edit settings -> Course format -> Course Layout -> show one section per page.
But if you want to display some of the sections - not just one - then you probably need to have a look at designing a course format
http://docs.moodle.org/dev/Course_formats
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.
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.
Is it possible to create pagination without getting all elements of table?
But with pages in GET like /1 /666…
It usually involves issuing two queries: one to get your "slice" of the result set, and one to get the total number of records. From there, you can work out how many pages you have and build pagination accordingly.
A simply example:
<?php
$where = ""; // your WHERE clause would go in here
$batch = 10; // how many results to show at any one time
$page = (intval($_GET['page']) > 0) ? intval($_GET['page']) : 1;
$start = $page-1/$batch;
$pages = ceil($total/$batch);
$sql = "SELECT COUNT(*) AS total FROM tbl $where";
$res = mysql_query($sql);
$row = mysql_fetch_assoc($res);
$total = $row['total'];
// start pagination
$paging = '<p class="paging">Pages:';
for ($i=1; $i <= $pages; $i++) {
if ($i==$page) {
$paging.= sprintf(' <span class="current">%d</a>', $i);
} else {
$paging.= sprintf(' %1$d', $i);
}
}
$paging.= sprintf' (%d total; showing %d to %d)', $total, $start+1, min($total, $start+$batch));
And then to see your pagination links:
...
// loop over result set here
// render pagination links
echo $paging;
I hope this helps.
Yes, using mySQL's LIMIT clause. Most pagination tutorials make good examples of how to use it.
See these questions for further links and information:
How do you implement pagination in PHP?
Searching for advanced php/mysql pagination script
more results
You can use LIMIT to paginate over your result set.
SELECT * FROM comments WHERE post_id = 1 LIMIT 5, 10
where LIMIT 5 means 5 comments and 10 is the offset. You can also use the longer syntax:
... LIMIT 5 OFFSET 10