People can search like "Jonathan Steve"
If i type only Jonathan, i get the result. But if i type Jonathan Steve there is no answer from php
Database: user_first | user_last Jonathan Steve
The user should type in one input "Jonathan Steve" and should get the result
EDIT:
<?php
session_start();
require_once("../config.php");
require_once("../inc/function.php");
$query = htmlspecialchars($mysqli->real_escape_string($_GET['q']));
$query = explode(" ", $query);
foreach($query as $search)
{
$searching = $mysqli->query("SELECT * FROM user_tbl WHERE LOWER(user_first) LIKE LOWER('%$search%') OR LOWER(user_last) LIKE LOWER('%$search%') OR LOWER(user_uname) LIKE LOWER('%$search%')");
echo "<h5 style=\"padding-left: 5px;\">Personen</h5>";
while($result = $searching->fetch_assoc())
{
$name = $result['user_first']." ".$result['user_last'];
$uname = $result['user_uname'];
$image = $result['user_img'];
$fid = $result['user_id'];
if(empty($image))
{
$image = "nop.jpg";
}
else
{
$image = "$fid/profileimages/$image";
}
echo "<div class='search-user'><a href='profile.php?p=$uname'><div style='background: url(_usr/$image) no-repeat center center; background-size: cover' class='imgdiv'></div><span>$name</span></a></div>";
}
}
What is the problem?
from what i can tell the below code is what you want. Instead of searching for "Mark" OR "Phillips", you should search for "Mark" & "Phillips".
What if there are two users called "Mark", it would match the first user even if that wasnt me.
Give it a go, totally untested, i hope it helps somewhat.
<?php
session_start();
require_once("../config.php");
require_once("../inc/function.php");
//First "explode" their name
$personsFullName = strtolower(htmlspecialchars($_GET['q']));
$personsNameExplosion = explode(" ", $personsFullName);
$firstName = null;
//If the explosion contains multiple elements, sort it into seperate variables for code-reading value
if ( sizeof($personsnameExplosion) > 1 )
{
$firstName = $personsNameExplosion[0];
$secondName = $personsNameExplosion[1];
}
//Start to build the query
$query = "SELECT * FROM user_tbl WHERE ";
//If we set $firstName, change the query.
if ( $firstName != null )
$query .= "LOWER(user_first) LIKE '%$firstName%' AND LOWER(user_last) LIKE '%$secondName%'"
else
$query = "LOWER(user_first) LIKE '%$personsFullName%' OR LOWER(user_last) LIKE '%$personsFullName%' OR LOWER(user_uname) LIKE '%$personsFullName%'"
//Run the query:
//Note: Please learn about MySQLi bindings to prevent SQL injection.
$queryResult = $mysqli->query($query);
echo "<h5 style=\"padding-left: 5px;\">Personen</h5>";
while($result = $queryResult->fetch_assoc())
{
$name = $result['user_first']." ".$result['user_last'];
$uname = $result['user_uname'];
$image = $result['user_img'];
$fid = $result['user_id'];
if(empty($image))
{
$image = "nop.jpg";
}
else
{
$image = "$fid/profileimages/$image";
}
echo "<div class='search-user'><a href='profile.php?p=$uname'><div style='background: url(_usr/$image) no-repeat center center; background-size: cover' class='imgdiv'></div><span>$name</span></a></div>";
}
?>
If "Jonathan Steve" is a first and last name, your code is trying to match it to user_first
OR user_last
, but not both together. There's nothing in user_first
that matches "Jonathan Steve" and the same applies to user_last
.
Try this instead:
$searching = $mysqli->query("SELECT * FROM user_tbl WHERE
LOWER(concat_ws(' ', user_first, user_last, user_uname)) LIKE LOWER('%$search%')");