Omar
Omar

Reputation: 31732

Search multiple by keyword

I have the below code and it works well. However, I am limited to search two rows only. I want to search another two rows (total 4 rows). I have tried several techniques but I couldn't get it working. Sometimes I get duplicated results.

<?php

$term = $_POST['term'];

$connect = new mysqli("localhost", "root", "hala3ammi", "phprealty");

$sql = "
 SELECT phprealty_property.*, phprealty_prop_img.p_id, phprealty_prop_img.fn
 FROM phprealty_property
 INNER JOIN phprealty_prop_img
 ON phprealty_property.id = phprealty_prop_img.p_id 
 WHERE phprealty_prop_img.def='1' 
 AND (phprealty_property.title like '%$term%'
 OR phprealty_property.full_desc like '%$term%')";

$query = mysqli_query($connect, $sql) or die (mysqli_error());

$result = mysqli_query($connect, $sql);

$found = mysqli_num_rows($result);

echo '<br/> Search result(s): '.$found;

while ($row = mysqli_fetch_array($result)){
$img = $row["fn"];
$thumb = 'th_' . $img;
$duh = '<img src="../falcon/listImgs/' . $thumb . '" />';
echo '<br/> Title: '.$row['title'];
echo '<br/> Price: '.$row['price'];
echo '<br/> Type: '.$row['type'];
echo '<br/> Image: '.$duh;
echo '<br/> Full Desc: '.$row['full_desc'];
echo '<br/><br/>';
}
?>

Database:

phprealty

Table where I want to search:

phprealty_property

The INNER join is meant for retrieving default thumbnails from the below table.

phprealty_prop_img

WHERE phprealty_prop_img.def='1'

Thank you in advance.

Upvotes: 0

Views: 82

Answers (1)

Dom
Dom

Reputation: 3100

I think the reason you might be getting results which appear to be duplicate is your inner join. If you have n rows in phprealty_prop_img that correspond to a single property listing in phprealty_property, (ie n rows in phprealty_prop_img with the same value p_id) then you will get n rows returned by your query, each one with identical information drawn from phprealty_property and only varying in phprealty_prop_img.fn. Task a look at the inner join example given on the W3C site http://www.w3schools.com/sql/sql_join_inner.asp

As to searching by phprealty_property.city & phprealty_property.short_desc, I don't understand the problem. Why can't you just add these into the OR clause at the end this this?

AND (phprealty_property.title like '%$term%' 
      OR phprealty_property.full_desc like '%$term%' 
      OR phprealty_property.city LIKE '%$city%'
      OR phprealty_property.short_desc LIKE '%$term%')

Upvotes: 1

Related Questions