So I wanna display the row number (ranking) before every record.
Now the code below shows correct on the first page. 1 thru 25, but then I go the second page then a 6 is added before the row number. Should be 26 thru 50 but instead it's 626 thru 650. Would be correct if the number 6 werent there. And so it goes on, on page 3 its 1251 thru 1275 (should be 51 thru 75). Whats wrong here?
$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
$perPage = 25;
$offset = ($page - 1) * $perPage;
$query "SELECT name, exp FROM people ORDER BY exp DESC LIMIT $offset, $perPage";
<? $i = (($perPage * $offset) +1); foreach($db->query($query) as $row): ?>
Ranking: <?=$i?>
Name: <?=$row['name']?>
<? $i++; endforeach ?>
Your $i calculation is wrong. You have already used $perPage in offset calculation. Why using it again?
$i = (($perPage * $offset) +1);
should be
$i = $offset + 1;
Demonstration for fun
Related
So I have this huge file that has 45K+ arrays and I can't just open a huge file on live server with high traffic from every request so I used array_chunk($array, 1000) and saved them in 46 files.
Now I want to read those files when specific page is accessed.
Problem?
$offset seems to be working fine with some pages but mostly offset changes to - number (minus number). Checked page 25, 50, 75 and more...
My Math is kinda (Very) weak, so any help will be appreciated. Thanks!
<?php
$page = ! empty( $_GET['page'] ) ? (int) $_GET['page'] : 1;
$limt = 40; //Page array/item limit
$fcnt = 46; //Total number of files
$tarr = 45187; //Total number of arrays
$fmu0 = (int)($limt*$fcnt*$page/$tarr); //Getting the file number according to page number
if(file_exists("$fmu0.json")){
$id = json_decode(file_get_contents("$fmu0.json"),true);
//Each file has 1000 arrays except the last file which has 187 arrays
$tpgs = ceil($tarr/$limt); //Total number of pages
$mult = $fmu0*count($id);
$offset = ($page - 1) * $limt-$mult;
if( $offset < 0 ){$offset = 0;}
$id = array_slice( $id, $offset, $limt );
var_dump($id);
}
?>
1000 objects per file and 40 objects per page makes 25 pages per file.
Here's how to find the file containing objects for $page number:
$fmu0 = floor($page / 25);
And here's how to find the starting index of the group of 40 ($limt) objects within that file corresponding to $page number, when the first page is 1:
$offset = (($page - 1) * $limt) - ($fmu0 * 1000);
<?php
$page = (!empty($_GET['page']) && 0 < $_GET['page'])
? (int)$_GET['page']: 1;
$limt = 40; //Page array/item limit
// FIND FILE, 25 pages per file
$fmu0 = floor($page / 25);
if(file_exists("$fmu0.json")){
$id = json_decode(file_get_contents("$fmu0.json"),true);
// FIND GROUP of 40 ($limt) page objects, 1000 objects per file
$offset = (($page - 1) * $limt) - ($fmu0 * 1000);
$id = array_slice( $id, $offset, $limt );
var_dump($id);
}
?>
I am fetching username and Id from the database. I have more than 500 usernames. I have to display pagination number only 1 to 5 and last pagination number.
Example:- pagination number is:- 1 2 3 4 5...20(last number).
Now I am getting all numbers in horizontal. Would you help me in this?
Can anyone help me with NEXT and LAST in pagination?
include('../db/connection.php');
$reclimit = 3;
if(isset($_GET['page'])){
$page = $_GET['page'];
} else {
$page = 1;
}
$start = (($page-1) * $reclimit);
$sql = "SELECT * FROM request";
$records =$conn->query($sql);;
$total = $records->num_rows;
$tpages = ceil($total / $reclimit);
$search_sql="SELECT * FROM request LIMIT ".$start."," .$reclimit;
$search_result = $conn->query($search_sql);
HTML
<body>
<?php
if (isset($search_result->num_rows) > 0) {
?>
<h2 class="result-title">Results matching your need</h2>
<?php
while($search_ok = $search_result->fetch_assoc()) {
$user_id=$search_ok['Id'];
$user_name=$search_ok['Name'];
echo "
<div class='search-section'>
<div class='search-profile'>
<div class='s_user_id'>{$user_id}</div>
<div class='s_user_name'>{$user_name}</div>
</div>
</div>
";
}}
for($i=1;$i<=$tpages;$i++) {
echo "".$i."";
}
?>
</body>
The pagination which you are using is simple and working one. But the pagination which you are searching is smart way and you should achieve this by using some if conditions. Similar answer are there in SO. Go to the following, this may help you
Smart pagination algorithm
PHP pagination
Limit pagination page number
It's a simple idea, but I didn't test it. Edit foreach displaying numbers:
$pgStart = 1;
if (isset($_GET['page'])) { // get first showing number = current page - 2
$pg = $_GET['page'] - 2;
$pgStart = $pg + 5 > $tpages ? $tpages - 4 : $pg; //EDIT fix when reach pages end
$pgStart = $pg < 1 ? 1 : $pg; // This must be after ending correction (previous line)
}
if ($pgStart > 1) { // show 1
echo '1 ... ';
}
for($i = $pgStart; $i <= $tpages && $i < $pgStart + 5; $i++) { // show 5 pages
echo ' '.$i.' ';
}
if ($i < $tpages) { // show last
echo ' ... '.$tpages.'';
}
EDIT
Output of this script with $_GET['page'] = 7 and $tpages = 20 from php sandbox is (without linebreaks):
1 ...
5
6
7
8
9
... 20
Here is my index.php code
My problem is my pagination has a continuous number of
pages.
<?php
$limit = 10;
$offset = (isset($_GET["page"]) ? $_GET["page"] - 1 : 0) * $limit;
$query = "SELECT * FROM employee ORDER BY employee_datecommenced ASC LIMIT
$offset,$limit ";
$list = getdata_inner_join($query);
?>
<?php
$total = $dbcon->query("SELECT count(*) FROM employee") or
die(mysqli_error());
$fetch = $total->fetch_assoc();
for($x = 0; $x < $fetch["count(*)"] / $limit ; $x ++){
$page = $x + 1;
if((isset($_GET["page"]) ? $_GET["page"] : 1) == $page)
$page = "<b>".$page."</b>";
echo ''.$page.' ';
}
?>
Click here to see the output photo
Thanks in advance
You say "My problem is my pagination has a continuous number of pages", but why is that a problem? What do you need to change?
in your for loop while printing the anchor tags, check the current page, if suppose the page is ($current), disable ($current-1), and print $current to ($current+5) and '....' and (current+1), if current is $min or $max manage the prev and next
Is it possible to show all results in one page, of Joomla's component "Smart search"?
I'm using Joomla 3.x but at the bottom of the results page there is a standard pagination showing "Previous 1 2 3 4 Next". I want to show all the results on the first page, so there is no need for pagination.
I have already overridden the Smart Search but I'm struggling with this code:
// Prepare the pagination string. Results X - Y of Z
$start = (int) $this->pagination->get('limitstart') + 1;
$total = (int) $this->pagination->get('total');
$limit = (int) $this->pagination->get('limit') * $this->pagination->pagesTotal;
$limit = (int) ($limit > $total ? $total : $limit);
$pages = JText::sprintf('COM_FINDER_SEARCH_RESULTS_OF', $start, $limit, $total);
?>
<br id="highlighter-start" />
<ul class="search-results<?php echo $this->pageclass_sfx; ?>">
<?php
for ($i = 0, $n = count($this->results); $i < $n; $i++):
$this->result = &$this->results[$i];
$layout = $this->getLayoutFile($this->result->layout);
?>
<?php echo $this->loadTemplate($layout); ?>
<?php
endfor;
?>
</ul>
(template/html/com_finder/search/default_results.php)
It does calculate the total amount of results, so is it possible to just show that instead of the for loop?
I have a news table in MySQL Database I query the table and show the title of the news in one PHP pages but now the table getting bigger so I want to divide the results into pages I mean to show each 50 news title in one page (pagenation).
I use this query to bring the news :
SELECT news.*, categories.category, users.username
FROM news INNER JOIN
users on news.user_id = users.id INNER JOIN
categories on categories.id = news.category_id
order by news.timestamp DESC
limit $min,$max
and this is part of the PHP page (How I calculate the max and min)
$news_per_page = 50;
if(!empty($_GET['p_n']))
{
$p_n = $_GET['p_n'];
$max = $news_per_page*$p_n;
$min = ($max - $news_per_page);
}
else
{
$p_n = 1;
$max = 50;
$min = 0;
}
$news = all_news($max,$min);
The sql giving me wrong result when i pass the limits I do not know why. Is it wrong to specify the max and min of sql query by this way?? Should I correct something in this code?
The LIMIT clause, as explained in the docs, takes arguments offset and count. So if you want to get, for example, results from 201 to 250, you would use LIMIT 200, 50. Start by renaming your variables $min and $max to $offset and $count, and from there everything will fall into place.
Pseudocode:
offset = (requestedPageNumber - 1) * rowsPerPage;
count = rowsPerPage;
PHP Code:
(assuming page number is 0-based)
$rowsPerPage = 50;
$page = empty($_GET['p_n']) ? 0 : $_GET['p_n'];
$offset = $rowsPerPage * (int) $page;
$news = all_news($offset, $rowsPerPage);
If you've got problems handling pagination properly, I suggest you use some code that is working, for example a pagination class that takes three parameters:
The current page.
The total count.
The number of items per page.
And then that class will generate the LIMIT clause for you. Example:
$pageNumber = 1;
$totalCount = 17;
$perPage = 5;
$pagination = new LimitPagination($pageNumber, $totalCount, $perPage);
echo $pagination, "\n";
This would output
LIMIT 0, 5
because you'er on the first page. Such a class then could also filter out those problems you have here, for example setting to a negative page - just automatically. And also it can provide a lot of extra data, like the next and previous page, the current page, the number of total pages, if it is the first or the last page and what not:
$pagination->setPage(-2);
echo $pagination, "\n";
echo "Current: ", $pagination->getPage(),
"; Total: ", $pagination->getTotalPages(),
"; Previous: ", $pagination->getPreviousPage(),
"; Next: ", $pagination->getNextPage(),
"\n";
Output:
LIMIT 0, 5
Current: 1; Total: 4; Previous: 1; Next: 2
This is then easy to integrate with different code, including yours:
$pagination = new LimitPagination($_GET['p_n'], $totalCount, 50);
$limit = sprintf("%d, %d", $pagination->getOffset(), $pagination->getCount());
This should easily do it. Class is here as Gist: https://gist.github.com/4469154
You need to set start (this you can achieve by using current page and per page property) and the second information is how many results you want (again per page property).
LIMIT . ($p_n - 1) * $news_per_page .', ' . $news_per_page
So, in your script it will be:
if(!empty($_GET['p_n']))
{
// You need to protect your variables for SQL injection. For numbers (int) or integer() function it is enough.
$p_n = (int) $_GET['p_n'];
$max = (int) $news_per_page;
$min = (int) ($p_n - 1) * $news_per_page;
}
else
{
$p_n = 1;
$max = 50;
$min = 0;
}
The correct code is:
$news_per_page = 50;
if(!empty($_GET['p_n']))
{
$p_n = intval($_GET['p_n']);
$min = ($p_n-1) * $news_per_page;
$max = $news_per_page;
}
else
{
$p_n = 1;
$max = 50;
$min = 0;
}
$news = all_news($max,$min);
While $_GET['p_n'] is page_number you dont' need to make any multiplies