Hello everyone I am encountering a problem while using pagination with GET[] Function.

Code does work well in the index page where the website displays all the categories, however i am encountering the problem when i want to use pagination with a specific category. EX:- Cars

This is the URL passing from the website to the "displaysubjects.php" page when user clicks on a specific category.

<a href='displaysubjects.php?subid=$subjectid'>".$subjectid."</a>

When i go to a specific category, the page will display the first 5 contents as it has been set in the pagination code and the pagination bar, however if i click on the next button or a number page will go blank.

Thank you very much for your time. Your help will be greatly appreciated.

Following is my code so far.

$subjectid = $_GET['subid'];

$sql = mysql_query("SELECT id,subjectid,pagetitle,bodytext,showing,datecreated,keywords FROM pages WHERE subjectid='$subjectid'"); 

$nr = mysql_num_rows($sql);
if (isset($_GET['pn'])) {
    $pn = preg_replace('#[^0-9]#i', '', $_GET['pn']); 

} else { 
    $pn = 1;
} 

$itemsPerPage = 5; 

$lastPage = ceil($nr / $itemsPerPage);

if ($pn < 1) {
    $pn = 1; 
} else if ($pn > $lastPage) { 
    $pn = $lastPage; 
} 

$centerPages = ""; 
$sub1 = $pn - 1;
$sub2 = $pn - 2;
$add1 = $pn + 1;
$add2 = $pn + 2;
if ($pn == 1) {
	$centerPages .= '&nbsp; <span class="pagNumActive">' . $pn . '</span> &nbsp;';
	$centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $add1 . '">' . $add1 . '</a> &nbsp;';
} else if ($pn == $lastPage) {
	$centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $sub1 . '">' . $sub1 . '</a> &nbsp;';
	$centerPages .= '&nbsp; <span class="pagNumActive">' . $pn . '</span> &nbsp;';
} else if ($pn > 2 && $pn < ($lastPage - 1)) {
	$centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $sub2 . '">' . $sub2 . '</a> &nbsp;';
	$centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $sub1 . '">' . $sub1 . '</a> &nbsp;';
	$centerPages .= '&nbsp; <span class="pagNumActive">' . $pn . '</span> &nbsp;';
	$centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $add1 . '">' . $add1 . '</a> &nbsp;';
	$centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $add2 . '">' . $add2 . '</a> &nbsp;';
} else if ($pn > 1 && $pn < $lastPage) {
	$centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $sub1 . '">' . $sub1 . '</a> &nbsp;';
	$centerPages .= '&nbsp; <span class="pagNumActive">' . $pn . '</span> &nbsp;';
	$centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $add1 . '">' . $add1 . '</a> &nbsp;';
}

$limit = 'LIMIT ' .($pn - 1) * $itemsPerPage .',' .$itemsPerPage; 

$sql2 = mysql_query("SELECT id,subjectid,pagetitle,bodytext,showing,datecreated,keywords FROM pages WHERE subjectid='$subjectid' $limit"); 

$paginationDisplay = ""; 
if ($lastPage != "1"){

    $paginationDisplay .= 'Page <strong>' . $pn . '</strong> of ' . $lastPage;

    if ($pn != 1) {
	    $previous = $pn - 1;
		$paginationDisplay .=  '&nbsp;  <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $previous . '"> Back</a> ';
    } 

    $paginationDisplay .= '<span class="paginationNumbers">' . $centerPages . '</span>';

    if ($pn != $lastPage) {
        $nextPage = $pn + 1;
		$paginationDisplay .=  '&nbsp;  <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $nextPage . '"> Next</a> ';
    } 
}

Recommended Answers

All 3 Replies

Can someone help me please.

I didn't look at the thread, but... I don't see where you are using anything from your sql statements. You need to add &subid=$subid where $subid is the subject id pulled from your database at the end of each of your links. so when your link posts back to the page, you can pass in the subject id so for example:
$centerPages .= '&nbsp; <a href="' . $_SERVER . '?pn=' . $add1 . '&subid='.$subid.'">' . $add1 . '</a> &nbsp;'; so set both your $pn variable, and your $subid variable on the links.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.