Jump to content

Search Engine help


Paul_Withers

Recommended Posts

Hi, I have a script which searches the MySQL database. It outputs the result which is a business name, but I need the business name to be in a link so the user can click on it and go to another page to find out more information. However the business name is not included in the link, but the $value is.

 

Here is my script

<?php
include 'init.php';

// normal search
$result_tb = "";
if (!empty($_POST['SEARCH']) && !empty($_POST['search_value'])) {

   $e = $_POST['search_value'];

   $query = 'SELECT aquaticCenterName FROM reviews WHERE ' .
           "aquaticCenterName LIKE '%$e%' OR " .
           "town LIKE '%$e%' OR " .
           "county LIKE '%$e%' OR " .
           "country LIKE '%$e%' OR " .
           "rating LIKE '%$e%' ";

   $query_result = $con->query($query);

 $result_tb = '<div>';
   while ($rows = $query_result->fetch_assoc()) {
      foreach ($rows as $k => $v) {
         $result_tb .= '<a href="reviewResults.php?aquaticCenterName= . $v . ">' . $v . '</a><br>';
      }
   }
   $result_tb .='</div><hr>';

   $con->close();
}
?>
<?php
include 'includes/overall/header.php';
include 'includes/logo.php';
?>
      <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
         <table>
            <tr>
               <td>
                  <input type="text" name="search_value" size="30" maxlength="30"/>
               </td>
               <td>
                  <input type="submit" name="SEARCH" value="Search"/>
               </td>
            </tr>
         </table>
      </form>
      <?php echo $result_tb; ?>
<?php
include 'includes/overall/footer.php';

But when I click on the link, I get this in the address bar

 

reviewResults.php?aquaticCenterName=%20.%20$v%20.

 

How do I get this to read

 

eg, reviewResults.php?aquaticCenterName=PaulsAquaticCenter

 

Many Thanks

 

aquaman

 

 

 

Link to comment
Share on other sites

I have tried changing it to

 

         $result_tb .= '<a href="reviewResults.php?aquaticCenterName='$v'">' . $v . '</a><br>';
 
but I get this error
 
Parse error: syntax error, unexpected '$v' (T_VARIABLE) in /Applications/XAMPP/xamppfiles/htdocs/reviews.php on line 23
 
How do I get the value of $v into the link?
Link to comment
Share on other sites

Hi and thanks for your reply. I made the change as suggested, but now when I search, I do not get any results.  Here is the code now

<?php
include 'init.php';

// normal search
$result_tb = "";
if (!empty($_POST['SEARCH']) && !empty($_POST['search_value'])) {

   $e = $_POST['search_value'];

   $query = 'SELECT aquaticCenterName FROM reviews WHERE ' .
           "aquaticCenterName LIKE '%$e%' OR " .
           "town LIKE '%$e%' OR " .
           "county LIKE '%$e%' OR " .
           "country LIKE '%$e%' OR " .
           "rating LIKE '%$e%' ";

   $query_result = $con->query($query);

 $result_tb = '<div>';
   while ($rows = $query_result->fetch_assoc()) 
{
   $v = $rows['aquaticCenterName'];
   $result_tb .= "<a href='reviewResults.php?aquaticCenterName=$v>$v</a><br>";
}
   }
   $result_tb .='</div><hr>';

   $con->close();

?>
<?php
include 'includes/overall/header.php';
include 'includes/logo.php';
?>
      <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
         <table>
            <tr>
               <td>
                  <input type="text" name="search_value" size="30" maxlength="30"/>
               </td>
               <td>
                  <input type="submit" name="SEARCH" value="Search"/>
               </td>
            </tr>
         </table>
      </form>
      <?php echo $result_tb; ?>
<?php
include 'includes/overall/footer.php';
Link to comment
Share on other sites

the href='...' attribute in the link is missing the closing ', which is probably breaking the rest of the html on the page. use the following inside the loop - 

$u = $rows['aquaticCenterName'];
$v = urlencode($u); // since this is a text name, make it url encoded
$result_tb .= "<a href='reviewResults.php?aquaticCenterName=$v'>$u</a><br>";

i would use the id, not the name, as the value being put into the link - reviewResults.php?id=123

and you can simplify the multiple col LIKE '%$e%' OR ... term in the query to be - 

$query = "SELECT aquaticCenterName FROM reviews
WHERE CONCAT_WS(' ', aquaticCenterName, town, county, country, rating) LIKE '%$e%'";
Link to comment
Share on other sites

Hi, thanks for your reply. Error checking is on and no errors are reported.

 

Here is the echoed query

 

SELECT aquaticCenterName FROM reviews WHERE aquaticCenterName LIKE '%Chelmsford%' OR town LIKE '%Chelmsford%' OR county LIKE '%Chelmsford%' OR country LIKE '%Chelmsford%' OR rating LIKE '%Chelmsford%'

 

And here is the echoed $v

 

Lissies Aquatics

 

This is the result of the query. I just cant get the value of $v to work in a link

Link to comment
Share on other sites

Hi mac_gyver, thanks for your reply. That all works great, apart from when the resulting aquatic center contains a + in between the words

 

eg. http://localhost/reviewResults.php?aquaticCenterName=Lissies+Aquatics

 

But I wont be able to retrieve information from the database with the + being there. Is it possible to remove this?

 

 

Many Thanks

 

aquaman

Link to comment
Share on other sites

did you actually try? the + in the link is the encoded space (because space characters are not valid in links). the + will be automatically converted by php (it does a urldecode on $_GET data) to a space and using the value in a database query will work.
 
you could always do what i suggested, which will eliminate the problem of trying to pass a space in the link - 
 

i would use the id, not the name, as the value being put into the link - reviewResults.php?id=123

Link to comment
Share on other sites

Hi mac_gyver, yes that did work, thank you

 

Now I have another search to search for items for sale. I have this working, with the $user_id being the result. But I don't want this to be displayed in the results, I would like the $aquaticCenterName to be displayed instead. ($user_id is going to be changed to id as it has nothing to do with the user).

 

Here is what I got so far

<?php
include 'init.php';

// normal search
$result_tb = "";
if (!empty($_POST['SEARCH']) && !empty($_POST['search_value'])) {

   $e = $_POST['search_value'];

  $query = "SELECT user_id FROM live_sales
WHERE CONCAT_WS(' ', user_id, listing_title, fishtype, speciesCommon, speciesScientific, town, county) LIKE '%$e%'";

   $query_result = $con->query($query);
 $result_tb = '<div><h2>Search Results:</h2>';
   while ($rows = $query_result->fetch_assoc()) {
      foreach ($rows as $k => $v) {
     $u = $rows['user_id'];
$v = urlencode($u); // since this is a text name, make it url encoded
$result_tb .= "<a href='searchResultslive.php?user_id=$v'>$u</a><br>";
      }
      
   }
   $result_tb .='</div>';

   $con->close();
}
?>
<?php
include 'includes/overall/header.php';
include 'includes/logo.php';
?>
<h1>Search Species for Sale</h1>
<p>Search for the aquatic species of your choice.</p>
<p> You can search for your item via species type, name, scientific name, town or county.</p>
      <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
         <table>
            <tr>
               <td>
                  <input type="text" name="search_value" size="40" maxlength="40" value="Search for species"/>
               </td>
               <td>
                  <input type="submit" name="SEARCH" value="Search"/>
               </td>
            </tr>
         </table>
      </form>
      <?php echo $result_tb; ?>
<?php
include 'includes/overall/footer.php';

So ideally the results would show the Aquatic Center Name, but the link would be results.php?user_id=2 ?

 

Many Thanks

 

aquaman

Link to comment
Share on other sites

Why do you loop through each field in each returned row? Lose the foreach()

 

while ($rows = $query_result->fetch_assoc()) {
    foreach ($rows as $k => $v) {
        $v = $rows['user_id'];

        $u = $rows['listing_title'];
        $v = urlencode($v); // since this is a text name, make it url encoded
        $result_tb .= "<a href='searchResultslive.php?user_id=$v'>$u</a><br>";
    } 

}

Link to comment
Share on other sites

  • 6 months later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.