> -----Original Message-----
> From: ctan [mailto:[EMAIL PROTECTED]] 
> Sent: Thursday, July 25, 2002 11:24 AM
> To: [EMAIL PROTECTED]
> Subject: [PHP] 'Previous' 1, 2, 3, 4, etc. 'Next'
> 
> 
> I seem to have a problem getting the page to display beyond 
> the limit if a
> page, i.e. if the limit if 10 rows in a page I'll only get 
> the 1st ten rows
> and then a link to further rows but when I chick on them they give me
> nothing. Here's the code:
> 
> 
> $searchword = $_POST['searchword'];
> print "Your word(s) is/are: <b>$searchword</b><p>\n\n";
> 
> // Searching by keyword
> if (! empty($searchword )){ 
>    $max = 0;
>    $query = "SELECT aml FROM arguments WHERE aml LIKE 
> '%$searchword%'";
>    $result1 = mysql_query($query)
>                               or die ("Query failed");
> 
> // Determine the number of items containing the $searchword   
>        while ($line1 = mysql_fetch_array($result1)){
>        $max++;
>        }      

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
This could just be:
$max = mysql_num_rows($result1); 
---------------------------------------------------
> 
> 
> // The number of results to be displayed on screen
> $maxresult = 10; 
> 
> $sql_text = "SELECT aml FROM arguments WHERE aml LIKE 
> '%$searchword%'";
> 
> // When the current page is yet to be determined
> if (!$page) { 
>        $page = 1;
>        } 

^^^^^^^^^^^^^^^^^^^^^^^
Do you have register_globals on or should you be setting  
$page = $_GET['page'] here?
--------------------------------------------------------

>     
> $backpage = $page - 1;
> $nextpage = $page + 1;
> $result2 = mysql_query($sql_text);
> $start = ($maxresult * $page) - $maxresult; 
> $num_rows = mysql_num_rows($result2); 
> 
> // When the query returns less or equal number of rows than 
> the limit set by
> $maxresult
> if ($num_rows <= $maxresult) {
>        $num_pages = 1; 
>    } 
> 
> // When the query returns the exact limit set by $maxresult
> else if (($num_rows % $maxresult) == 0) {
>    $num_pages = ($num_rows / $maxresult);

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
You could simplify this here with
$num_pages = ceil(($num_rows / $maxresult));
And get rid of the else below
----------------------------------------------

>    } 
> 
> // For any other cases...
> else {
>    $num_pages = ($num_rows / $maxresult) + 1;
>    } 
> 
> // Declared as an integer
> $num_pages = (int) $num_pages;
> 
> // The current page is greater than the total number of pages or 
> //     the current page is less than 0    
> if (($page > $num_pages) || ($page < 0)) {
>        error("You have specified an invalid page number");
>    }
> 
> // Set the limit per page   
> $sql_text = $sql_text . " LIMIT $start, $maxresult";
> $result2 = mysql_query($sql_text);
> 
> // The navigation between pages
> // Ensure only display when total number of return results exceeds
> $maxresult
> //     i.e. will not display if only 1 page    
> if ($max>$maxresult){
>        print "<center>- ";
>    if ($backpage) { 
>    print "<a
> href=\"$PHP_SELF?searchword=$searchword&page=$backpage\">Prev</a>";
>    } 
> 
> // If its the first page; have 'Prev' un-clickable
> else {
>    print "Prev";
>    }
> 
> for ($i = 1; $i <= $num_pages; $i++) {
>    if ($i != $page) { 
>                       
>       print " <a 
> href=\"$PHP_SELF?searchword=$searchword&page=$i\">$i</a> ";
>    } 
>        else { 
>       print " $i "; 
>    } 
> }
>     
> if ($page != $num_pages) {
>    print "<a
> href=\"$PHP_SELF?searchword=$searchword&page=$nextpage\">Next</a> -";
>    } 
> else {
>    print "Next -";
>    }
>    print "</center>";
> }
> 
> print "<table border=\"1\"><th BGCOLOR=\"#ff0000\">Results</th>";
> while ($line = mysql_fetch_array($result2, MYSQL_ASSOC)) {
>                       print "\t<tr BGCOLOR=\"#000080\">\n";
>                       // The different color, in this case 
> light blue will
> show that
>                       //    the particular table belong to search by
> keywords
>                 foreach ($line as$col_value) {
>                                       print 
> "\t\t<td>$col_value</td>\n";
>       }
>       print "\t</tr>\n";
> }
> 
> 
> }
> 

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

Reply via email to