# php paginate code 

**URL:** <https://www.phphelp.com/t/php-paginate-code/22152>\
**Category:** Security and PHP\
**Created:** [February 20, 2014, 4:34am UTC](https://www.phphelp.com/t/php-paginate-code/22152 "2014-02-20T04:34:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![infoitmanoj](https://www.phphelp.com/user_avatar/www.phphelp.com/infoitmanoj/32/1021_2.png) [@infoitmanoj](https://www.phphelp.com/u/infoitmanoj)\
**Post date:** [February 20, 2014, 4:34am UTC](https://www.phphelp.com/t/php-paginate-code/22152/1 "2014-02-20T04:34:32Z")

</div>

pls any one knows tell php paginate coding understanding easily

---

<div class="post-metadata">

**Author:** ![scottlpool2003](https://www.phphelp.com/letter_avatar_proxy/v4/letter/s/71e660/32.png) [@scottlpool2003](https://www.phphelp.com/u/scottlpool2003)\
**Post date:** [February 20, 2014, 9:50am UTC](https://www.phphelp.com/t/php-paginate-code/22152/2 "2014-02-20T09:50:27Z")

</div>

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]

---

<div class="post-metadata">

**Author:** ![infoitmanoj](https://www.phphelp.com/user_avatar/www.phphelp.com/infoitmanoj/32/1021_2.png) [@infoitmanoj](https://www.phphelp.com/u/infoitmanoj)\
**Post date:** [February 21, 2014, 7:54am UTC](https://www.phphelp.com/t/php-paginate-code/22152/3 "2014-02-21T07:54:14Z")

</div>

thanks dude
