Jump to content

Recommended Posts

hello, im having problem using this pagination within a while loop that fetch items from the db (not my code, didnt know how to create one so i looked it up also its a old code) the code originally use mysql but since my connection uses mysqli i changed it to match my database, the code seem to be correct but when i added it to my other script that fetch the items it doesnt out. also when using pagination doesnt it need to use the table names from the database? this is the code im using

<?php	   include_once("php/config.php");	 $query = "SELECT id, title, author, post, DATE_FORMAT(date, '%M %d, %Y') as sd FROM news ORDER BY date DESC";	 $result = mysqli_query($con, $query);$nr = mysqli_num_rows($result);if(isset($_GET['id'])){    $pn = preg_replace('#[^0-9]#i', '', $_GET['id']);}else{    $pn = 1;}$itemsPerPage = 8;$lastPage = ceil($nr / $itemsPerPage);if($pn < 1){    $pn = 1;}else if($pn > $lastPage){	    $pn = $lastPage;    }       // creating the numbers    $centerPages = "";    $sub1 = $pn -1;    $sub2 = $pn -2;    $add1 = $pn +1;    $add2 = $pn +2;    if($pn == 1){	    $centerPages .= ' <span  class"PageNumActive">' . $pn . '</span>  ';	    $centerPages .= ' <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $add1 . '">' . $add1 . '</a> ';    }else if($pn == $lastPage){	    $centerPages .= ' <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $sub1 . '">' . $sub1 . '</a> ';	    $centerPages .= ' <span  class="pagNumActive">' . $pn . '</span> ';    }else if($pn >2 && $pn<($lastPage - 1)){	    $centerPages .= ' <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $sub2 . '">' . $sub2 . '</a> ';	    $centerPages .= ' <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $sub1 . '">' . $sub1 . '</a> ';	    $centerPages .= ' <span  class="pagNumActive">' . $pn . '</span> ';	    $centerPages .= ' <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $add1 . '">' . $add1 . '</a> ';	    $centerPages .= ' <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $add2 . '">' . $add2 . '</a> ';    }else if($pn >1 && $pn < $lastPage){	    $centerPages .= ' <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $sub1 . '">' . $sub1 . '</a> ';	    $centerPages .= ' <span  class="pagNumActive">' . $pn . '</span> ';	    $centerPages .= ' <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $add1 . '">' . $add1 . '</a> ';    }    // creating the limit variable    $limit = 'LIMIT' . ($pn - 1)* $itemsPerPage . ',' . $itemsPerPage;    // now adding the limit variable to the query    $query2 = "SELECT id, title, author, post, DATE_FORMAT(date, '%M %d, %Y') as sd FROM news ORDER BY date DESC $limit";    // Displaying the pagination    $paginationDisplay = "";    if($lastPage != "1"){	    $paginationDisplay .= 'Page <strong>' . $pn . '</strong> of' . $lastPage . '   ';	   	    // Determining if the person is on page one	    if($pn != 1){		    $previous = $pn - 1;		    $paginationDisplay .= ' <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $previous . '">back</a>';		   		    // adding the numbers in the middle of both back and next		    $paginationDisplay .= '<span class="paginationNumbers">' . $centerPages . '</span>';		   		    // now add the next button		    if($pn != $lastPage){			    $nextPage = $pn +1;			    $paginationDisplay .= ' <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $nextPage . '">next</a>';		    }	    }    }	 $result = mysqli_query($con, $query2);	 if($result){	  while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){	   echo '<table width="600" border="0" align="center" bgcolor="#FFFFFF"><tr>';	   echo '<td width="70" align="left" valign="bottom">Topic:</td>';	   echo '<td colspan="2" align="left" valign="bottom" bgcolor="#FFFFFF">' . $row['title'] . '</td></tr>' . '<br />';	   echo '<tr><td align="left" valign="bottom">Author:</td>';	   echo '<td colspan="2" align="left" valign="bottom" bgcolor="#FFFFFF">' . $row['author'] . '</td></tr>' . '<br />';	   echo '<tr><td align="left" valign="top">Article:</td>';	   echo '<td colspan="2" align="left" valign="bottom" bgcolor="#FFFFFF">' . $row['post'] . '</td></tr>' . '<br />';	   echo '<tr><td align="left" valign="bottom"> </td>';	   echo '<td width="396" align="left" valign="bottom"> </td>';	   echo '<td width="120" valign="bottom" bgcolor="#FFFFFF">' . $row['sd'] . '</td></tr>';	   echo '</table>';	  }	 }else {	  echo "There\'s Are No News Post(s) To Display";	 }    ?>

what the above code suppose to do, is when 8 topics is displayed, and if theres more in the database, it give you the option to go to the next page etc etc and if theres only 2 pages it give you the option to go back to the first page etc etc. but im not being able to display anything. any tip, suggestion, etc would be greatly appreciated

Link to post
Share on other sites

what is happening when you run it? what is it showing?are there any error?

Link to post
Share on other sites
what is happening when you run it? what is it showing?are there any error?
no errors, just a blank page
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.   Paste as plain text instead

  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.

Loading...
×
×
  • Create New...