Jump to content

Each search result is displayed 3 times


aquapetcentre
Go to solution Solved by gizmola,

Recommended Posts

Hi, I have the following page containing the search engine. All works fine, except for the face that it retrieves the information from the database, and displays each result 3 times. Have no idea why this is happening when my other search results are fine

<?php
include 'init.php';

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

   $e = $_POST['search_value'];

  $query = "SELECT aquaticCenterName, town, county FROM reviews
WHERE CONCAT_WS(' ', aquaticCenterName, town, county) LIKE '%$e%'";

   $query_result = $con->query($query);
 $result_tb = '<div><h2>Aquatic Center Name:</h2>';
   while ($rows = $query_result->fetch_assoc()) {
      foreach ($rows as $k => $v) {
     $u = $rows['aquaticCenterName'];
     $r = $rows['town'];
     $s = $rows['county'];
     $t = urlencode($r); // since this is a text name, make it url encoded
	 $z = urlencode($s); // since this is a text name, make it url encoded
	 $v = urlencode($u); // since this is a text name, make it url encoded
$result_tb .= "<hr> <a href='reviewResults.php?aquaticCenterName=$v'><strong><p1>Aquatic Centre Name:</strong> $u - <strong>Town:</strong> $t - <strong>County:</strong> $z</a></p1><br>";

      }
      
   }
   $result_tb .='</div>';

   $con->close();
}
?>
<?php
include 'init.php';
include 'includes/overall/header.php';
?>


</br>
<div class="centerDiv">
	<h4>Search Aquatic Center Reviews</h4>
<p>Want to find the best aquatic centre in your area? You can search via store name, town or county.</p>
<p>Alternatively <a href="leaveReview.php">Click Here</a> to leave a review.</p>
    <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
        
<p class="register">
	<input type="text" name="search_value" size="30" maxlength="30" value="Search for store name, town or county"/>
    <input type="submit" name="SEARCH" value="Search"/>
</p>
              
      </form>
</div>
      <?php echo $result_tb; ?>
    
 


<?php
include 'includes/overall/footer.php';

Please help!!!

 

http://www.aquapetcentre.com

Link to comment
Share on other sites

  • Solution

It's really a simple error on your part. You have an extraneous foreach loop.

 

foreach ($rows as $k => $v) {
What does this do exactly? Well in your case you have a single row, even though you named it rows. That variable is an associative array of the columns in the row. Since there are apparently 3 columns, you cycle through your output 3 times.

 

Remove the foreach and it's end block and everything should be what you want.

 

Naming variables $v, $u etc. isn't really a great idea, unless they are truly throw away counters.

 

You also don't need to make extra temp variables all over the place.

 

     $u = $rows['aquaticCenterName'];
     $r = $rows['town'];
     $s = $rows['county'];
Why? $rows is already a variable. Just use it directly rather than making 3 more variables.
Link to comment
Share on other sites

It's really a simple error on your part. You have an extraneous foreach loop.

 

foreach ($rows as $k => $v) {
What does this do exactly? Well in your case you have a single row, even though you named it rows. That variable is an associative array of the columns in the row. Since there are apparently 3 columns, you cycle through your output 3 times.

 

Remove the foreach and it's end block and everything should be what you want.

 

Naming variables $v, $u etc. isn't really a great idea, unless they are truly throw away counters.

 

You also don't need to make extra temp variables all over the place.

 

     $u = $rows['aquaticCenterName'];
     $r = $rows['town'];
     $s = $rows['county'];
Why? $rows is already a variable. Just use it directly rather than making 3 more variables.

 

 

 

Hi thank you, it all works now :)

Link to comment
Share on other sites

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.