Search the Community
Showing results for tags 'parent/child menu'.
-
Hi all, I have the following code to add the id and parent_id of a menu item from one table (nodes ) to another table (roleMenu ) when a checkbox is checked in the menu options . If successful the response is permissionGranted (sent via ajax). I am using a recursive function to do this. The problem is if a child is added (checkbox is checked) where the parent was not previously added, both the child and parent menu items are added but the response is not echoed. I have attached images of the nodes table, roleMenu table and the menu of checkbox options. Can anyone help with my code below? - or - if you think of a better way to do this I am open to suggestions. I've worked on this for several hours but no luck. Any help would be greatly appreciated. Thanks. if ($action == "grantAccess") { function addMenuItems($userRoleValue, $pageIdValue) { include ('include/dbconnect.php'); try { $pdo = new PDO ("mysql:host=$dbhost;dbname=$dbname","$dbusername","$dbuserpass"); } catch (PDOException $e) { echo "Failed to get DB handle: " . $e->getMessage() . "\n"; exit; } $queryRoleParents = $pdo->prepare("SELECT id, parent_id FROM nodes WHERE id = \"$pageIdValue\""); $queryRoleParents->execute(); while($row = $queryRoleParents->fetch()) { if($row['parent_id'] == 0) { $queryRoleItem = $pdo->prepare("SELECT * FROM roleMenu WHERE menuid = \"$pageIdValue\" AND role = \"$userRoleValue\""); $queryRoleItem->execute(); $countRoleItem = $queryRoleItem->rowCount(); if($countRoleItem == 0) { $grantPageAccess = $pdo->prepare("INSERT INTO roleMenu (rmid, role, menuid, parent_id) VALUES ('', \"$userRoleValue\", \"$pageIdValue\", ".$row['parent_id'].")"); if ($grantPageAccess->execute()) { echo "permissionGranted"; } else { echo "permissionFailed"; } } } else { $queryRoleItem = $pdo->prepare("SELECT * FROM roleMenu WHERE menuid = \"$pageIdValue\" AND role = \"$userRoleValue\""); $queryRoleItem->execute(); $countRoleItem = $queryRoleItem->rowCount(); if($countRoleItem == 0) { $grantPageAccess = $pdo->prepare("INSERT INTO roleMenu (rmid, role, menuid, parent_id) VALUES ('', \"$userRoleValue\", \"$pageIdValue\", ".$row['parent_id'].")"); if ($grantPageAccess->execute()) { echo "permissionGranted"; } else { echo "permissionFailed"; } addMenuItems($userRoleValue, $row['parent_id']); } } } } return addMenuItems($userRoleValue, $pageIdValue); }