I’m using php to loop over an array that calls for SQL information. It looks like this. The code shows the result for the first item in the array but it returns blanks for the rest.
$sql = "SELECT name, rating,location,tags, MapUrl FROM restdata WHERE tags LIKE CONCAT('%', ?, '%') ORDER BY rating DESC";
$stmt = $mysqli->prepare($sql);
foreach ($hint_split as $tag) {
// SQL query to select rows where the tags column contains the specified text
$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_map_url = $row['MapUrl'];
$restaurant_div = $restaurant_rating * 20;
?>
<!-- Displays restaurant info -->
<div class="rest-stuff">
<h2 class="restaurant_name" onclick="MapChange(this)"> <?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>
<p class="restaurant_url"><?php echo $restaurant_map_url ?></p>
</div>
<br />
<?php
}
}
else {
echo "Something is wrong";
}
}
$stmt->close();```
I narrowed down my problem to the SQL information. I think there is something wrong with it that causes the code to return a null for every other item in the array. However, I don't know what this is. I have checked Stack Overflow for answers but none of them seemed to solve my problem.