I'm trying to make the jump from procedural code to the PDO class, and have run into problems when querying the DB based on the results of an initial query.
In this instance, I have a menu (id = $parent) of submenus, the details of which I query successfully and are stored in an array for me to access. I then try to form a foreach loop to cycle through that initial array and query the DB again to find the pages belonging to each submenu. It is here the process fails me.
Just as a note, the connection is fine, and the queries also work without a hitch procedurally. I'm merely failing (I think) with the new format.
If someone could point out where I'm going wrong, I'd be grateful. I had been running queries run inside while loops, but I figured foreach was the way to go this time.
The Code
function navigation($parent) {
$conn = ConnManager::get('DB');
//initial query to get list of submenus belonging to $parent
try {
$qNAV= $conn->prepare('SELECT ID, Name FROM prm_menu WHERE Parent = :parent');
$qNAV->execute(array('parent' => $parent));
$qNAV->setFetchMode(PDO::FETCH_ASSOC);
$n=$qNAV->fetchAll();
}
catch(PDOException $e) {
echo $e->getMessage();
}
//now I try to cycle through the array and run another query each time.
foreach($n as $menu) {
$id = $menu['ID'];
try{
$qPAGE=$conn->prepare('SELECT ID, Title, URLName, MenuOrder FROM posts
WHERE MenuID = $id AND Status = 1 ORDER BY MenuOrder ASC, ID ASC');
$qPAGE->execute();
$qPAGE->setFetchMode(PDO::FETCH_ASSOC);
while($m= $qPAGE->fetchAll()) {
$menu_item = '<li id="navPage'.$m['ID'].'" class="menu-nav">';
$menu_item.= '<a href="'.$m['URLName'].'">'.$m['Title'].'</a>';
$menu_item.= '</li>';
echo '<div class="menu-nav-wrap">';
echo '<span class="menu-title">'.$menu['Name'].'</span>';
echo '<ul class="menu-list">';
echo $menu_item;
echo '</ul>';
echo '</div>';
}
}
catch(PDOException $e) {
echo $e -> getMessage();
}
}
}