Jump to content

Each search result is displayed 3 times


aquapetcentre

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
https://forums.phpfreaks.com/topic/295987-each-search-result-is-displayed-3-times/
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.

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 :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.