jenkins Posted June 12, 2013 Share Posted June 12, 2013 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); } Quote Link to comment Share on other sites More sharing options...
Q695 Posted June 12, 2013 Share Posted June 12, 2013 nesting via table/layer abbreviations (table 2 variable= table 1 variable if different, or where table byte operator = byte) to say how deep the option is, or doing a php join if the data is different. Quote Link to comment Share on other sites More sharing options...
jenkins Posted June 13, 2013 Author Share Posted June 13, 2013 Actually, when a checkbox is checked the item (and if necessary its parent) are copied from the nodes table to the roleMenu table - that is fine. It just doesn't echo permissionGranted the first time through if the parent has not been previously added. Somewhere when I am checking if the parent_id has not been added to the roleMenu and the function is called again to add it there is an error. When an item has been added, it is displayed like this: Thanks again for any help! Quote Link to comment Share on other sites More sharing options...
Q695 Posted June 13, 2013 Share Posted June 13, 2013 That's just a joining system with a while inside a while with different value systems based upon where you want it to show up. One of the only exceptions you would use a nested while loop in. At the final level you do a check to see if the item would be checked, or not. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.