John Davidson

php - Echo comma separated value from MySql in search bar separately

0 comments
Message:


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



My Answer:
You can achieve this by exploding the comma-separated values from the database and then checking if the keyword matches any of the values. Here's an example code snippet to help you achieve this:

php
// Assuming $keyword contains the search keyword entered by the user

// Fetch the data from the database
$query = "SELECT Title FROM My_DB_Name";
$result = mysqli_query($connection, $query);

// Initialize an empty array to store the results
$searchResults = array();

// Loop through the results
while ($row = mysqli_fetch_assoc($result)) {
$titles = explode(",", $row['Title']);

// Check if the keyword matches any of the values
foreach ($titles as $title) {
if (stripos($title, $keyword) !== false) {
$searchResults[] = $title;
}
}
}

// Display the search results
foreach ($searchResults as $result) {
echo $result . "
";
}


This code snippet will explode the comma-separated values from the database and check if the keyword matches any of the values. If a match is found, it will store the result in the `$searchResults` array and then display the results separately.

Rate this post

3 of 5 based on 5571 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us