Add new layer

How do I add another layer of nesting to a MySQL DB?

For example, right now we have
    > Android
    > iPhone
but need
    > Android
       > Crackberry
    > iPhone
      > 5c/5s

How do I do that?
Bob StoneIT GuruAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Your DB setup would help but something like this

ID	Name		Parent
1	Android		0
2	Samsung		1
3	Parts		2
4	Crackberry	1

Open in new window

Bob StoneIT GuruAuthor Commented:
What's the SQL syntax and php code to query that and make it line up properly?
This is what I use on one site

SELECT id, name, parent FROM table ORDER BY parent, name

Open in new window

This is the code that sets up the arrays
$refs = array();
$list = array();

Open in new window

This is the code that loops the recordset
while($data = $stmt->fetch(PDO::FETCH_ASSOC)) {
	$this_ref = &$refs[ $data['id'] ];
	$this_ref['parent'] = $data['parent'];
	$this_ref['name'] = $data['name'];
	if ($data['parent'] == 0) {
		$list[ $data['id'] ] = &$this_ref;
	} else {
		$refs[ $data['parent'] ]['children'][ $data['id'] ] = &$this_ref;

Open in new window

This is the function that builds up a UL of the categories with nested children
function toUL($arr,$level){
	global $links;
	if($level!=0){$html .= '<ul>';}
	foreach ($arr as $v){
		$html .= '<li>
		<a href="'.$links.$v['url'].'">' . $v['name'].'</a>';
		if (array_key_exists('children', $v)){
			$html .= toUL($v['children'],1);
		$html .= '</li>';
	if($level!=0){$html .= '</ul>';}
	return $html;

Open in new window

This is kinda plug 'n' play code, just needs to be adapted to your table column names. And of course you will add to add your styling

I made an edit to my sample table set up above - this code relies on the top category parent value being 0 (zero)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
MySQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.