Jump to content

Add parent/child menu item to DB table


jenkins

Recommended Posts

Hi all,

 

I have the following code to add the id and parent_id of a menu item from one table (nodes post-138495-0-17254000-1371032433_thumb.png) to another table (roleMenu post-138495-0-78272200-1371032434_thumb.png) when a checkbox is checked in the menu options post-138495-0-10548100-1371032436_thumb.png.

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);
}
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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: 

post-138495-0-08230900-1371154606_thumb.png

 

Thanks again for any help! 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.