Version: 1.2
Type: Class
Category: Databases
License: GNU General Public License
Description: Pass it a mysql database link, an sql statement and a total results to show. Then access the sql results plus total results, next and prev all created for you.
<?php // Example usage: // // $search = new Search($databaseLink, $sqlQuery, $numResultToDisplay); // // $search->getNumRows() // Total rows found from 0 to $numResultToDisplay // // $search->results // Results from query so.... while ($row = mysql_fetch_assoc($search->results)) { // // $search->getPrev() // Link for previous results if they exist or '' // // $seatch->getNext() // Link for next results if they exists or '' // // if($search->getPrev()) { $prev='<a href="'.$search->getPrev().'">Previous</a>'; } else { $prev='Previous'; } class Search { var $tmp; function Search($DbLink, $sql, $limit) { // Get total results $numresults = mysql_query($sql, $DbLink); $this->setNumRows(mysql_num_rows($numresults)); mysql_free_result($numresults); if(! $_GET['offset']) { $offset = 0; } else { $offset = $_GET['offset']; } $prevoffset = $_GET['offset'] - $limit; $nextoffset = $offset + $limit; $sql = $sql . " limit $offset, $limit"; $this->results = mysql_query($sql, $DbLink); $this->setPrev($prevoffset); $this->setNext($nextoffset); return $results; } // Number of rows returned function setNumRows($numrows) { $this->tmp['numrows'] = $numrows; } function getNumRows() { return $this->tmp['numrows']; } // Create Previous link function setPrev($prevoffset) { if($prevoffset < 0) { $this->tmp['prev'] = ''; } else { $string = $_SERVER['REQUEST_URI']; $pattern = '/&*offset=d+/'; if(preg_match($pattern, $string)) { $string = preg_replace($pattern, '&offset=' . $prevoffset, $string); } else { $string .= '&offset=' . $prevoffset; } $this->tmp['prev'] = $string; } } function getPrev() { return $this->tmp['prev']; } // Create Next link function setNext($nextoffset) { if($nextoffset >= $this->getNumRows()) { $this->tmp['next'] = ''; } else { $string = $_SERVER['REQUEST_URI']; $pattern = '/&*offset=d+/'; if(preg_match($pattern, $string)) { $string = preg_replace($pattern, '&offset=' . $nextoffset, $string); } else { $string .= '&offset=' . $nextoffset; } $this->tmp['next'] = $string; } } function getNext() { return $this->tmp['next']; } } ?>