Pagination now not working

I had pagination on my pages successfully working, but I had the code spewed all over the place, and decided it was time to shrink this to be much cleaner. I decided to include this pagination into my functions.php file, and see if this would work.

I basically copied to code to the fuctions.php file and plugged in necessary values.

Here is my code in the functions.php file:


function pagination($mid, $perpage) {

	$currentpage = array_key_exists('page', $_GET) ? (int)$_GET['page'] : 1;

	if (!isset($currentpage)) {
		$currentpage = 1;
	} else {
		$currentpage = $_GET['page'];
	}

	dbconnect();
	$query = "SELECT * FROM content WHERE mid = '$mid' AND active = '1'";
	$contentCount = mysql_num_rows(mysql_query($query));

	$maxpage = ceil($countContent / $perpage);

	// Next and Previous links
	if ($currentpage == 1) {
          if ($maxpage == 1) {
          	$previous = '';
          	$next = '';
     	} elseif ($maxpage > 1) {
          	$previous = '';
     		$next = '<a href="pictures.php?page='.($currentpage + 1).'">Next</a>';
     	}
	} else {
          if ($currentpage == $maxpage) {
          	$previous = '<a href="pictures.php?page='.($currentpage - 1).'">Previous</a>';
          	$next = '';
          } else {
          	$previous = '<a href="pictures.php?page='.($currentpage - 1).'">Previous</a> - ';
          	$next = '<a href="pictures.php?page='.($currentpage + 1).'">Next</a>';
          }
     }

	// Page number links
	if ($maxpage <= 1) {
		$links = '';
	} else {
		for($i=1; $i<=$maxpage; $i++) {
			$links = '<a href="pictures.php?page='.$i.'"> '.$i.' </a>';
		}
	}

	return $previous.$links.$next;

}

The MID stands for “media ID” since I have four different types of media (pictures, text messages, stories, and videos). Each has a unique number 1-4. I tried to call the pagination() function like so:

pagination(1,5);

The only thing displayed was the “Previous” link with a value that goes to “page=-1” and I can not figure out why.

Thank you very much for your help.

I figured out what the problem was, now I just figured out the hard way the for() loop will not work in this situation. Is there any way around this?

I’m not sure exactly what your problem is with the for loop, but I think you need to concatenate the $links variable, not overwrite it within the loop:


    // Page number links
    $links = '';
    if ($maxpage > 1) {
        for($i=1; $i<=$maxpage; $i++) {
            $links .= '<a href="pictures.php?page='.$i.'"> '.$i.' </a>';
        }
    }

Notice the .= sign I’m using here to add the <a> together in one string.

If you use only the = sign, every loop overwrites the value stored in $links with the new <a>. Eventually you end up with only one link, the last one (i.e. $maxpage).

I’ve re-written the function for you. You had some problems in your if statements and concatenation.


<?php
function pagination($whatever, $rowsPerPage) {
    dbconnect();
    $query = "SELECT 1 FROM content WHERE whatever = $whatever AND active = 1";
    $rowsCount = mysql_num_rows(mysql_query($query));
    $pageCount = ceil($rowsCount / $rowsPerPage);
    $currentPage = array_key_exists('page', $_GET) ? (int) $_GET['page'] : 1;
    if($currentPage < 1 || $currentPage > $pageCount) {
        $currentPage = 1;
    }
    if($currentPage == 1 || $pageCount == 1){
        $prevLink = '';
    }
    else {
        $prevLink = '<a href="pictures.php?page=' . ($currentPage - 1). '">Prev</a>' . PHP_EOL;
    }
    $pageLinks = '';
    for ($i = 1; $i <= $pageCount; $i++) {
        if($i == $currentPage){
            $pageLinks .= '<span>' . $i . '</span>' . PHP_EOL;
        }
        else {
            $pageLinks .= '<a href="pictures.php?page=' . $i . '">' . $i . '</span>' . PHP_EOL;
        }
    }
    if($currentPage == $pageCount || $pageCount == 1){
        $nextLink = '';
    }
    else {
        $nextLink = '<a href="pictures.php?page=' . ($currentPage + 1). '">Next</a>' . PHP_EOL;
    }
    return $prevLink . $pageLinks . $nextLink;
}
?>

Thank you for the help :slight_smile:

The period before the equals sign was what was going wrong. Which makes perfect sense. Each for loop that ran, would overwrite the link produced previously. So If I had 4 pages, only 4 would show up because that was as high as the for loop went.

I will be cleaning up the code, and taking the pointers from your rewritten function as well!