PHP Search Pagination Problem
Posted: Fri Mar 20, 2009 9:32 pm
Hi, this is my first post so please have mercy.
I've been making a search engine for my site and have been trying to implement pagination for the past week if no success, i have tried all the tutorials in existance but cant seem to get anything to work with my code. Could someone please tell me where im going wrong?
It shows the first 5 results fine, but when i click on e.g. page 2 of the results i get a
Fatal error: SQL in C:\webserver\Apache2\htdocs\Cake-Photos.com\php\search2.php on line 175
I have high lighted my query below.
I've been making a search engine for my site and have been trying to implement pagination for the past week if no success, i have tried all the tutorials in existance but cant seem to get anything to work with my code. Could someone please tell me where im going wrong?
It shows the first 5 results fine, but when i click on e.g. page 2 of the results i get a
Fatal error: SQL in C:\webserver\Apache2\htdocs\Cake-Photos.com\php\search2.php on line 175
I have high lighted my query below.
Code: Select all
<?PHP
include "../includes/headnav.html" ;
echo '<div id="head"></div>
<div id="search">' ;
include "../includes/header-search.html" ;
echo '</div>
<div id="sidebar">' ;
include "../includes/sidenav.html" ;
echo'</div>
<div id="main">' ;
if (!file_exists("dbconnect.php"))
{
die("Database settings not found, administrator intervention required.") ;
}
else
{
require("dbconnect.php") ; //Must connect to the database.
}
$word = $_GET['word'];
$type = $_GET['type'] ;
$flavor = $_GET['flavor'] ;
$tiers = $_GET['tiers'] ;
$serves = $_GET['serves'] ;
$price = $_GET['price'] ;
$word = strip_tags($word) ;
$symbol[0] = '£' ;
$symbol[1] = '$' ;
$replace[0] = '£' ;
$replace[1] = '$' ;
$price = str_replace($replace,$symbol,$price) ;
if(isset($word))
{
include ('eliminator.php') ; //Include the word cleaner, deletes symbols etc.
include ('stemmer.php') ; //Include the Stemmer Algorythm, "tests" = "test" etc.
$stemmer = new PorterStemmer ; //Call class in the stemmer.php file.
$stemmed_string = $stemmer->stem(strtolower($word)); //force words to lower case.
$cleanup = new Cleaner ; //Call word cleaner class in the eliminator.php file.
$stemmed_string = $cleanup->parseString($stemmed_string) ;
$sanction = split(" ",$stemmed_string) ; //Spaces constiture a new word?
foreach ($sanction as $array => $V)
{
$x_string .= ''.$V.' ' ;
}
$x_string = substr($x_string,0,(strlen($x_string)-1)) ;
$split_stemmed = split(" ",$x_string) ;
while(list($key,$V)=each($split_stemmed))
{
if($V<>" " AND strlen($V) > 0){
$wordx .= "(tags LIKE '%$V%' OR title LIKE '%$V%' OR Description LIKE '%$V%' OR decorator LIKE '%$V%') OR" ;
}
}
$wordx = substr($wordx,0,(strlen($wordx)-3)) ;
echo "<h2>You Searched for $word</h2>" ;
}
if(isset($type) && $type != $def)
{
if(isset($word))
{
$typex = "&& type LIKE '$type'}'" ;
}
else
$typex = "type LIKE '$type'" ;
}
if(isset($flavor) && $flavor != $def)
{
if(isset($word) || isset($type))
{
$flavorx = "&& flavor LIKE '$flavor'" ;
}
else
$flavorx = "flavor LIKE '$flavor'" ;
}
if(isset($tiers) && $tiers != $def)
{
if(isset($word) || isset($type) || isset($flavor))
{
$tiersx = "&& tiers LIKE '$tiers'" ;
}
else
$tiersx = "tiers LIKE '$tiers'" ;
}
if(isset($serves) && $serves != $def)
{
if(isset($word) || isset($type) || isset($flavor) || isset($tiers))
{
$servesx = "&& serves LIKE '$serves'" ;
}
else
$servesx = "serves LIKE '$serves'" ;
}
if(isset($price) && $price != $def)
{
if(isset($word) || isset($type) || isset($flavor) || isset($tiers) || isset($serves))
{
$pricex = "&& price LIKE '$price'" ;
}
else
$pricex = "price LIKE '$price'" ;
}
// find out how many rows are in the table
$sql = "SELECT COUNT(*) FROM image_bank";
$result = mysql_query($sql) or trigger_error("SQL", E_USER_ERROR);
$r = mysql_fetch_row($result);
$numrows = $r[0];
// number of rows to show per page
$rowsperpage = 5;
// find out total pages
$totalpages = ceil($numrows / $rowsperpage);
// get the current page or set a default
if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
// cast var as int
$currentpage = (int) $_GET['currentpage'];
} else {
// default page num
$currentpage = 1;
} // end if
// if current page is greater than total pages...
if ($currentpage > $totalpages) {
// set current page to last page
$currentpage = $totalpages;
} // end if
// if current page is less than first page...
if ($currentpage < 1) {
// set current page to first page
$currentpage = 1;
} // end if
// the offset of the list, based on current page
$offset = ($currentpage - 1) * $rowsperpage;
[color=#FF4000]// get the info from the db
$sql = "SELECT * FROM image_bank WHERE $wordx $typex $flavorx $tiersx $servesx $pricex LIMIT $offset, $rowsperpage";
$result = mysql_query($sql) or trigger_error("SQL", E_USER_ERROR);[/color]
// while there are rows to be fetched...
while ($row = mysql_fetch_assoc($result)) {
// echo data
$dbtitle = $row['title'] ;
$dbdescr = $row['description'] ;
$dbtags = $row['tags'] ;
$dbdec = $row['decorator'] ;
$dbtiers = $row['tiers'] ;
$dbtype = $row['type'] ;
$dbflavor = $row['flavor'] ;
$dbserves = $row['serves'] ;
$dbprice = $row['price'] ;
$imgurl = $row['url'];
$thumburl = $row['thumb_url'] ;
$rating = $row['rating'] ;
echo "$dbtags" . "<br />" ;
echo "$dbtitle" . "<br />" ;
echo "Decorated by : $dbdec" . "<br />" ;
echo "Description : $dbdescr" . "<br />" ;
echo "$dbtiers tier cake" . "<br />" ;
echo "Type : $dbtype cake" . "<br />" ;
echo "Flavor : $dbflavor" . "<br />" ;
echo "Serves : $dbserves" . "<br />" ;
echo "Costs : $dbprice" . "<br />" ;
echo 'Information Page : <a href="' . "$imgurl" . '">' . "$imgurl" . "</a>" . "<br />" ;
echo '<img src="' . "$thumburl" . '" height="100" width="50" />' ;
echo "$rating" ;
} // end while
$range = 3;
// if not on page 1, don't show back links
if ($currentpage > 1) {
// show << link to go back to page 1
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'><<</a> ";
// get previous page num
$prevpage = $currentpage - 1;
// show < link to go back to 1 page
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'><</a> ";
} // end if
// loop to show links to range of pages around current page
for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
// if it's a valid page number...
if (($x > 0) && ($x <= $totalpages)) {
// if we're on current page...
if ($x == $currentpage) {
// 'highlight' it but don't make a link
echo " [<b>$x</b>] ";
// if not current page...
} else {
// make it a link
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> ";
} // end else
} // end if
} // end for
// if not on last page, show forward and last page links
if ($currentpage != $totalpages) {
// get next page
$nextpage = $currentpage + 1;
// echo forward link for next page
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'>></a> ";
// echo forward link for lastpage
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'>>></a> ";
} // end if
/****** end build pagination links ******/
echo '</div><div id="rightzone">' ;
//include 'random.php' ;
echo '</div>
<div id="footer">' ;
include "../includes/footer.html" ;
echo '</div>
</div>
</body>
</html>' ;
?>