I am creating a selection code in php that takes the user input, matches it to a word, and matches that word to a corresponding restaurant in an SQL database. It tries to autocomplete the selection so sometimes the word is multiple words. Ex: user types in ch, ‘hint’ variable is cheeseburger, cheap, chinese etc.. and it will display restauants that have cheeseburgers, cheap food, chinese.. I created code to do that, the $hint variable is where the user input is located
<?php
//use this to show hints
//: $hint ,"<br>"
//Match hint/matched tag to name
// SQL query to select rows where the tags column contains the specified text
$sql = "SELECT name,rating,location,tags FROM restdata WHERE tags LIKE CONCAT('%', ?, '%') ORDER BY rating DESC;";
$stmt = $mysqli->prepare($sql);
$stmt->bind_param("s", $hint);
$stmt->execute();
$result = $stmt->get_result();
// Initialize variables (sessions so it can be accessed in other pages)
$restaurant_name = "";
$restaurant_location = "";
$restaurant_rating = "";
$restaurant_tags = "";
//Makes sure the hint is an actual input
if ($hint === "") {
echo "Input a searching tag please!";
}
//If the input is something - Check if any rows were found
else if ($result->num_rows > 0) {
// Output the names of restaurants that match the hint
// echo "Restaurants matching the hint '$hint':<br>";
while ($row = $result->fetch_assoc()) {
// echo $row['name'] . "<br>";
$restaurant_name = $row['name'];
$restaurant_location = $row['location'];
$restaurant_rating = $row['rating'];
$restaurant_tags = $row['tags'];
$restaurant_div = $restaurant_rating * 20;
?>
<div class="rest-stuff">
<h2 class="restaurant_name" onclick="changeMapUrl()"> <?php echo $restaurant_name ?> </h2>
<div class="star-rating" style="background-image: linear-gradient(to right, gold 0%, gold <?php echo $restaurant_div;?>%, transparent <?php echo $restaurant_div;?>%, transparent 100%);"></div>
<p class="restaurant_rating"> Rating: <?php echo $restaurant_rating ?></p>
<p class="restaurant_location"> <?php echo $restaurant_location ?> </p>
<p class="restaurant_tags">Restaurant Offers: <?php echo $restaurant_tags?></p>
</div>
<br />
<?php
}
}
//if the input is more than one word (iterate over the array and search every item there)
else if (str_contains($hint, ',')) {
$hint_split = array_unique(explode(",", $hint, -1));
// print_r($hint_split);
foreach ($hint_split as $tag) {
echo $tag;
// SQL query to select rows where the tags column contains the specified text
$sql = "SELECT name, rating,location,tags FROM restdata WHERE tags LIKE CONCAT('%', ?, '%') ORDER BY rating DESC";
$stmt = $mysqli->prepare($sql);
$stmt->bind_param("s", $tag);
$stmt->execute();
$result = $stmt->get_result();
// Output the names of restaurants that match the tag
if ($result->num_rows > 0) {
// echo "Restaurants matching the tag '$tag':<br>";
while ($row = $result->fetch_assoc()) {
$restaurant_name = $row['name'];
$restaurant_location = $row['location'];
$restaurant_rating = $row['rating'];
$restaurant_tags = $row['tags'];
$restaurant_div = $restaurant_rating * 20;
?>
<!-- Displays restaurant info -->
<div class="rest-stuff">
<h2 class="restaurant_name" onclick="changeMapUrl()"> <?php echo $restaurant_name ?> </h2>
<div class="star-rating" style="background-image: linear-gradient(to right, gold 0%, gold <?php echo $restaurant_div;?>%, transparent <?php echo $restaurant_div;?>%, transparent 100%);"></div>
<p class="restaurant_rating"> Rating: <?php echo $restaurant_rating ?> </p>
<p class="restaurant_location"> <?php echo $restaurant_location ?> </p>
<p class="restaurant_tags">Restaurant Offers: <?php echo $restaurant_tags ?> </p>
</div>
<br />
<?php
}
}
}
}
else {
// No restaurants found matching the hint
echo "No restaurants found matching the hint '$hint'";
}
?>
All of my data is stored in an SQL table and this code is trying to pull from it. I already initialized the SQL data beforehand I just didn’t display that part of my code.
I tried the code above and wanted it to display multiple restaurants but when I checked it only showed the restaurant of the topmost setting so in the ch example it only showed restaurants with cheeseburgers. Picture of my website on front end, it breaks up the array of tags_split but it doesn’t iterate over it
I troubleshooted the code and found out the problem was in the second else if block where it breaks up $hint and it places it an array but it doesn’t iterate over every item in the array, only the first one.
user24932335 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.