My DB
Name
Title
Abu,Ali
Red Shoes
Mia,Sarah
Yellow shoes
I created a search bar using LIKE '%$keyword%'. When enter keyword for example Abu or Red shoes the result i want to be appear is:
Abu
Red Shoes
Ali
Red Shoes
I only get the result like this:
Abu,Ali
Red Shoes
How can i separate the result without separating then in the DB...I need to store the data in comma value
CodePudding user response:
after you get the values in php you can iterate over the names and use the explode function of php. Please be aware that this database is set up to fail, it will very likely cause problems and headaches in the future. It is better to seperate the names into individual rows. However if you really cannot do it any other way, this is how you can seperate the names (I assume you get the SQL result into a variable called $result)
if($result->num_rows > 0){
while($row = $result->fetch_assoc()){
$names = explode(",", $row["Name"]);
for($i = 0; $i < count($names); $i ){
echo "Name: " . $names[$i] . "Title: " . $row["Title"];
//In this for-loop you just iterate over the names and print them into
//the page. The first loop would be "Name: Abu Title: Red Shoes"
}
}
}