RE: Search engine won't page properly

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



At 12:11 AM 04/02/2011, Tommy Pham wrote:
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

Perhaps interesting code but when I said including code would help, I was referring to code for the best way of preserving the record counter, $s, when the program the program recycles back on itself on PHP_SELF. As mentioned, at present it does not carry forward the $s value so it can accumulate with the next page of records and therefore gets stuck on
page one.

Thank you for your effort.

The general code is already there. In fact, if you were to take a closer look, it is a fairly sophisticated algorithm with a lot of checks and validations. It is also very user friendly, letting users click on "Next >>" to go ahead one page and "<< Prev" to go backward one
page.

My testing shows that it should work well IF the value of the record counter would only transfer on restarting the page. I will try using SESSION today to see if it will do the job.

Bill Mudry
MIssissauga


[1] http://dev.mysql.com/doc/refman/5.1/en/select.html

[Index of Archives]     [PHP Home]     [PHP Users]     [PHP Database Programming]     [PHP Install]     [Kernel Newbies]     [Yosemite Forum]     [PHP Books]

  Powered by Linux