Re: PHP Results on multiple pages

From: Date: Thu, 29 Jan 2009 04:45:03 +0000
Subject: Re: PHP Results on multiple pages
References: 1 2  Groups: php.db 
Request: Send a blank email to php-db+get-45901@lists.php.net to get a copy of this message
Hello Chris, I am inserting complete script for you to have a look at and was using $s = $pages // Get the search variable from URL $var = @$_GET['q'] ; $trimmed = trim($var); //trim whitespace from the stored variable // rows to return $limit=10; // check for an empty string and display a message. if ($trimmed == "") { echo "<p>Please enter a search...</p>"; exit; } // check for a search parameter if (!isset($var)) { echo "<p>We dont seem to have a search parameter!</p>"; exit; } //connect to your database ** mysql_connect("localhost","username","password"); //(host, username, password) //specify database ** mysql_select_db("_database") or die("Unable to select database"); //select which database we're using // Build SQL Query $query = "select * from mytable where massage like '%".$trimmed."%' order by id"; // EDIT HERE and specify your table and field names for the SQL query $numresults=mysql_query($query); $numrows=mysql_num_rows($numresults); // If we have no results, offer a google search as an alternative if ($numrows == 0) { echo "<h4>Results</h4>"; echo "<p>Sorry, your search: &quot;" . $trimmed . "&quot; returned zero results</p>"; } // next determine if s has been passed to script, if not use 0 if (empty($s)) { $s=0; } // get results $query .= " limit $s,$limit"; $result = mysql_query($query) or die("Couldn't execute query"); // display what the person searched for echo "<p>You searched for :<font color=blue size=+2> &quot;" . $var . "&quot;</font>"; // begin to show results set echo " found $numrows results <p>"; $count = 1 + $s; // now you can display the results returned while ($row= mysql_fetch_array($result)) { $title = $row["sname"]; $url = $row["url"]; $message = $row["massage"]; //&nbsp;<a href=\"$row['url']\">"$row['sname']</a><br /> $row['massage']<br><hr>'\ echo "$count.)&nbsp"; echo "<b><a href=\"$url\" target=\"_blank\">$title</a></b>"; echo "<br>"; echo "$message<br><hr height=12 color=lightblue><br>"; $count++; } $currPage = (($s/$limit) + 1); //break before paging echo "<br />"; // next we need to do the links to other results &lt;&lt; if ($s>=1) { // bypass PREV link if s is 0 $prevs=($s-$limit); print "&nbsp;<a href='{$_SERVER['PHP_SELF']}?s=$prevs&q=$var'> <<< Prev 10</a>&nbsp&nbsp;"; } // calculate number of pages needing links // $s=intval($numrows/$limit); $s=ceil($numrows/$limit); // $s now contains int of pages needed unless there is a remainder from division if ($numrows%$limit) { // has remainder so add one page $s++; } // check to see if last page if (!((($s+$limit)/$limit)==$s) && $s!=1) { // not last page so give NEXT link $PHP_SELF &gt;&gt; $news="$s+$limit"; echo "&nbsp;<a href='{$_SERVER['PHP_SELF']}?s=$news&q=$var'>Next 10 >>> </a>"; } $a = $s+$limit; if ($a > $numrows) { $a = $numrows; } $b = $s+1; echo "<p>Showing results $b to $a of $numrows</p>"; include ('footer.inc'); ?> "Chris" <dmagick@gmail.com> wrote in message news:497E31E9.2040808@gmail.com...
This script do everything fine counting total records it display's 1 to 10 records on first page with numbers but second location where i want it to display Showing results 1 to 10 it shows 17 to 26 second it displays a link to Next but not working.
It takes roughly 20 secs to do a search? Perhaps that should also be looked at. Try to narrow down your problem.
// begin to show results set echo " found $numrows results <p>"; $count = 1 + $s;
Where does $s come from? What is it supposed to mean? (Name your variables properly).
// now you can display the results returned while ($row= mysql_fetch_array($result)) { $title = $row["sname"]; $url = $row["url"]; $message = $row["massage"]; //&nbsp;<a href=\"$row['url']\">"$row['sname']</a><br /> $row['massage']<br><hr>'\ echo "$count.)&nbsp"; echo "<b><a href=\"$url\" target=\"_blank\">$title</a></b>"; echo "<br>"; echo "$message<br><hr height=12 color=lightblue><br>"; $count++; } $currPage = (($s/$limit) + 1); //break before paging echo "<br />"; // next we need to do the links to other results if ($s>=1) { // bypass PREV link if s is 0 $prevs=($s-$limit); print "&nbsp;<a href='{$_SERVER['PHP_SELF']}?s=$prevs&q=$var'> <<< Prev 10</a>&nbsp&nbsp;"; } // calculate number of pages needing links // $s=intval($numrows/$limit); $s=ceil($numrows/$limit); // $s now contains int of pages needed unless there is a remainder from division if ($numrows%$limit) { // has remainder so add one page $s++; }
What is $s set to here when there's extra pages to display?
// check to see if last page if (!((($s+$limit)/$limit)==$s) && $s!=1) {
That's just too complicated to read. The first part seems redundant since you're calculating $s anyway, why re-do it? // are there extra pages to display? if ($s > 1) { echo "show extra pages"; }
// not last page so give NEXT link $PHP_SELF &gt;&gt; $news="$s+$limit"; echo "&nbsp;<a href='{$_SERVER['PHP_SELF']}?s=$news&q=$var'>Next 10 >>> </a>"; } $a = $s+$limit; if ($a > $numrows) { $a = $numrows; } $b = $s+1; echo "<p>Showing results $b to $a of $numrows</p>"; include ('footer.inc'); ?> I shall highly appreciate if anyone shall have a look at it and let me know where i am wrong. Thank you.
-- Postgresql & php tutorials http://www.designmagick.com/


« previous php.db (#45901) next »