How do I loop over an array in php that contains data that must be matched back to an SQL table?

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.

New contributor

user24932335 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật