I am a working with php and mysql and I managed to do pagination following this tutorial:
http://www.phpfreaks.com/tutorial/basic-pagination
Here is "mywebpage" (on work): http://ada.uprrp.edu/~ehazim/hpcf_proj/miejemplo.php
But Now, I want to make it "pretty" using bootstrap Pagination:http://getbootstrap.com/components/#pagination
I am using _GET['current_page'] to get the page where I am. The problem is that I dont know how to change the echo's to echo the pagination from bootstrap... Yes, it may be stupid, but It is my first time with php and I am like 2 hours just trying to do this. Can someone help me? Below is the code that I have, following the tutorial of php freaks (which I understand, except for some echos with toomany quotes :/ ):
<div class="pagination">
<ul>
<?php
/****** build the pagination links ******/
// if not on page 1, don't show back links
if ($currentpage > 1) {
// show << link to go back to page 1
# TRYING TO CHANGE HERE AND IN OTHER ECHOS
#echo "<li>«</li>";
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'><<</a> ";
// get previous page num
$prevpage = $currentpage - 1;
// show < link to go back to 1 page
#echo " <li><a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'><</a> ";
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'><</a> ";
} // end if
# range of num links to show
$range = 3;
# loop to show links to range of pages around current page
for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
// if it's a valid page number...
if (($x > 0) && ($x <= $totalpages)) {
// if we're on current page...
if ($x == $currentpage) {
// 'highlight' it but don't make a link
echo " [<b>$x</b>] ";
// if not current page...
}
else {
// make it a link
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> ";
} // end else
} // end if
} // end for
// if not on last page, show forward and last page links
if ($currentpage != $totalpages) {
// get next page
$nextpage = $currentpage + 1;
// echo forward link for next page
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'>></a> ";
// echo forward link for lastpage
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'>>></a> ";
} // end if
# end build pagination links
?>
</ul>
</div>
To get you started, Bootstrap pagination works on a ul of class = pagination with the page links as list items.
Towards the start of your php code add the pagination class to the ul (not the div)
<ul class="pagination">
Then wherever you echo a page link, wrap it with li tags, e.g.
echo " <li><a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> </li>";
EDIT
You also want to tweak the HTML for the current page by changing
echo " [<b>$x</b>] ";
to
echo " <li>$x</li> ";
Edit
If you want to center your pagination bar, Bootstrap 3 has a the text-center class which you can use. See http://jsfiddle.net/panchroma/8RHzw/
Change the first line of your php to
<div class="text-center">
With Bootstrap 2, use
<div style="text-align:center;">
Hope this helps!
Further to David Taiaroa's answer above, for the active button state you need to add the class="active" and A tags.
echo " <li>$x</li> ";
..becomes..
echo " <li class=\"active\"><a>$x</a></li> ";
Related
I have just changed my website to be dynamic switching from html to php for a lot of my pages. In doing so I have lost a scroll effect I was using in jquery. The function allowed the user to click on the appropriate link on the header and it would scroll to the class linked to that button. In making the page dynamic each div on the page now has the same id. I was wondering if there was a way to set the buttons to scroll to specific pixels, or potentially using nth child?
function ScrollTo(id, speed)
{
$('html, body').animate({
scrollTop: $(id).offset().top
}, speed);
return false;
}
<div class="header-case2">
<a class="case2" onclick="ScrollTo('.case-div-1', 599)">Technology</a>
<a class="case2" onclick="ScrollTo('.case-div-2', 599)">Local Authority Modules</a>
<a class="case2" onclick="ScrollTo('.case-div-3', 599)">Data Channel</a>
<a class="case2" onclick="ScrollTo('.case-div-8', 599)">Housing Solutions</a>
<a class="case2" onclick="ScrollTo('.case-div-4', 599)"> Tenant Engagement</a>
<a class="case2" onclick="ScrollTo('.case-div-6', 599)">H&A Management</ a>
</div>
$query = $handler->query('SELECT * FROM solutions');
$results = $query->fetchAll(PDO::FETCH_ASSOC);
for ($i=0; $i < count($results); $i++) {
echo '<div class="case-div">';
echo '<h2 class="upper-blue">'.$results[$i]['headline'].'<br>'.'</h2>';
echo '<p class="pp13">'.$results[$i]['text'].'<br>'.'</p>';
echo '</div>';
echo '<div class="pa7"></div>';
}
You are missing your increments on your divs -
this can be easily fixed by adding your $i (your index of the for loop) so your divs classes -
echo '<div class="case-div-'.$i.'">';
Or maybe
echo '<div class="case-div-'.$i+1.'">';
since it starts on zero - This will make your onclicks work again.
Hello i have a quick question.
This is the code
<div class="beta-pagination text-center">
<?php
if($page > 1) {
echo '<i class="fa fa-chevron-left"></i>' . "\n";
if($page > 2) {
echo '...';
}
echo ''. ($page - 1) .'';
} else {
echo '<i class="fa fa-chevron-left"></i>' . "\n";
}
?>
<?php echo $page; ?>
<?php echo ($page + 1); ?>
...
<i class="fa fa-chevron-right"></i>
</div>
I use this pagination div on one of my shopping website, and is working great if use this url "www.domain. com/search?q=shoes" website is displaying all of my products from database.
And when i press next page goes to "www.domain. com/search.php?page=2&q=shoes" and is perfect.
But the problem is when i want to display second page of products based on merchant.
For example if i go on www.domain .com/search/search.php?brand=503 it display the results alright, but when press next page button it sends me to www.domain .com/search.php?page=2&q= and i need to go on www.domain .com/search.php?page=2&brand=503
Can someone please help me out with what should i add to my div code? a paste with the new code will be great! Thanks.
You lose 'brand' when making url for href. For common case write such code to save input get string
$query = $_GET;
if(isset($query['page'])) unset($query['page']);
$query = http_build_query($query);
and replace .'&q='. by.'&'.$query.
I have to links Next and Previous in Pagination. What I should do so that 'previous' link should hide when I was on the first page and the 'next' link should hide when I was on the last page?
my code for next and previous links is:
echo '<a href="'.$_SERVER['PHP_SELF'].'?page='
.($page-1).'&date1='.$_REQUEST["date1"].'&date2='.$_REQUEST["date2"].'">Previous</a>';
echo '<a href="'.$_SERVER['PHP_SELF'].'?page='
.($page+1).'&date1='.$_REQUEST["date1"].'&date2='.$_REQUEST["date2"].'">Next</a>';
It wont work well the way your doing it right now, unless you have the specific number of pages.
If you have the total number of pages, you can do this
if( $page > 1 ) {
echo '<a href="'.$_SERVER['PHP_SELF'].'?page='
.($page-1).'&date1='.$_REQUEST["date1"].'&date2='.$_REQUEST["date2"].'">Previous</a>';
}
if( $page < $totalPages ) {
echo '<a href="'.$_SERVER['PHP_SELF'].'?page='
.($page+1).'&date1='.$_REQUEST["date1"].'&date2='.$_REQUEST["date2"].'">Next</a>';
}
I've followed the documentation here (at the bottom) to create next and back buttons at the bottom of my page.
It seems to work fine until I get to the last page where the link just redirects me back to the first page. Is there a way to say if there isn't a next page to not show the link? I assumed thats what the if statement was supposed to do!!
<?php
$pagelist = get_pages('sort_column=menu_order&sort_order=asc');
$pages = array();
foreach ($pagelist as $page) {
$pages[] += $page->ID;
}
$current = array_search(get_the_ID(), $pages);
$prevID = $pages[$current-1];
$nextID = $pages[$current+1];
?>
<?php if (!empty($prevID)) { ?>
<a class="back" href="<?php echo get_permalink($prevID); ?>">BACK</a>
<?php } ?>
<?php if (!empty($nextID)) { ?>
<a class="next" href="<?php echo get_permalink($nextID); ?>">NEXT</a>
<?php } ?>
p.s Please don't move my question to the Wordpress Stack - that seems to be dying a bit of a death and doesn't get many responses!
My Pages are setup like this:
Parent page
Sub page 1
Sub page 2
Sub page 3
I've created a link on the parent page to goto the first subpage. Then on the subpage template I've got the code above. I just want the next link to appear on each page then when it gets to page 3 it shouldn't show the next link.
If you are saying that this is effectively looping around then $nextID must never be empty, which would be why the link was always displayed.
You could set a $firstID, ie
$firstID = pages[0];
and then check;
if ($firstID != $nextID ) {
// Display link
}
I am having some issues trying to create page navigation using php,
I have variable called $PageNo that I can navigate through using using next prev links -1 or +1.
eg.
echo "<a href='http://".$_SERVER["HTTP_HOST"]."/product.php?page=".($PageNo+1)."'>Next</a>";
echo "<a href='http://".$_SERVER["HTTP_HOST"]."/product.php?page=".($PageNo-1)."'>Prev</a>";
but aswell as this is want to display direct links to the pages so i have a navigation like so
PREV 1 2 3 4 NEXT
echo "<a href='http://".$_SERVER["HTTP_HOST"]."/product.php?page=".($PageNo+1)."'>Next</a>";
echo "<a href='http://".$_SERVER["HTTP_HOST"]."/product.php?page=1'>1</a>";
echo "<a href='http://".$_SERVER["HTTP_HOST"]."/product.php?page=2'>2</a>";
echo "<a href='http://".$_SERVER["HTTP_HOST"]."/product.php?page=3'>3</a>";
echo "<a href='http://".$_SERVER["HTTP_HOST"]."/product.php?page=4'>4</a>";
echo "<a href='http://".$_SERVER["HTTP_HOST"]."/product.php?page=".($PageNo-1)."'>Prev</a>";
If I know the total number of product pages is 4 how would you generate the links to give
echo "<a href='http://".$_SERVER["HTTP_HOST"]."/product.php?page=1'>1</a>";
echo "<a href='http://".$_SERVER["HTTP_HOST"]."/product.php?page=2'>2</a>";
echo "<a href='http://".$_SERVER["HTTP_HOST"]."/product.php?page=3'>3</a>";
echo "<a href='http://".$_SERVER["HTTP_HOST"]."/product.php?page=4'>4</a>";
Any help would be great.
How about to try this one?
// $total_num : total number of the pages
foreach (range(1, $total_num) as $p) {
echo "<a href='http://".$_SERVER["HTTP_HOST"]."/product.php?page=$p"'>$p</a>";
}
You need to do the math, and then a little bit of code.
calculate the number os pages you need (based on the number os records and records per page)
Just use a for loop to do links like (not tested):
for ($page = 1; $page <= $total_pg; ++$page) {
echo "$page";
}
then, the product.php page reads that number and displays the subset of records
This is a simple for loop, and it's very basic stuff that's in all programming languages out there (just like if/else statements and while loops among others).
Say the total number of pages is 4, then you set a random variable, say $p (for pages) initially to it's start value 1, then continue the loop, increasing $p by 1 every time until $p is 4. Would result in this:
// Previous link before the numbers (unless we are on page 1)
if($PageNo > 1) {
echo "<a href='http://".$_SERVER["HTTP_HOST"]."/product.php?page=".($PageNo-1)."'>Prev</a>";
}
// We loop over all the numbered pages here
for($p = 1;$p <= 4;$p++) {
echo "<a href='http://".$_SERVER["HTTP_HOST"]."/product.php?page=" . $p . "'>" . $p . "</a>";
}
// Next link goes after the numbers (if there are any pages left)
if(($PageNo + 1) <= $p) {
echo "<a href='http://".$_SERVER["HTTP_HOST"]."/product.php?page=".($PageNo+1)."'>Next</a>";
}