Jump to content
Sign in to follow this  
MFA

For loop in pagination code not working

Recommended Posts

Hi
 
I'm trying to make a "pagination" for my webpage so people can flick through several pages of images (6 columns by 4 rows). The code is meant to check how many pages are needed and display a maximum of 5 page links, 2 links for the pages before the current page and 2 links for the pages just after. 

 

Anyway, I have tested it and the if statements are only read when the for loop is removed however the latter is necessary for this code to work. 

 

Also I'm very basic in my coding as I'm still new. 
 
here is my code:
 

 
if ($numberofobjects > '23')
{
 
function getCurrentPageUrl() 
{
$pageURL = 'http';
if (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $pageURL;
}
 
$hrefpages = getCurrentPageUrl();
 
 
$draftnumberofpages = $numberofobjects / '24';
$numberofpages = count($draftnumberofpages, 0, PHP_ROUND_HALF_UP);
 
 
$currentpage = substr($hrefpages, -1);
$linkwithoutpage = substr($hrefpages, 0, -1);
 
$prevpage1 = $currentpage - '1';
$nextpage1 = $currentpage + '1';
$prevpage2 = $currentpage - 2;
$nextpage2 = $currentpage + 2;
 
$keepgoing = '0';
$currentpagination = '0';
$nomoreprev = '0';
$nomorenext = '0';
 
echo '<div class="row"></div>';
echo '<div class="row"></div>';
echo '<div class="pagination-centered">
  <ul class="pagination">';
  if ($currentpage != '1')
  {
   echo '<li class="arrow"><a href="'.$linkwithoutpage.$prevpage1.'">«</a></li>';
  }
  
for ($paginationincrement = 0; $paginationincrement == 5; $keepgoing++)
  {
  if ($prevpage2 >= 1)
  {
echo '<li ><a href="'.$linkwithoutpage.$prevpage2.'">'.$prevpage2.'</a></li>';
echo '<li ><a href="'.$linkwithoutpage.$prevpage1.'">'.$prevpage1.'</a></li>';
 $paginationincrement + 2;
 $nomoreprev = '1';
  }
    if ($prevpage1 >= 1 && $nomoreprev != 1 )
  {
echo '<li ><a href="'.$linkwithoutpage.$prevpage1.'">2</a></li>';
 
 $paginationincrement ++;
  }
  
  
  if ($currentpagination == '0')
  {
  
  $paginationarray = '<li class="current"><a href="#">'.$currentpage.'</a></li>';
  $paginationincrement++;
  $currentpagination = '1';
  }
  
  
  if ($nextpage2 <= $numberofpages)
  {
 
echo '<li ><a href="'.$linkwithoutpage.$nextpage2.'">'.$nextpage1.'</a></li>';
echo '<li ><a href="'.$linkwithoutpage.$nextpage1.'">'.$nextpage2.'</a></li>';
 $paginationincrement + 2;
 $nomorenext = 1;
  }
    if ($nextpage1 <= $numberofpages && $nomorenext != 1)
  {
 
echo '<li ><a href="'.$linkwithoutpage.$nextpage1.'">'.$nextpage1.'</a></li>';
 $paginationincrement ++;
  }
  if ($paginationincrement == $numberofpages)
  {
 $paginationincrement = 5;
  }
  
  
  }
 
 
 
 
if ($currentpage != $numberofpages)
{
   echo '<li class="arrow"><a href="'.$linkwithoutpage.$prevpage1.'">»</a></li>';
}
 echo ' </ul>
</div></div>';
 
}

Share this post


Link to post
Share on other sites

Maybe it's me but that code seems quite confusing. You can make this really simple:

1. If page > 1 then show

2. For loop from 1 to current page - 1: show link for page

3. Show "link" for current page

4. For loop from current page + 1 to last page: show link for page

5. If page > link for last page

 

1, 3, and 5 are pretty obvious.

 

For 2 and 4, it's possible for a loop to not even execute depending on what the current page is. Consider if the current page is 1 and the pages are 1-4:

1. Page is not > 1 then do not show

2. Loop from 1 to 0: show link (links: none)

3. Current page (links: 1)

4. Loop from 2 to 4: show link (links: 1, 2, 3, 4)

5. Page is > link (links: 1, 2, 3, 4, >>)

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.