Jump to content

MYSQL Recursive Data


rem

Recommended Posts

Hi all,

 

There's been a while since me and PHP got acquainted but still there's only a few month though since I started digging into arrays and theirs functionality. Since I'm here, of course there's a problem and I would like to ask you, more experienced guys, for help.

 

I need to develop my own navigation system with unlimited levels of hierarchical links and I need to get them as a multidimensional array:

 

<?php
$menu = array (
            'menu 1' => 'path1.html',  // main link where parent = 0 and ID 1
            'menu 2' => 'path2.html', // main link where parent = 0 and ID 2
            'menu 3' => array('submenu1' => 'subpath1.html', 'submenu1' => 'subpath1.html') // main link with ID 3, sub-links with parent = 3;
          // and so on .....
);
?>

 

Just in a few easy steps: how could I insert this data into MYSQL and then fetch it into an above like array?

Further, I have a function which is recursively digging into the array and is displaying me the links as a tree. Which is exactly what I need to do, but I'm stuck when is about to handle the MYSQL part ... :(

 

Thank you very much for all the help provided and for reading this!

 

Regards,

 

Rem.

 

Link to comment
https://forums.phpfreaks.com/topic/36616-mysql-recursive-data/
Share on other sites

you can use serialize and unserialize for this:

 

<?php
$menuToDB = serialize($menu);

later after retrieved from database:
$menuFromDB = unserialize(data from database);

now, your $menuFromDB is exactly as it was.
?>

 

Thanks a lot for your answer but that's not what I meant. It's my fault, I had to explain it better:

Let's consider the following MYSQL records:

 

ID    title        path        parent

===========================

1    menu1    path1.html    0

2    menu2    path2.html    0

3    menu3    path3.html    0

4  submenu1  path4.html    3

5  submenu2  path5.html    3

 

and so on...

 

I need those records fetched from MYSQL and hierarchically inserted into a multidimensional array to be like the one in the example above. That's the transformation I need! MySQL data -> Array() ...

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/36616-mysql-recursive-data/#findComment-174522
Share on other sites

You can try this code


$rs = myslq_query("select .....");

$menu = array();
while($row = mysql_fetch_assoc($rs))
{
    if ($row['parent'] == 0) // if parent is 0
    {
        $node = $row['id'];
        $menu[$node] = $row['path'];
    }
    else if ($row['parent'] == 0) // if parent > 0
    {
        $node = $row['parent'];
        if (!is_array($menu[$node])) // this node have childrent, so change it to array
            $menu[$node] = array();
        $menu[$node][] = $row['path'];
    }
}

 

The only thing can go wrong is if a title "menu3" has a path of its own, and have children, its own path will be replaced with array of chilrent (sub menu), because 1 variable can not be a string AND an array.

Link to comment
https://forums.phpfreaks.com/topic/36616-mysql-recursive-data/#findComment-174534
Share on other sites

The only thing can go wrong is if a title "menu3" has a path of its own, and have children, its own path will be replaced with array of chilrent (sub menu), because 1 variable can not be a string AND an array.

 

That's perfectly fine! This is how i need it to do. This code is great, thanks a mil, except instead of links I have numbers!

The array needs to be

 

Array
(
    [menu1] => title.html
    [menu2] => Array
        (
            [submenu3] => subtitle1.html
        )

)

 

instead of

 

Array
(
    [1] => title.html
    [2] => Array
        (
            [0] => subtitle1.html
        )

)

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/36616-mysql-recursive-data/#findComment-174547
Share on other sites

And another thing is that if a lower level of links is added, a sub-sub link, the script is displaying another main link with a child, instead of hierarchically display it along the tree...

 

ex:

 

    
[1] => title.html
[2] => Array
        (
            [0] => subtitle1.html
        )

[3] => Array
        (
            [0] => subsubtitle1.html // this should be a child of subtitle1.html
        )

)

Link to comment
https://forums.phpfreaks.com/topic/36616-mysql-recursive-data/#findComment-174551
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.