Gerard Samuel wrote:
I could have sworn that references and I used to get along...
In the menu::hasChild method, I cant seem to reference $this->_data.
$this->_data is supposed to be a reference to the original array $array
in the local scope.
The only way to make this script work is to call on the $GLOBALS array
in menu::hasChild.
Could anyone explain to me, why I'm unable to use $this->_data
in menu::hasChild?
Nevermind. I figured what was causing the problem...
New Code:
-- Code Snip --
<?php
header('content-type: text/plain');
$array = array (
'0000000000000000' =>
array (
'id' => '0000000000000000',
'pid' => '0',
'name' => 'Home',
'url' => 'index.php',
),
'AB4wFQI2QUewz3P7' =>
array (
'id' => 'AB4wFQI2QUewz3P7',
'pid' => '0000000000000000',
'name' => 'User',
'url' => 'modules/user/index.php',
),
'AB4wFQI2QU2iY4SP' =>
array (
'id' => 'AB4wFQI2QU2iY4SP',
'pid' => 'AB4wFQI2QUewz3P7',
'name' => 'Admin',
'url' => 'modules/user/admin/index.php',
),
'AB4wFQI2QU2ihsz2' =>
array (
'id' => 'AB4wFQI2QU2ihsz2',
'pid' => 'AB4wFQI2QU2iY4SP',
'name' => 'delete',
'url' => 'modules/user/admin/list_to_delete.php',
),
'AB4wFQI2QUp0uEk7' =>
array (
'id' => 'AB4wFQI2QUp0uEk7',
'pid' => 'AB4wFQI2QUewz3P7',
'name' => 'Edit Account',
'url' => 'modules/user/edit_account.php',
),
'AB4wFQI2QUp2huwX' =>
array (
'id' => 'AB4wFQI2QUp2huwX',
'pid' => 'AB4wFQI2QUewz3P7',
'name' => 'Login',
'url' => 'modules/user/login.php',
),
'AB4wFQI2QUpzujZ8' =>
array (
'id' => 'AB4wFQI2QUpzujZ8',
'pid' => 'AB4wFQI2QUewz3P7',
'name' => 'Register',
'url' => 'modules/user/register.php',
),
);
$menu = new menu;
$menu->setData( $array );
$data = $menu->constructMenuData( $array );
var_dump($data);
class menu
{
var $_data = array();
function setData(&$data)
{
$this->_data =& $data;
}
function constructMenuData(&$raw_data)
{
$data = array();
foreach($raw_data as $key => $value)
{
$data[ $key ]['name'] = $value['name'];
$data[ $key ]['url'] = $value['url'];
$child = $this->hasChild( $value['id'] );
if (!empty($child))
{
$data[ $key ]['sub'] = $this->constructMenuData( $child );
}
}
return $data;
}
function hasChild($id)
{
$data = array();
foreach($this->_data as $key => $value)
{
if ($value['pid'] === $id)
{
$data[ $key ] = $this->_data[ $key ];
}
}
return $data;
}
}
?>
-- End Code Snip --
-- Code Snip --
<?php
header('content-type: text/plain');
$array = array (
'0000000000000000' =>
array (
'id' => '0000000000000000',
'pid' => '0',
'name' => 'Home',
'url' => 'index.php',
),
'AB4wFQI2QUewz3P7' =>
array (
'id' => 'AB4wFQI2QUewz3P7',
'pid' => '0000000000000000',
'name' => 'User',
'url' => 'modules/user/index.php',
),
'AB4wFQI2QU2iY4SP' =>
array (
'id' => 'AB4wFQI2QU2iY4SP',
'pid' => 'AB4wFQI2QUewz3P7',
'name' => 'Admin',
'url' => 'modules/user/admin/index.php',
),
'AB4wFQI2QU2ihsz2' =>
array (
'id' => 'AB4wFQI2QU2ihsz2',
'pid' => 'AB4wFQI2QU2iY4SP',
'name' => 'delete',
'url' => 'modules/user/admin/list_to_delete.php',
),
'AB4wFQI2QUp0uEk7' =>
array (
'id' => 'AB4wFQI2QUp0uEk7',
'pid' => 'AB4wFQI2QUewz3P7',
'name' => 'Edit Account',
'url' => 'modules/user/edit_account.php',
),
'AB4wFQI2QUp2huwX' =>
array (
'id' => 'AB4wFQI2QUp2huwX',
'pid' => 'AB4wFQI2QUewz3P7',
'name' => 'Login',
'url' => 'modules/user/login.php',
),
'AB4wFQI2QUpzujZ8' =>
array (
'id' => 'AB4wFQI2QUpzujZ8',
'pid' => 'AB4wFQI2QUewz3P7',
'name' => 'Register',
'url' => 'modules/user/register.php',
),
);
$menu = new menu;
$data = $menu->constructMenuData( $array );
var_dump($data);
class menu
{
var $_data = array();
function constructMenuData(&$raw_data)
{
$data = array();
$this->_data =& $raw_data;
foreach($this->_data as $key => $value)
{
$data[ $key ]['name'] = $value['name'];
$data[ $key ]['url'] = $value['url'];
$child = $this->hasChild( $value['id'] );
if (!empty($child))
{
$data[ $key ]['sub'] = $this->constructMenuData( $child );
}
}
return $data;
}
function hasChild($id)
{
$data = array();
// foreach($this->_data as $key => $value)
foreach($GLOBALS['array'] as $key => $value)
{
if ($value['pid'] === $id)
{
// $data[ $key ] = $this->_data[ $key ];
$data[ $key ] = $GLOBALS['array'][ $key ];
}
}
return $data;
}
}
?>
-- End Code snip --
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php