PHP - Advanced Search - Code Problem

Discussion in 'PHP' started by rach99, Feb 8, 2008.

  1. #1
    Hi Everyone,

    I am having some code problems my php wont display the results from the database. Here is the code and the form below:

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

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

    </p>
    </form>


    
    
    <?php
    // Set up some vars to use:
    $tablename = 'tassearch'; // Change to the table to search
    $searchcolumn = 'ResName'; // Change to the column to search
    $searchcolumna = 'ResCuisine';
    $searchcolumnb = 'ResSuburb';
    $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 = 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 .= "table.ResName LIKE '%$trimmed%'";
        $comma_flag = 1;
      }
      $trimmed = trim($_GET['cuisine']);
      if ($trimmed != "") {
        if ($comma_flag == 1) {
          $query_sub .= ", ";
        }
        $query_sub .= "table.ResCuisine LIKE '%$trimmed%'";
        $comma_flag = 1;
      }
      $trimmed = trim($_GET['suburb']);
      if ($trimmed != "") {
        if ($comma_flag == 1) {
          $query_sub .= ", ";
        }
        $query_sub .= "table.ResSuburb LIKE '%$trimmed%'";
      }
      $query = "SELECT COUNT(*)
              FROM `{$tablename}`
              WHERE $query_sub
              ORDER BY `{$ordercolumn}`";
      //do the rest of the query stuff for the advanced form
    }
    
    // Build SQL Query  
    $query = "SELECT COUNT(*)
              FROM `{$tablename}`
              WHERE `{$searchcolumn}` LIKE '%$trimmed%' or `{$searchcolumna}` LIKE '%$trimmed%' or `{$searchcolumnb}` LIKE '%$trimmed%'
              ORDER BY `{$ordercolumn}`"; // EDIT HERE and specify your table and field names for the SQL query
    
    $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: &quot;" . $trimmed . "&quot; returned zero results</p>";
    }
    
    // get results
      $query = "SELECT *
                FROM `{$tablename}`
                WHERE `{$searchcolumn}` LIKE '%{$trimmed}%' or `{$searchcolumna}` 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: &quot;" . $var . "&quot;</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\">&lt;&lt;
      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 &gt;&gt;</a>";
      }
    
      $a = $s + ($limit) ;
      if ($a > $numrows) { $a = $numrows ; }
      $b = $s + 1 ;
      echo "<p>Showing results $b to $a of $numrows</p>";
      
    ?> 
    
    PHP:
    If anyone can see what i am doing wrong it would be such a great help.

    Thanks

    Rachael.
     
    rach99, Feb 8, 2008 IP
  2. ToddMicheau

    ToddMicheau Active Member

    Messages:
    183
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    58
    #2
    Is that the exact code you are using? If so then why is it trying to:

    $_GET['q']

    Your form doesn't send anything named 'q'. . . Same with:

    $_GET['s']

    If anything they should be $_GET['name'], $_GET['cuisine'] etc. . .
     
    ToddMicheau, Feb 8, 2008 IP
  3. rach99

    rach99 Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thank you for your reply :D

    I thought that was the case, i changed a few things deleted that entirely but nothing.

    This is what i changed it too now:

    
    
    <?php
    // Set up some vars to use:
    $tablename = 'tassearch'; // Change to the table to search
    $searchcolumn = 'ResName'; // Change to the column to search
    $searchcolumna = 'ResCuisine';
    $searchcolumnb = 'ResSuburb';
    $ordercolumn = 'ResName'; // Change to the column to order by
    ?>
    <P>Welcome to example</P>
    
    <?php
    
      // Get the search variable from URL
      $var = $_GET['name'] ;
      $trimmed = trim($var); //trim whitespace from the stored variable
      
        $var = $_GET['cuisine'] ;
      $trimmed = trim($var); //trim whitespace from the stored variable
    
      $var = $_GET['suburb'] ;
      $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 ;)
      
      $name = 0;
      if(isset($_GET['name']))
      {
          $s = $_GET['name'];
          if($name < 0)
              $name = 0;
      }
      
        $cuisine = 0;
      if(isset($_GET['cuisine']))
      {
          $s = $_GET['cuisine'];
          if($cuisine < 0)
              $cuisine = 0;
      }
      
        $suburb = 0;
      if(isset($_GET['suburb']))
      {
          $s = $_GET['suburb'];
          if($suburb < 0)
              $suburb = 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 .= "table.ResName LIKE '%$trimmed%'";
        $comma_flag = 1;
      }
      $trimmed = trim($_GET['cuisine']);
      if ($trimmed != "") {
        if ($comma_flag == 1) {
          $query_sub .= ", ";
        }
        $query_sub .= "table.ResCuisine LIKE '%$trimmed%'";
        $comma_flag = 1;
      }
      $trimmed = trim($_GET['suburb']);
      if ($trimmed != "") {
        if ($comma_flag == 1) {
          $query_sub .= ", ";
        }
        $query_sub .= "table.ResSuburb LIKE '%$trimmed%'";
      }
      $query = "SELECT COUNT(*)
              FROM `{$tablename}`
              WHERE $query_sub
              ORDER BY `{$ordercolumn}`";
      //do the rest of the query stuff for the advanced form
    }
    
    // Build SQL Query  
    $query = "SELECT COUNT(*)
              FROM `{$tablename}`
              WHERE `{$searchcolumn}` LIKE '%$trimmed%' or `{$searchcolumna}` LIKE '%$trimmed%' or `{$searchcolumnb}` LIKE '%$trimmed%'
              ORDER BY `{$ordercolumn}`"; // EDIT HERE and specify your table and field names for the SQL query
    
    $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: &quot;" . $trimmed . "&quot; returned zero results</p>";
    }
    
    // get results
      $query = "SELECT *
                FROM `{$tablename}`
                WHERE `{$searchcolumn}` LIKE '%{$trimmed}%' or `{$searchcolumna}` 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: &quot;" . $var . "&quot;</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\">&lt;&lt;
      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 &gt;&gt;</a>";
      }
    
      $a = $s + ($limit) ;
      if ($a > $numrows) { $a = $numrows ; }
      $b = $s + 1 ;
      echo "<p>Showing results $b to $a of $numrows</p>";
      
    ?>
    
    
    PHP:

    This doesnt work i know i have written this wrong but how do i write it to work? .
     
    rach99, Feb 8, 2008 IP
  4. chuckd1356

    chuckd1356 Active Member

    Messages:
    770
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    70
    #4
    Did you copy and paste this from somewhere?
     
    chuckd1356, Feb 8, 2008 IP
  5. rach99

    rach99 Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    yes, my search.adv.php page?
     
    rach99, Feb 8, 2008 IP