Show Query Results with WordPress - php

I have been searching with no luck so sorry if this has been answered before.
I am wanting to show the results of the query with something like " Viewing 1-10 of 100" basically showing the current posts I am viewing and the total post count. If someone can point me in the right direction I would appreciate it very much.

You can insert the following code in the file search.php or anywhere you need.
<?php
function get_number_of_results( $current_page, $total, $jumps = 10){
$first_result = 1;
$start = $first_result;
$end = $jumps;
if($current_page >=2) {
$previous = $current_page - 1;
$first_result = ($previous * $jumps) + 1;
$start = $first_result;
$end = $start + ($total - 1);
}
return "$start - $end";
}
$current_page = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$total = $wp_query->post_count;
// Result
echo "Viewing " . get_number_of_results( $current_page, $total);
?>
Notes
You can move the function to the file functions.php to just call the two las lines of my example.
The line: <?php $total = $wp_query->post_count; ?> retrieve the number of total results.
The variable $current_page retrieve the current page of the results (more info).
The function can jump between the number of pages you only need to specify as third parameter.

Related

how to fix pagination continuous number of page IN PHP

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

joomla smart search show all results

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?

Can not send levels to switch pages

Am working on array pagination. I am having some trouble forming equations. what I want is when
$CurrentPage = 1 then $Start = 1,
if $CurrentPage = 2 then $Start = 30,
if $CurrentPage = 3 then $Start = 60,
if $CurrentPage = 4 then$Start = 90
and so on..
How do I write the if else block ?
you can also use this:
$Start = ($currentPage==1 ? 1 : ($currentPage-1)*30);
and forget all the if/else.
If the current page is one, display from result one (for what ever reason not from result 0), if page bigger than one display from result (page - 1)*30
if ((int)$currentPage > 1) {
$start = ($currentPage - 1)*30;
}
else {
$start = 1;
}
or in a shorter way
$start = ($currentPage > 1) ? ((int)$currentPage - 1) * 30 : 1;
Keep it simple:
$Start = max(1, ($currentPage-1) * 30);

PHP Pagination - having trouble detecting pages

I want to make a minimalistic pagination script that basically does three things:
On first page, just a next button.
On last page, just a previous button.
For all others in between, both.
I have most of the code worked out, but I'm just making some if/elseif statements that determine which page the user is on and I'm having a bit of trouble. (those at bottom) First, here's the query code:
$per_page = 10;
$pages_query = mysql_query("SELECT COUNT(idnum) FROM images");
$pages = ceil(mysql_result($pages_query, 0) / $per_page);
$page = (isset($_GET['page'])) ? (int)$_GET['page'] :1;
$start = ($page -1) * $per_page;
$query = mysql_query("SELECT * FROM images ORDER BY idnum DESC LIMIT $start, $per_page");
And here's the if statement part:
$nextend = $pages - 1;
$next = $page + 1;
$previous = $page - 1;
if ($pages >= 1 && $page = 1) {
echo 'next';
} elseif ($pages >= 1 && $page = 2) {
echo 'previous';
}
It always results in the next button, no matter what page I'm on. How do I detect the page number so I can display the pagination buttons the way I want to? By the way, I know I don't have the else statement for the middle pages (next and previous) yet.
You are assigning in your if statements rather than comparing. You don't want this in your if statement:
$page = 1
That just assigns 1 to $page.
You want this:
$page == 1
Or this:
$page === 1
$nextend = $pages - 1;
$next = $page + 1;
$previous = $page - 1;
$maxpages = ? //You need to have a variable with the last page number
if ($pages > 1) {
echo 'previous';
}
if ($page < $maxpages) {
echo 'next';
}
I don't understand what logic you are trying to do with your if/else statements, also when checking if a variable is equal to a number/other variable your "==" not "="
What you've got to do is way simple:
If you're not on the first page, you always have to show a back button.
If you're not on the last page you always have to show a next button.
Please note that both those conditions can happen at the same time so using an elseif between them won't work as it will only allow one of them to execute.
Example:
if ( $page > 1 )
{
echo( "Previous" );
}
if ( $page < $pages )
{
echo( "Next" );
}

PHP: Trying to come up with a "prev" and "next" link

I'm displaying 10 records per page. The variables I have currently that I'm working with are..
$total = total number of records
$page = whats the current page I'm displaying
I placed this at the top of my page...
if ( $_GET['page'] == '' ) { $page = 1; } //if no page is specified set it to `1`
else { $page = ($_GET['page']); } // if page is specified set it
Here are my two links...
if ( $page != 1 ) { echo '<div style="float:left" ><a href="index.php?page='. ( $page - 1 ) .'" rev="prev" >Prev</a></div>'; }
if ( !( ( $total / ( 10 * $page ) ) < $page ) ) { echo '<div style="float:right" ><a href="index.php?page='. ( $page + 1 ) .'" rev="next" >Next</a></div>'; }
Now I guess (unless I'm not thinking of something) that I can display the "Prev" link every time except when the page is '1'. How can make it where the "Next" link doesn't show on the last page though?
replace your last line of code with:
if ($page*10 < $total) echo '<div style="float:right" ><a href="index.php?page='. ( $page + 1 ) .'" rev="next" >Next</a></div>';
!( ( $total / ( 10 * $page ) ) < $page )
I ... don't think this is correct.
Suppose $total is 1000 - then on page 99, 1000/990 is less than 99, but there's still another page to show.
What you probably want is to check whether 10 * $page is greater than or equal to $total.
You need the total divided by the pagecount ($total / 10) with an extra page if there is a remainder. Use the ceil function.
$page < ceil($total / 10)
Pagination is something that comes up so often in web projects, that it's usually best to create some kind of class somewhere, or at least some conveniences functions, that give you everything you need.
Here's a function that would return a dictionary of things that might be useful:
function pagination($numberItems, $perPage, $currentPage) {
$numPages = ceil($numberItems/$perPage);
return array(
'numberPages' => $numPages,
'start' => ($currentPage - 1) * $perPage + 1,
'end' => min($currentPage * $perPage, $numberItems),
'hasNext' => $page != $numPages,
'hasPrev' => $page != 1
);
}
You have a mistake in the last line:
$totalPages = ceil( $total / 10 );
if ( $page < $totalPages ) ) { echo "<div style="float:right" ><a href="index.php?page='. ( $page + 1 ) .'" rev="next" >Next</a></div>"; }
// lets get total number of pages here
$itemsPerPage = 10;
$totalItems = 105; // just for example
$pagesNo = ceil( $totalItems / $itemsPerPage );
// get the current page here...
$current = ( isset($_GET['page']) ) ? (int)$_GET['page'] : 1; // if page is defined, use it, instead use 1
$current = ( $current > 0 ) ? $current : 1; // if page was -1,-2... for instance, use 1
// show us 'Prev' button
echo ( $current > 1) ? "<a href='".($current-1)."'>Prev</a>" : "";
// show current page NO
echo $current;
// show 'Next' button
echo ( $current < $pagesNo) ? "<a href='".($current+1)."'>Next</a>" : "";
Should be like this... This is just quick code... Later, work on some page span (set how many pages should be shown before/after current page number...)
Sounds like you need to detect if you're on the first or the last page and "guard" the code against those conditions.
if (/* not the first page */) {
// print the prev link
}
if (/* not the last page */) {
// print the next link
}
You already have the code to detect if page=1 so now you just need to determine if you're on the last page or not (integer division or ceil() should be the way to go).
For pages of PHP results from SQL, there's a neat article on PHPFreaks http://www.phpfreaks.com/tutorial/basic-pagination

Categories