Reputation: 9
I have developed a website with a user profile page with eye_colour , hair_colour , size etc. Each item has a set range of values entered through a drop down list. I wish to allow the users to search these set range’s to find their ideal match. To this end I have a search form set up and a number of SELECT statements feeding to array seach using the tuple user. i.e.
<?php
$var1 = SELECT ‘user’ FROM ‘profile’ WHERE ‘hair_colour’ = $hair;
$var2 = SELECT ‘user’ FROM ‘profile’ WHERE ‘eye_colour’ = $eye;
$var3………4………..5………..n etcc
?>
I think there is a function which allows all the arrays to be taken in and unioned ALL together, then sorted/weighted by the most occurances of the user and all items can then be displayed. Does anyone know if such a php function exists and if it does what its name is? I could try the SQL if such a function doesn’t exist but I’d definitely make a few mistakes!! Thanks for any help!
Upvotes: 0
Views: 124
Reputation: 7157
I would use a single query first to get the rows you want:
$sql = "SELECT * FROM profile WHERE (hair = '$hair_esc') OR (eye = '$eye_esc')";
$result = mysql_query($sql);
Then loop over the results, assigning a score to each row:
$rows = array();
while ($row = mysql_fetch_array($result)){
$row['score'] = 0;
if ($row['hair'] == $hair) $row['score']++;
if ($row['eye'] == $eye ) $row['score']++;
$rows[] = $row;
}
And then finally sort the list by the score:
usort($rows, 'sort_by_score');
function sort_by_score($a, $b){
return $a['score'] - $b['score'];
}
Upvotes: 1