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?
Related
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
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
I am, using the Codeigniter pagination library,
I am wondering how I can grab the number of the first and last items displayed using the pagination class?
So, if I had 12 results, and per_page was set to 5. I would want
Page 1: Displaying 1 to 5 of 12 results
Page 2: Displaying 6 to 10 of 12 results
Page 3: Displaying 11 to 12 of 12 results.
Keeping it simple.
You need 3 variables. Result start, result end (in the page, not the whole) and total result.
You already know the total results (from the pagination). Let's call it $total.
so, now get the current page ($curpage) value from CI instance. Then,
$result_start = ($curpage - 1) * $per_page + 1;
if ($result_start == 0) $result_start= 1; // *it happens only for the first run*
for $result_end, you just need to add the per page value but considering it'll be 1 less,
$result_end = $result_start+$per_page-1;
if ($result_end < $per_page) // happens when records less than per page
$result_end = $per_page;
else if ($result_end > $total) // happens when result end is greater than total records
$result_end = $total;
send all those 3 values to view.
echo "displaying $result_start to $result_end of $total";
itachi's solution works great except you need to handle the case where the last page has less than $per_page elements.
if ($result_end > $total) {
$result_end = $total;
}
In CodeIgniter Controller file
$params['limit'] = 15;
$params['offset'] = ($this->input->get('per_page')) ? $this->input->get('per_page') : 0;
$config = $this->config->item('pagination');
$config['per_page'] = $params['limit'];
$config['base_url'] = site_url('cy_controller/action?');
$config['total_rows'] = $this->abcd_model->get_count($params);
$this->pagination->initialize($config);
$data['merchant'] = $this->abcd_model->get_all($params);
$countMerchant = count($data['merchant']);
if ($params['offset'] == 0) {
$find_total_record = $countMerchant;
} else {
$valuec = $params['offset'] + $params['limit'];
if ($valuec > $config['total_rows'])
$find_total_record = $params['offset'] + $countMerchant;
else
$find_total_record = $params['offset'] + $params['limit'];
}
$per_page_total = $find_total_record;
$initial = $params['offset'] == 0 ? 1 : $params['offset'];
$data['showing'] = "Showing " . $initial . " to " . $per_page_total . " of " . $config['total_rows'] . " results";
$data['_view'] = 'cy_controller/action';
$this->load->view('layouts/main', $data);
And in view file
<div class="pull-left">
<?php echo $showing; ?>
</div>
I have an 300 rows(data) each page contain 10 records I need first 10 pages then click next then display next 10 page here is my code:
<?php
$eu = ($start - 0);
$limit = 10;
$this1 = $eu + $limit;
$back = $eu - $limit;
$next = $eu + $limit;
if ($nume > $limit)
{
echo'<div class="pagination right">';
if ($back >= 0)
{
echo"<a href='$page_name?start=$back&o" . $_SESSION['clicked'] . "&p=$desc'>«</a>";
}
$i = 0;
$l = 1;
for ($i = 0; $i < $nume; $i = $i + $limit)
{
if ($i <> $eu)
{
echo"<a href='$page_name?start=$i&o=" . $_SESSION['clicked'] . "&p=$desc'>$l</a>";
}
else
{
echo "<a class='active'>$l</a>";
}
$l = $l + 1;
}
if ($this1 < $nume)
{
echo "<a href='$page_name?start=$next&o=" . $_SESSION['clicked'] . "&p=$desc'>»</a>";
}
echo '</div>';
}
echo '</div></div>';
?>
Then I will get response like this http://app.ologie.us/app/admin/Screen.png
Can any one please guide to effective paging in PHP.
Here's a tutorial on basic pagination.
http://www.phpfreaks.com/tutorial/basic-pagination
Doing it like Tim said is, according to me, mainly a bad way to do this. Read in the data needed WHEN it's needed.
You could do it with a min - max index to your array.
Consider you want to display 10 records per page:
$min = $page * 10; //Page is the current page
$max = $min + 10;
Then you just loop through the data between the min - max indexes.
for($i = $min; $i < $max; $i++) {
//Echo your data like normal
}
If you're gathering your data from MySQL, you might want to look up LIMIT.
If $limit is your page size and you add another request variable $page - representing the search result page requested by the user - you can use
$start = ($page-1)*$limit;
for($i=$start;$i < $start + $limit;$i=$i+1) {
...
}
to iterate over a "page" of the results.
But of course it does not make sense to handle paging on the level of the result view. You want to limit the number of results you get from the query.
In addition I would really consider using a PHP framework rather than coding it in the way you present in your request. Chances are your application will end up to be difficult to maintain.
You do it all client side with the DataTables plugin for jQuery. This plugin also gives you a LOT more options like sorting and searching.
http://datatables.net/
If you have a VERY large table, this probably isn't a good idea, though.