RE: [PHP-WIN] Search engine won't page properly
| From: | Tommy Pham | Date: | Fri, 04 Feb 2011 05:11:11 +0000 |
| Subject: | RE: [PHP-WIN] Search engine won't page properly | ||
| References: | 1 | Groups: | php.windows |
| Request: | Send a blank email to php-windows+get-30498@lists.php.net to get a copy of this message | ||
Bill,
That's a lot of reading ... Anyway, this seems to be a 'general' PHP
question. If I understood you correctly, you're having problem getting PHP
to page the SQL results? If so, look the below code logic and adapt as
necessary:
$numOfResults = 10; // change this to your need
if( !empty($_GET['page']) && intval($_GET['page']) > 1 ) $currentPage
=
intval($_GET['page']) - 1; // change the $_GET['page'] accordingly
elseif (!empty($_GET['page']) && strtolower(trim($_GET['page'])) ==
'all')
$currentPage = 'all';
else $currentPage = 0;
$sqlQuery = 'SELECT * FROM my_table ';
If( $currentPage != 'all' ) $sqlQuery .= ' LIMIT
'.$currentPage*$numOfResults.', '.$numOfResults; // see [1]
$result = mysql_query($sqlQuery);
Would give you the following:
?page=all returns all results
?page=1 or ?page=0 or ?page= or ? yields 1st batch $numOfResults ( 1 to 10
)
?page=2 yields 2nd batch $numOfResults ( 11 to 20 )
?page=3 yields 3rd batch $numOfResults ( 21 to 30 )
?page=4 yields 4th batch $numOfResults ( 31 to 40 )
Etc....
Regards,
Tommy
[1] http://dev.mysql.com/doc/refman/5.1/en/select.html