hello i have a code the next previous and last page are working but i can't get the link for the pages to work where the page will be disabled when you go there . i tried changing some parts but when i tried it there's no error simply all of the page number is disabled
here's my php code the last part only can't fix it tried but i'm still really a beginner
echo "<center>" . $numrows . " search results found</center>";
echo "<center>";
if ($pages >=1 && $page <= $pages) {
if($page == $first_page){
echo "Previous ";
} else{
if(!isset($page)){
echo "Previous ";
}else{
// But if page is set and it's not 1.
$previous = $page-1; echo "<a
href='?page=".$previous."&q=".$searchtext."'>Previous</a> ";
}
}
for ($x=1; $x<=$pages; $x++) {
echo ($x == $page) ? '<strong>'.$x.' </strong>' : ''.$x.' ' ;
}
if($page == $last_page){
echo "Next ";
}else{
// If page is not set or it is set and it's not the last page.
if(!isset($page)){
$next = $first_page+1; echo "<a href='?page=".$next."'>Next</a> ";
}else{
$next = $page+1; echo "<a href='?page=".$next."'>Next</a> ";
}
} echo "<a href='?page=".$last_page."&q=".$searchtext."'>Last
page</a>";
}
echo "</center>";
the
for ($x=1; $x<=$pages; $x++) {
echo ($x == $page) ? '<strong>'.$x.' </strong>' : ''.$x.' ' ;
}
part is the one i use to output the pages don't know the configuration for the isset of that part.
If I understand your question correctly you want the current page number to have no link? If so you could try something like the following.
for ($x=1; $x<=$pages; $x++) {
if ($page <> $x){
echo ($x == $page) ? '<strong>'.$x.'</strong>' : ''.$x.' ';
} else { echo $x.' '; }
}
Related
I am trying to make a pagination for my project,
Im am trying to learn some new skills and decided to start an old project i started a while ago and thought everything was going well and thought i had succeeded...
With the example data I have in my database everything seemed perfect, however when I imported more example data I noticed the pagination tabs were getting very long
currently i have them working as 1|2|3|4|5|6|7|8|9|10
but after i imported more data the list went onto 50+
the PHP code i am using is
<?php
for ($x = 1; $x <= $page_neeed; $x++) {
if ($page==$x){$class="active";}else{$class="";};
echo "<li class='page-item $class'><a class='page-link' href='example.php?page=$x'>$x</a></li>";
}
?>
I have had a look at other sites and decided to have something like 1|2|3|...|50|51|52
For the life of me, I can't get my head around how to approach this.
if you could point me in the right direction that would be fantastic as I don't know what to even search to try to solve this.
After digging around with the links that Tim Lewis gave me in the comments on the OP I soon realised it was more complex than I first thought.
But....
I have managed to get a better pagination than I first had, almost the way I need.
I thought I would post the code here in case someone else could use it.
<?php
$max = $pages_needed-4;
//if we need more than 6 pages
if ($pages_needed > 6) {
//seeing if the current page is between 5 and the last page -4
if (($page >= 5 && $page < $max)) {
$lower_page = $page - 2;
$next_page = $page + 1;
for ($x = $lower_page; $x <= $next_page; $x++) {
if ($page == $x) {
$class = "active";
} else {
$class = "";
}
echo "<li class='page-item $class'><a class='page-link' href='$share_url/category/$category_slug?page=$x'>$x</a></li>";
}
echo "<li class='page-item'><span class='page-link'>...</span></li>";
echo "<li class='page-item $class'><a class='page-link' href='$share_url/category/$category_slug?page=$pages_needed'>$pages_needed</a></li>";
};
//seeing if the current page is near the end
if (($page >= $max && $page < $pages_needed+1)) {
$lower_page = $page - 2;
$next_page = $page + 1;
echo "<li class='page-item'><a class='page-link' href='$share_url/category/$category_slug?page=1'>1</a></li>";
echo "<li class='page-item'><span class='page-link'>...</span></li>";
for ($x = $lower_page; $x <= $pages_needed; $x++) {
if ($page == $x) {
$class = "active";
} else {
$class = "";
}
echo "<li class='page-item $class'><a class='page-link' href='$share_url/category/$category_slug?page=$x'>$x</a></li>";
}
};
//seeing if the current page is near the start
if (($page >= 1 && $page < 5)) {
$lower_page = $page - 2;
$next_page = $page + 1;
for ($x = 1; $x <= 5; $x++) {
if ($page==$x){$class="active";}else{$class="";};
echo "<li class='page-item $class'><a class='page-link' href='$share_url/category/$category_slug?page=$x'>$x</a></li>";
}
echo "<li class='page-item'><span class='page-link'>...</span></li>";
echo "<li class='page-item $class'><a class='page-link' href='$share_url/category/$category_slug?page=$pages_needed'>$pages_needed</a></li>";
};
}
//if we need more less 6 pages
else{
for ($x = 1; $x <= $pages_needed; $x++) {
if ($page==$x){$class="active";}else{$class="";};
echo "<li class='page-item $class'><a class='page-link' href='$share_url/category/$category_slug?page=$x'>$x</a></li>";
}
}
?>
This alows me to have 1|2|3|4|5|6
When on page 5 it goes 3|4|[5]|6|...|30|
On the last 6 pages it gives you 1|...|26|27|28|29|30
I'm pretty sure someone could simplify this but I'm still learning and hope this helps someone else.
I have a strange problem with my navigation next/previous code. Everything works fine but random after I click on the next page, the whole navigation is gone. And when I change the pagina variable from page 4 to 5 for example, does everything work again, but pagina 4 keeps removing the navigation. I don't know why it occurs?
On the top of my page do I have:
if (isset($_GET['pagina']) && $_GET['pagina']!="") {
$page_no = $_GET['pagina'];
}
else {
$page_no = 1;
}
$total_records_per_page = 100;
$offset = ($page_no-1) * $total_records_per_page;
$previous_page = $page_no - 1;
$next_page = $page_no + 1;
$adjacents = "2";
And on the bottom of my page
$taal = htmlspecialchars(addslashes($_GET['taal']));
if($page_no > 1){
echo <a href='".$page."&taal=".$taal."&zoekveld=".$zoekveld."&pagina=".$previous_page."'>Vorige</a>";
}
if ($total_no_of_pages <= 10){
for ($counter = 1; $counter <= $total_no_of_pages; $counter++){
if ($counter == $page_no) {
echo "<a class='active'>".$counter."</a>";
}
else
{
echo "<a href='".$page."&taal=".$taal."&zoekveld=".$zoekveld."&pagina=".$counter."'>".$counter."</a>";
}
}
}
elseif ($total_no_of_pages > 10){
if($page_no <= 4) {
for ($counter = 1; $counter < 8; $counter++){
if ($counter == $page_no) {
echo "<span class='active'><a>".$counter."</a></span>";
}
else {
echo "<a href='".$page."&taal=".$taal."&zoekveld=".$zoekveld."&pagina=".$counter."'>".$counter."</a>";
}
}
echo "<a>...</a>";
echo "<a href='".$page."&taal=".$taal."&zoekveld=".$zoekveld."&pagina=".$second_last."'>".$second_last."</a>";
echo "<a href='".$page."&taal=".$taal."&zoekveld=".$zoekveld."&pagina=".$total_no_of_pages."'>".$total_no_of_pages."</a>";
}
elseif($page_no > 4 && $page_no < $total_no_of_pages - 4) {
echo "<a href='".$page."&taal=".$taal."&zoekveld=".$zoekveld."&pagina=1'>1</a>";
echo "<a href='".$page."&taal=".$taal."&zoekveld=".$zoekveld."&pagina=2'>2</a>";
//echo "<li><a>...</a></li>";
for (
$counter = $page_no - $adjacents;
$counter <= $page_no + $adjacents;
$counter++
) {
if ($counter == $page_no) {
echo "<span class='active'><a>".$counter."</a></span>";
}else{
echo "<a href='".$page."&taal=".$taal."&zoekveld=".$zoekveld."&pagina=".$counter."'>".$counter."</a>";
}
}
echo "<a>...</a>";
echo "<a href='".$page."&taal=".$taal."&zoekveld=".$zoekveld."&pagina=".$second_last."'>".$second_last."</a>";
echo "<a href='".$page."&taal=".$taal."&zoekveld=".$zoekveld."&pagina=".$total_no_of_pages."'>".$total_no_of_pages."</a>";
}
else {
echo "<a href='".$page."&taal=".$taal."&zoekveld=".$zoekveld."&pagina=1'>1</a>";
echo "<a href='".$page."&taal=".$taal."&zoekveld=".$zoekveld."&pagina=2'>2</a>";
echo "<a>...</a>";
for (
$counter = $total_no_of_pages - 6;
$counter <= $total_no_of_pages;
$counter++
)
{
if ($counter == $page_no) {
echo "<span class='active'><a>".$counter."</a></span>";
}
else{
echo "<a href='".$page."&taal=".$taal."&zoekveld=".$zoekveld."&pagina=".$counter."'>".$counter."</a>";
}
}
}
}
if($page_no < $total_no_of_pages) {
echo "<a href='".$page."&taal=".$taal."&zoekveld=".$zoekveld."&pagina=".$next_page."'>Volgende</a>";
}
$taal is language,
$page_no is which page where I'm on
$zoekveld is search field to search on
$pagina is pagenumber in url
I think it has something to do with my GET search field (Zoekveld) but why occurs it after a couple of pages?
Thanks in advance
I wonder how to easily make pagination in PHP with MySQL.. May I ask you a favor of you?
This my code have not error... take this code with your project implement..
<ul class="pagination justify-content-end">
<?php
$sql = "SELECT * FROM json_data";
$rs_result1 = mysqli_query($connect, $sql);
$row = mysqli_num_rows($rs_result1);
//print_r($rs_result1); die();
$limit = 10;
$total_records = $row;
//$total_pages = ceil($total_records / $limit)-220;
// calculate total pages
$total_pages = ceil($row / $limit);
$prev = $page-1;
$next = $page+1;
$pagination_buttons =5;
$last_page = $total_pages;
$half = floor($pagination_buttons/2);
//echo '<ul class="pagination">';
if($page >= 5){
echo '<li>First</li>';
}
if($page < $pagination_buttons AND ($last_page == $pagination_buttons OR $last_page > $pagination_buttons)){
for($i=1; $i<=$pagination_buttons; $i++){
if($i == $page){
echo '<li class="active">'.$i.'</li>';
}
else{
echo '<li>'.$i.'</li>';
}
}
if($last_page > $pagination_buttons){
echo '<li>Next</li>';
}
}
else if($page >= $pagination_buttons AND $last_page > $pagination_buttons){
if(($page+$half) >= $last_page){
echo '<li>Previous</li>';
for ($i=($last_page-$pagination_buttons)+1; $i<=$last_page; $i++) {
if($i == $page){
echo '<li class="active">'.$i.'</li>';
}
else{
echo '<li>'.$i.'</li>';
}
}
}
else if(($page+$half) < $last_page){
echo '<li>Previous</li>';
for ($i=($page-$half); $i<=($page+$half); $i++) {
if($i == $page){
echo '<li class="active">'.$i.'</li>';
}
else{
echo '<li>'.$i.'</li>';
}
}
echo '<li>Next</li>';
}
}
if($page != $total_pages && $total_pages >= 6){
echo '<li>Last</li>';
} ?>
i solve it my code in pagination in php with mysql.. and hope may help this code your pagination project...
I coded pagination code for my site in php and mysql.. I wonder how to easily make pagination in PHP with MySQL.. May I ask you a favor of you? This my code have not error... take this code with your project implement..
Help me, i was writing my code, all done succesfully but my problem is when the "..." (separator) for my curerent page and last page showed after the last page, not between my current page and my last page
how can that possible ?
this is my script for showing the page :
<?php
$paging2 = mysqli_query($koneksi,"select * from laporan where seksi='{$seksi}'");
$jmldata = mysqli_num_rows($paging2);
$jmlhalaman = ceil($jmldata/$batas);
echo "<ul class='pagination'>";
if ($halaman !== 1){
echo "<li><a href='index.php?load=table_laporan&halaman=1'>First</a></li>";
}
if ($halaman > 1){
echo "<li><a href='index.php?load=table_laporan&halaman=".($halaman-1)."'>Previous</a></li>";
}
for($i =1; $i <= $jmlhalaman; $i++){
if ((($i >= $halaman - 3) && ($i <= $halaman + 3)) || ($i == 1) || ($i ==$jmlhalaman))
{
if ($i == $halaman){
$class="active";
} else{
$class="disable";
} //echo "<b>$i</b>";
echo "<li class='$class'>".$i."<span class='sr-only'>(current)</span></li>";
if($i==$jmlhalaman && $halaman <= $jmlhalaman-5) echo "<li><a>...</a></li>";
if($i==1 && $halaman >= 6) echo "<li><a>...</a></li>";
}
}
if ($halaman >= 1){
echo "<li><a href=index.php?load=table_laporan&halaman=". ($halaman+1)."'>Next</a></li>";
} if ($halaman >= 1){
echo "<li><a href='index.php?load=table_laporan&halaman=". ($jmlhalaman)."'>Last</a></li>";
}
echo "</ul>";
echo "<p>Total Data : <b>$jmldata</b> Data</p>";
?>
I'm having problems resolving this so I'm asking for more professional solutions. I have pagination setup for my list of products. Problem is when I switch pages, the active class on the number is not showing or showing wrongly. For example, when users click on 2 (Page 2), it displays the 2nd page of items, but 5 is the one highlighted. Need help fixing this up to highlight the currently being viewed page.
This code is for setting the offset.
$totalrows = db_getvalue("select count(buyinfo_id) from buyinfo,user $addtable where buyinfo.user_id=user.user_id $clause $orderbyclause", $global_connection);
if ($totalrows == "")
$totalrows = 0;
$totalpages = ceil($totalrows/$listcount);
if ($totalpages > 1) {
$curpageoffset = 0;
if (isset($_REQUEST['offset'])) {
if ($_REQUEST['offset'] >= $totalpages)
$curpageoffset = $totalpages - 1;
else if ($_REQUEST['offset']>0)
$curpageoffset = $_REQUEST['offset']*$listcount-1;
}
$limitstr = " limit $curpageoffset,$listcount";
}
When I go to Page 3 for example, it displays www.site.com/?offset=2
I guess its because of the -1 in $curpageoffset = $_REQUEST['offset']*$listcount-1;
This is the code responsible for the page numbers:
<?php
if ($totalpages>1) {
echo "<tr><td><ul class='pagination'>";
echo "<li><a href='' aria-label='Previous' ";
if ($curpageoffset == 0) {
echo "onclick='return false;'";
} else {
echo "onclick='util_reload(\"posts/?offset=".($curpageoffset-1)."$pageparam\"); return false;'";
}
echo " ><span aria-hidden='true'>«</span></a></li>";
$tmpctr = 0;
while ($tmpctr < $totalpages) {
$curstyle = "";
if ($tmpctr == $curpageoffset)
$curstyle = "class='active'";
echo "<li $curstyle><a href='' onclick='util_reload(\"posts/?offset=$tmpctr$pageparam\"); return false;'>";
$tmpctr++;
echo "$tmpctr</a></li>";
}
echo "<li><a href='' aria-label='Next' ";
if ($curpageoffset >= $totalpages-1) {
echo "onclick='return false;'";
} else {
echo "onclick='util_reload(\"posts/?offset=".($curpageoffset+1)."$pageparam\"); return false;'";
}
echo " ><span aria-hidden='true'>»</span></a></li>";
echo "</ul></td></tr>";
}
?>
Since you've set the value of curpageoffset for use in your query, you can't use it in your pagination code to highlight the page you're on ... I would change your code this way .. using the original $_REQUEST['offset'] since that's essentially the page number.
while ($tmpctr < $totalpages) {
$curstyle = "";
if ($tmpctr == $_REQUEST['offset'])
$curstyle = "class='active'";
echo "<li $curstyle><a href='' onclick='util_reload(\"posts/?offset=$tmpctr$pageparam\"); return false;'>";
$tmpctr++;
echo "$tmpctr</a></li>";
}