基于PHP和MySQL实现Bootstrap下拉菜单遇子分类显示问题求助
Hey there! Let's work through this issue together since you're new to raw PHP after coming from Laravel. It makes total sense that you'd want to switch for speed, so let's figure out why your $tag->categories isn't rendering as a dropdown menu.
First: Check if your query is actually fetching category data
Unlike Laravel's Eloquent which handles relationships automatically, raw PHP requires you to explicitly join tables or fetch related data manually. If you're only pulling tags without linking them to categories, $tag->categories (or whatever array key you're using) will be empty.
For example, if you have a many-to-many relationship between tags and categories (with a pivot table like tag_category), here's how you can fetch tags with their associated categories:
// Assuming you have a MySQL connection stored in $conn $fetchQuery = " SELECT t.id AS tag_id, t.name AS tag_name, c.id AS cat_id, c.name AS cat_name FROM tags t LEFT JOIN tag_category tc ON t.id = tc.tag_id LEFT JOIN categories c ON tc.category_id = c.id "; $result = mysqli_query($conn, $fetchQuery); // Organize the results into a structured array (mimicking Laravel's relationship structure) $tags = []; while ($row = mysqli_fetch_assoc($result)) { $tagId = $row['tag_id']; // Initialize the tag if we haven't seen it yet if (!isset($tags[$tagId])) { $tags[$tagId] = [ 'id' => $tagId, 'name' => $row['tag_name'], 'categories' => [] ]; } // Add the category only if it exists (avoids empty entries from LEFT JOIN) if (!empty($row['cat_id'])) { $tags[$tagId]['categories'][] = [ 'id' => $row['cat_id'], 'name' => $row['cat_name'] ]; } } // Convert to an indexed array if you prefer that over associative $tags = array_values($tags);
Second: Fix your dropdown rendering code
Once you have the structured $tags array, make sure your HTML rendering loop correctly accesses the categories. Also, don't forget to sanitize output to prevent XSS attacks (Laravel does this automatically, but raw PHP doesn't):
foreach ($tags as $tag) { echo '<div class="tag-container">'; echo '<h4>' . htmlspecialchars($tag['name']) . '</h4>'; // Render the dropdown echo '<select name="category_for_tag_' . $tag['id'] . '">'; echo '<option value="">Choose a category</option>'; // Check if the tag has categories if (isset($tag['categories']) && !empty($tag['categories'])) { foreach ($tag['categories'] as $category) { echo '<option value="' . htmlspecialchars($category['id']) . '">' . htmlspecialchars($category['name']) . '</option>'; } } else { echo '<option value="" disabled>No categories available</option>'; } echo '</select>'; echo '</div>'; }
Third: Debug your data structure
If you're still stuck, print out the $tags array to see what data you're actually working with. This will tell you if the issue is with your query or your rendering code:
// Print the array in a readable format echo '<pre>' . print_r($tags, true) . '</pre>';
If you see empty categories arrays here, go back to your query and make sure the joins are correct (check table names, foreign keys, etc.). If the categories are present but not rendering, double-check your loop logic.
内容的提问来源于stack exchange,提问作者Young Code

