1

I wrote this code to search users from mysql :

$query = $db->query("SELECT * FROM `users` WHERE `id` = {$id} ");
while($row = $query->fetch_assoc()){
    echo $row['username'];
    // ....
    echo "People also search :";
    // the people also search box
}

I want it so that when the user pages load in the bottom of the page be a box like this:

Walter

...

People also search :

Bruce Kevin Jesse

something like the Google when you search actors or ...

how could I do this ?

Richard
  • 325
  • 7
  • 23
Daniel
  • 108
  • 17

2 Answers2

0

why don't you start by getting it to work with some random values from the database response

$query = $db->query("SELECT username FROM `users");
while($row = $query->fetch_assoc()){
     $rows[]=$row
}
//id of the user by which you have made the search
echo $rows[$id];
unset($rows[$id]);
echo "People also search :".PHP_EOL;
$rand_keys = array_rand($rows, 5);
foreach($rand_keys as $key){
    echo $rows[$key];
}
-1

You'd need to keep a record of past searches and then when a new one is searched find all the people who searched for that in the past and find what else they searched for.

Mike
  • 457
  • 3
  • 13
  • it would be a lot of search and i just want to show the similar users – Daniel Mar 28 '18 at 17:30
  • OK, how do you define "similar"? If just similar names then you can do a text search https://dev.mysql.com/doc/refman/5.7/en/fulltext-search.html – Mike Mar 28 '18 at 17:33
  • If you're using something else to define similar (eg permissions) then you'd need a way to compare those. – Mike Mar 28 '18 at 17:35
  • OK my initial one would be people who searched for this also searched for these. If you just want ones with similar text (eg correct for spelling mistakes) then this question would probably help you: https://stackoverflow.com/questions/3338889/how-to-find-similar-results-and-sort-by-similarity – Mike Apr 18 '18 at 21:36
  • But if you want how google works when searching for actorsthen you're going to need a list of attribute that make the results "similar". In that movie example you'll get actors who worked on the same movies as the person you searched for so you'll need to have a database of connections and return the 3 with the most number of connections to what you searched for. – Mike Apr 18 '18 at 21:38