简体   繁体   中英

Echo comma separated value from MySql in search bar separately

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

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"
        }
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM