Parent-child relation in php mysql

I’m trying create simple cms with help of core php and mysql.

  1. I’m trying created post having parent and child relation like in WordPress where user can make any post parent and any post child, how can i give that option to the user where he can make the post parent or child of his choice.

  2. how can i display parent post as main menu and child as sub-menu under parent menu.

Hi,

One way to do do this is to have a mapping table in your database where you have parent_id and child_id.

In the UI if a post is to be a child of another you write the parent_id and the post id for the child post to the child_id. Then you can query all the post that have children and get all children that belong to a post.

1 Like

This should help.
http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/

1 Like

but i want user to be able to select and make parent post dynamically like in wordpress, they can select from dashboard manually

its helpful but its seems to be static but there should option where user should be able to make post parent and child like in wordpress its should be dynamic

The suggestions given describe how to structure and store the data in SQL, which is an important part of what you are trying to do.
Though I guess your question is about the UI side of things. Well that would be more of an HTML thing, as it would involve some kind of HTML form, with inputs for selecting parent/child relationships for posts.
Once you figure out what the UI form will be like, you can then look at the PHP to parse the form submission, then store the data.

1 Like

For Ui you could put on the admin side for posts you could have an icon and name saying “Add Child Page”.

If a user creates a page that is a normal page template. Under the hood you save the new child ID linked to the parent post’s ID. The child post can be saved in the posts table.

If you want, you can have a posts page where you can edit or delete posts. You could have a trigger to delete children on child posts and if children pages has children then the trigger could delete those as well.

1 Like