PHP SEARCH CODE PROBLEM

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rach99z
Forum Newbie
Posts: 2
Joined: Thu Feb 07, 2008 10:23 pm

PHP SEARCH CODE PROBLEM

Post by rach99z »

Hi All,

I am trying to get this code working for my advanced search page. I keep getting an error:

Parse error: syntax error, unexpected $end in /home/rach99/public_html/client/search_adv.php on line 298

Which i researched and it said a bracket is missing. but i can't seem to work out from where?

This code was working as a normal search, this is the new code i added:

Code: Select all

 
 
 if (isset($_GET['search'])) {
  $query_sub = "";
  
  //set a flag to use if you need a comma
  $comma_flag = 0;
  //see if 'name' is not blank
  $trimmed = trim($_GET['name']);
  if ($trimmed != "") {
    $query_sub .= "tassearch.ResName LIKE '%$trimmed%'";
    $comma_flag = 1;
  }
  $trimmed = trim($_GET['cuisine']);
  if ($trimmed != "") {
    if ($comma_flag == 1) {
      $query_sub .= " AND ";
    }
    $query_sub .= "tascuisine.ResCuisine LIKE '%$trimmed%'";
    $comma_flag = 1;
  }
  $trimmed = trim($_GET['suburb']);
  if ($trimmed != "") {
    if ($comma_flag == 1) {
      $query_sub .= " AND ";
    }
    $query_sub .= "tassearch.ResSuburb LIKE '%$trimmed%'";
}
$query = "SELECT COUNT(*)
          FROM `{$tablename}`
          WHERE {$query_sub}`
          ORDER BY `{$ordercolumn}`";
 
 
This is it added to my old code:

Code: Select all

 
<?php
// Set up some vars to use:
$tablename = 'tassearch'; // Change to the table to search
$searchcolumn = 'ResName'; // Change to the column to search
$searchcolumnb = 'ResSuburb';
$searchcolumna = 'ResCuisine';
$ordercolumn = 'ResName'; // Change to the column to order by
?>
<P>Welcome to example</P>
 
<?php
 
  // Get the search variable from URL
  $var = $_GET['q'] ;
  $trimmed = trim($var); //trim whitespace from the stored variable
 
// If s is sent, then grab it.  If it's less than 0, make it 0 <!-- s;) --><img src=\"{SMILIES_PATH}/icon_wink.gif\" alt=\";)\" title=\"Wink\" /><!-- s;) -->
  $s = 0;
  if(isset($_GET['s']))
  {
      $s = $_GET['s'];
      if($s < 0)
          $s = 0;
  }
 
// rows to return
$limit=10;
 
// check for an empty string and display a message.
if ($trimmed == "")
  {
  echo "<p>Please enter a search...</p>";
  exit;
  }
 
// secure the "trimmed" value
$trimmed = mysql_real_escape_string($trimmed);
 
// check for a search parameter
if (!isset($var))
  {
  echo "<p>We dont seem to have a search parameter!</p>";
  exit;
  }
  
  if (isset($_GET['search'])) {
  $query_sub = "";
  
  //set a flag to use if you need a comma
  $comma_flag = 0;
  //see if 'name' is not blank
  $trimmed = trim($_GET['name']);
  if ($trimmed != "") {
    $query_sub .= "tassearch.ResName LIKE '%$trimmed%'";
    $comma_flag = 1;
  }
  $trimmed = trim($_GET['cuisine']);
  if ($trimmed != "") {
    if ($comma_flag == 1) {
      $query_sub .= " AND ";
    }
    $query_sub .= "tascuisine.ResCuisine LIKE '%$trimmed%'";
    $comma_flag = 1;
  }
  $trimmed = trim($_GET['suburb']);
  if ($trimmed != "") {
    if ($comma_flag == 1) {
      $query_sub .= " AND ";
    }
    $query_sub .= "tassearch.ResSuburb LIKE '%$trimmed%'";
}
$query = "SELECT COUNT(*)
          FROM `{$tablename}`
          WHERE {$query_sub}`
          ORDER BY `{$ordercolumn}`";
          
// Build SQL Query  
$query = "SELECT COUNT(*)
          FROM `{$tablename}`
          WHERE `{$searchcolumn}` LIKE '%$trimmed%' or `{$searchcolumnb}` LIKE '%$trimmed%'
          ORDER BY `{$ordercolumn}`"; // EDIT HERE and specify your table and field names for the SQL query
 
$sql = "SELECT a.tascuisine, t.ResName
        FROM tascuisine a
            INNER JOIN linktable lt ON a.CusId = lt.FK_CusId
            INNER JOIN tablesearch t ON lt.FK_ResNameId = t.ResNameId
        WHERE a.CusId = $selectedCusId";
 
 
$rslt=mysql_query($query) or die('MySQL Error: ' . mysql_error());
$numrows = mysql_result($rslt, 0, 0);
@mysql_free_result($rslt);
 
// If we have no results, offer a google search as an alternative
 
if ($numrows == 0)
{
  echo "<h4>Results</h4>";
  echo "<p>Sorry, your search: "" . $trimmed . "" returned zero results</p>";
}
 
// get results
  $query = "SELECT *
            FROM `{$tablename}`
            WHERE `{$searchcolumn}` LIKE '%{$trimmed}%' or `{$searchcolumnb}` LIKE '%{$trimmed}%'
            ORDER BY `{$ordercolumn}`
            LIMIT {$s}, {$limit}";
            
            
  $result = mysql_query($query) or die("Couldn't execute query");
 
// display what the person searched for
echo "<p>You searched for: "" . $var . ""</p>";
 
// begin to show results set
echo "<p>Results</p>";
$count = 1 + $s ;
  
while ($row = mysql_fetch_array($result)) {
  $link = $row['ResLink'];
  $link_title = $row['ResName'];
 
  echo $count . '.) <a href="' . $link . '">' . $link_title . '</a><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=\"$PHP_SELF?s=$prevs&q=$var\"><<
  Prev 10</a>&nbsp&nbsp;";
  }
 
// calculate number of pages needing links
  $pages=intval($numrows/$limit);
 
// $pages now contains int of pages needed unless there is a remainder from division
 
  if ($numrows%$limit) {
  // has remainder so add one page
  $pages++;
  }
 
// check to see if last page
  if (!((($s+$limit)/$limit)==$pages) && $pages!=1) {
 
  // not last page so give NEXT link
  $news=$s+$limit;
 
  echo "&nbsp;<a href=\"$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>";
  
?> 
 
Any help would be great!

Thanks

Rachael
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: PHP SEARCH CODE PROBLEM

Post by Christopher »

What is line 298? And why is $end appearing where it should not be?
(#10850)
rach99z
Forum Newbie
Posts: 2
Joined: Thu Feb 07, 2008 10:23 pm

Re: PHP SEARCH CODE PROBLEM

Post by rach99z »

no idea.

that line is after my html brackets close.

i figured out that i needed a close bracket for my 'if' statement though.

but my search results are not showing up now i think there is a problem with obtaining my results.

my form is below:

<form action="search_adv.php" method="get" name="search">

name <input name="name" type="text" value="" size="15">
cuisine <input name="cuisine" type="text" value="" size="15">
suburb <input name="suburb" type="text" value="" size="15">
<input name="search" type="submit" value="Search">

</form>

Can anyone see anything that does not link my code to my form or database properly?

any help would be greatly appreciated.

do i need to place the php in my form page?
Post Reply