RE: Search engine won't page properly

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

 



> -----Original Message-----
> From: Tommy Pham [mailto:tommyhp2@xxxxxxxxx]
> Sent: Friday, February 04, 2011 3:49 PM
> To: 'Bill Mudry'
> Cc: 'php-windows@xxxxxxxxxxxxx'
> Subject: RE:  Search engine won't page properly
> 
> 
> Previously,
> 
> > [CODE]
> 
> <snip>
> 
> > <BODY bgcolor='ivory'>
> >
> >    <?php
> >          //$s = 14; hard coded as a debug statement
> >          $S= @$_GET['$s'] ;
> 
> With the above, PHP is looking for the GET parameter name $s, not the
> value of $s as the the GET parameter name.  If you want the latter, change
it
> to $_GET[$s].  IIRC, PHP is not case specific, not like Java and C#.
> 
> >    Echo "\$s on line 69 is - $s<BR />"; //debug statement
> >    ?>
> 
> <snip>
> 
> >
> > [/CODE]
> 
> 
> On Fri, Feb 4, 2011 at 8:05 AM, Bill Mudry <billmudry@xxxxxxxxxx> wrote:
> > 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.
> >
> 
> I think you're over complicating things.  Paging doesn't depend
complicated
> code algorithm but rather simple math deductions.  See the below revised
> code.
> 
> > 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
> 
> 
> Revised code:
> 
> $sqlSearchCount = "SELECT COUNT(*) AS Total FROM species WHERE
> $searchfield like \"%$querystring%\" order by '$searchfield'";  /*  look
into
> [1] */
> 
> $result = mysql_query( $sqlSearchCount ); $totalRows =
> mysql_fetch_assoc('Total');
> 
> $numOfResults = 10;
> 
> $maxPages = ceil( $totalRows / $numOfResults );
> 
> if( !empty($_GET['page']) )
> {
>     if( strtolower(trim($_GET['page'])) == 'all' )
>     {
>         $currentPage = 'all';
>     }
>     else {
>         $currentPage = intval($_GET['page']);
>         if( $currentPage > $maxPages ) $currentPage = 1;
>         elseif ( $currentPage > 1 ) $currentPage--;
>         else $currentPage = 1;
>     }
> }
> else $currentPage = 0;

Sorry, was in hurry to go run an errand didn't have time to review my
e-mail.  That should be else $currentPage = 1;

> 
> $sqlSearch = "SELECT * FROM species WHERE $searchfield like
> \"%$querystring%\" order by '$searchfield'";
> 
> if( $currentPage != 'all' ) $sqlSearch .= ' LIMIT '.($currentPage-
> 1)*$numOfResults.', '.$numOfResults;
> 
> $result = mysql_query( $sqlSearch );
> /* code to display $result */
> 
> if( $currentPage > 1 ) echo '<a href="?page='.$currentPage -
> 1.'">Previous</a>'; if( $currentPage < $maxPages ) echo '<a
> href="?page='.$currentPage + 1.'">Next</a>'; if( $totalRows > 0 ) echo
> 'Showing '.($currentPage - 1)*$numOfResults+1.' to '.( $currentPage <
> $maxPages ) ? ( $currentPage )*$numOfResults : $totalRows .' of total:
> '.$totalRows;
> 
> NOTE:  The code is not tested, but rather quickly cranked out based on
logic.
> 
> Regards,
> Tommy
> 
> [1] http://php.net/mysql_escape_string



-- 
PHP Windows Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



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

  Powered by Linux