0

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!

3
  • php.net/implode for your "array unioned all". And then I hope the bad quoting in your code sample is just due to whatever text editor your wrote up the question in, because SQL does not like "smart quotes". And consider just using WHERE hair_color = '$hair' OR eye_colour = '$eye' OR .... and so on. There is absolutely no need for a UNION query for such a simplistic situation. Commented Apr 9, 2012 at 4:40
  • look the extract Commented Apr 9, 2012 at 4:43
  • Yeah I'll blaim the editor. Didn't copy directly from the code just entered the question on word and didn't even think of the smart quotes. should have used these I believe. Thanks for the solution guys Commented Apr 9, 2012 at 4:57

1 Answer 1

1

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'];
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.