I have a database which holds two tables.
- table 1: - 'subjects'
- table 2: - 'pages'
I wish to display the navigation so that I get the right pages with the corresponding subjects.
I.e
*subject 1
- page 1
- page 2
*subject 2
- page 1
My pages have the same Id which connects them to their subjects. I've called this 'page_link' or 'subject_link' for example
*subject 1 (has a subject_link of 1)
-page 1 (has a page_link of 1)
So far my code lists subject 1 and all of the sub pages within that. Then it lists the next subject but it doesn't list it's sub pages.
Why is this?
<?php
// writing the query - selecting subject name
$subject_name_query = "SELECT id, subject_name, subject_link FROM subjects";
$subject_name_result = mysqli_query($connection, $subject_name_query);
// writing the query - selecting page
$page_name_query = "SELECT id, page_name, page_link FROM pages";
$page_name_result = mysqli_query($connection, $page_name_query);
?>
<ul>
<?php
while($subject_name = mysqli_fetch_assoc($subject_name_result)) {
echo '<li><a href="?subject=' . $subject_name['id'] . '">' . $subject_name['subject_name'] . '</a>';
while($page_name = mysqli_fetch_assoc($page_name_result)) {
if($page_name['page_link'] == $subject_name['subject_link']) {
echo '<ul><li><a href="?page=' . $page_name['id'] . '">' . $page_name['page_name'] . '</a></li></ul>';
}
}
echo '</li>';
}
?>
</ul>