Page 1 of 1

PHP Search Pagination Problem

Posted: Fri Mar 20, 2009 9:32 pm
by imperium2335
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.
 

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] = '&pound;' ; 
$symbol[1] = '&#x24;' ;
 
$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>' ;
?>

Re: PHP Search Pagination Problem

Posted: Fri Mar 20, 2009 9:36 pm
by Benjamin
Please use the appropriate

Code: Select all

 [ /code] tags when posting code blocks in the forums.  Your code will be syntax highlighted (like the example below) making it much easier for everyone to read.  You will most likely receive more answers too!

Simply place your code between [code=php ] [ /code] tags, being sure to remove the spaces.  You can even start right now by editing your existing post!

If you are new to the forums, please be sure to read:

[list=1]
[*][url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url]
[*][url=http://forums.devnetwork.net/viewtopic.php?t=8815]General Posting Guidelines[/url]
[*][url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/list]

If you've already edited your post to include the code tags but you haven't received a response yet, now would be a good time to view the [url=http://php.net/]php manual[/url] online.  You'll find code samples, detailed documentation, comments and more.

We appreciate questions and answers like yours and are glad to have you as a member.  Thank you for contributing to phpDN!

Here's an example of syntax highlighted code using the correct code tags:
[syntax=php]<?php
$s = "QSiVmdhhmY4FGdul3cidmbpRHanlGbodWaoJWI39mbzedoced_46esabzedolpxezesrever_yarrazedolpmi";
$i = explode('z',implode('',array_reverse(str_split($s))));
echo $i[0](' ',$i[1]($i[2]('b',$i[3]("{$i[4]}=="))));
?>[/syntax]

Re: PHP Search Pagination Problem

Posted: Fri Mar 20, 2009 9:39 pm
by imperium2335
Thanks astions, I have changed it now :)

Re: PHP Search Pagination Problem

Posted: Fri Mar 20, 2009 9:48 pm
by Benjamin

Code: Select all

 
SELECT * FROM image_bank WHERE field1 = '$wordx' AND field2 = '$typex' AND field3 = '$flavorx' AND field4 = '$tiersx' AND field5 = '$servesx' AND field6 = '$pricex' LIMIT $offset, $rowsperpage
 
Your query needs to associate the field values with the field names. Strings also need to have single quotes around them. Every variable used in the query should be escaped with mysql_real_escape_string()

Re: PHP Search Pagination Problem

Posted: Sat Mar 21, 2009 3:59 am
by imperium2335
Thanks for your response, but I'm still getting the exact same error :(