php paginate code

pls any one knows tell php paginate coding understanding easily

This is one I used a while back which is well commented:

[php]// grab the search terms and put them in an array
$terms = explode(" ", urldecode($_GET[‘searchterms’]));

// run the seach query. First line builds the query, the for loops through the remaining entries adding each one in turn. Final executes query & counts lines returned.
$statement = $dbconn->prepare("
SELECT stories.id, stories.dateposted, stories.subject, stories.body, images.name
FROM stories

	INNER JOIN images ON stories.id = images.story_id
	WHERE body LIKE :terms
	GROUP BY images.story_id
	ORDER BY dateposted DESC
");

$statement->execute([
“:terms” => “%” . $_GET[‘searchterms’] . “%”
]);

for($i=1; $i<count($terms); $i++) {
$query = $query." AND body LIKE ‘%". $terms[$i] . "%’";
}
$searchresult = $statement->fetchAll();
$searchnumrows = $statement->rowCount();

// number of rows to show per page
$rowsperpage = 20;
// find out total pages
$totalpages = ceil($searchnumrows / $rowsperpage);

// get the current page or set a default
if (isset($_GET[‘currentpage’]) && is_numeric($_GET[‘currentpage’])) {
// cast var as int
$currentpage = (int) $_GET[‘currentpage’];
} else {
// default page num
$currentpage = 1;
} // end if

// if current page is greater than total pages…
if ($currentpage > $totalpages) {
// set current page to last page
$currentpage = $totalpages;
} // end if
// if current page is less than first page…
if ($currentpage < 1) {
// set current page to first page
$currentpage = 1;
} // end if

// Output page number and amount of results
echo “Page " . $currentpage . " of " . $searchnumrows.” results for ‘" . $_GET[‘searchterms’] . "’";

// the offset of the list, based on current page
$offset = ($currentpage - 1) * $rowsperpage;

// get the info from the db
$statement = $dbconn->prepare("
SELECT stories.id, stories.dateposted, stories.subject, stories.body, images.name
FROM stories
INNER JOIN images ON stories.id = images.story_id
WHERE body LIKE :terms
GROUP BY images.story_id
ORDER BY dateposted DESC
LIMIT $offset, $rowsperpage
");
$statement->execute([
“:terms” => “%” . $_GET[‘searchterms’] . “%”
]);
while ($list = $statement->fetch()){
echo “

”;
echo “<img class=‘main_image’ src=’” . $config_basedir . “images/” . $list[‘name’] . “’ width=‘70’ height=‘70’>”;
// echo data
 //Title for friendly urls
      $title = preg_replace('/[^A-Za-z0-9-]+/', '-', ucwords($list['subject']));

      //echo "<h2><a href='/$papername/$row[id]/$title.html' title='" . $row['subject'] . "'>" . $row['subject'] . "</a></h2>";
      echo "<h2><a href='/$list[id]/$title.html' title='" . ucwords($list['subject']) . "'>" . htmlallentities($list['subject']) . "</a></h2>";
      echo "<p class='date'>";
      echo date("D jS F Y g.iA", strtotime($list['dateposted']));

      // short_description() (found in functions.php takes the text and provides a summary.)
      echo "" . htmlallentities(short_description($list['body'] . ""));
      echo "</p></div><div style=\"clear:both;\"></div>";

} // end while

/****** build the pagination links ******/
// range of num links to show
$range = 3;

// if not on page 1, don’t show back links
if ($currentpage > 1) {
// show << link to go back to page 1
echo " <a href=’{$_SERVER[‘PHP_SELF’]}?currentpage=1&searchterms=" . $_GET[‘searchterms’] . "’><< “;
// get previous page num
$prevpage = $currentpage - 1;
// show < link to go back to 1 page
echo " <a href=’{$_SERVER[‘PHP_SELF’]}?currentpage=$prevpage&searchterms=” . $_GET[‘searchterms’] . "’>< ";
} // end if

// loop to show links to range of pages around current page
for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
// if it’s a valid page number…
if (($x > 0) && ($x <= $totalpages)) {
// if we’re on current page…
if ($x == $currentpage) {
// ‘highlight’ it but don’t make a link
echo " [$x] “;
// if not current page…
} else {
// make it a link
echo " <a href=’{$_SERVER[‘PHP_SELF’]}?currentpage=$x&searchterms=” . $_GET[‘searchterms’] . "’>$x ";
} // end else
} // end if
} // end for

// if not on last page, show forward and last page links
if ($currentpage != $totalpages) {
// get next page
$nextpage = $currentpage + 1;
// echo forward link for next page
echo " <a href=’{$_SERVER[‘PHP_SELF’]}?currentpage=$nextpage&searchterms=" . $_GET[‘searchterms’] . "’>> “;
// echo forward link for lastpage
echo " <a href=’{$_SERVER[‘PHP_SELF’]}?currentpage=$totalpages&searchterms=” . $_GET[‘searchterms’] . "’>>> ";
} // end if
/****** end build pagination links ******/[/php]

thanks dude

Sponsor our Newsletter | Privacy Policy | Terms of Service