accessing array inside object inside array
- Cae
- Expert


- Joined: Feb 25, 2004
- Posts: 734
- Status: Offline
well, assuming that what i said above is true... this is what i have so far...
however, it is outputing a 0...
PHP Code: [ Select ]
<?php
include('sqlQuery.php');
$level = '0';
$menuStr = NULL;
$nodes = NULL;
$index = 0;
$menu = generateMenu('0.2');
echo($menu);
function generateMenu($address){
global $nodes;
$nodes = explode('.', $address);
$menu = menuCrawler();
return $menu;
}
function menuCrawler(){
global $level, $menuStr, $nodes, $index;
$level += '.' . $nodes[$index];
$result = queryDB("SELECT address FROM menus WHERE address = '" . $level . "'", true);
for($i = 0; $i < count($result) - 1; $i++){
$result = queryDB("SELECT ID, link FROM menus WHERE address = '" . $level . "'", true);
foreach($result as $key => $item){
$menuStr += "<li>" . $item[1];
if($item[0] == $nodes[$index + 1]){
$index += 1;
$menuStr += "<ul>" . menuCrawler($level) . "</ul>";
}
}
$menuStr += "</li>";
}
return $menuStr;
}
?>
include('sqlQuery.php');
$level = '0';
$menuStr = NULL;
$nodes = NULL;
$index = 0;
$menu = generateMenu('0.2');
echo($menu);
function generateMenu($address){
global $nodes;
$nodes = explode('.', $address);
$menu = menuCrawler();
return $menu;
}
function menuCrawler(){
global $level, $menuStr, $nodes, $index;
$level += '.' . $nodes[$index];
$result = queryDB("SELECT address FROM menus WHERE address = '" . $level . "'", true);
for($i = 0; $i < count($result) - 1; $i++){
$result = queryDB("SELECT ID, link FROM menus WHERE address = '" . $level . "'", true);
foreach($result as $key => $item){
$menuStr += "<li>" . $item[1];
if($item[0] == $nodes[$index + 1]){
$index += 1;
$menuStr += "<ul>" . menuCrawler($level) . "</ul>";
}
}
$menuStr += "</li>";
}
return $menuStr;
}
?>
- <?php
- include('sqlQuery.php');
- $level = '0';
- $menuStr = NULL;
- $nodes = NULL;
- $index = 0;
- $menu = generateMenu('0.2');
- echo($menu);
- function generateMenu($address){
- global $nodes;
- $nodes = explode('.', $address);
- $menu = menuCrawler();
- return $menu;
- }
- function menuCrawler(){
- global $level, $menuStr, $nodes, $index;
- $level += '.' . $nodes[$index];
- $result = queryDB("SELECT address FROM menus WHERE address = '" . $level . "'", true);
- for($i = 0; $i < count($result) - 1; $i++){
- $result = queryDB("SELECT ID, link FROM menus WHERE address = '" . $level . "'", true);
- foreach($result as $key => $item){
- $menuStr += "<li>" . $item[1];
- if($item[0] == $nodes[$index + 1]){
- $index += 1;
- $menuStr += "<ul>" . menuCrawler($level) . "</ul>";
- }
- }
- $menuStr += "</li>";
- }
- return $menuStr;
- }
- ?>
however, it is outputing a 0...
- Anonymous
- Bot


- Joined: 25 Feb 2008
- Posts: ?
- Loc: Ozzuland
- Status: Online
June 27th, 2004, 5:24 pm
- rtm223
- Mastermind


- Joined: Mar 24, 2004
- Posts: 1855
- Loc: Uk
- Status: Offline
Calendae wrote:
Quote:
parentID - ID - link
0 - 0 - <a href = "/" class = "nav level1"...
0 - 1 - <a href ="/main.php?page=e2004" class ="nav level1...
0 - 2 - <a href ="/main.php?page=wow" class ="nav level1"...
0 - 3 - <a href ="/main.php?page=stargate" class ="nav lev...
2 - 0 - <a href =" /main.php?page=wow&sub=races" class =" ...
2 - 1 - <a href =" /main.php?page=wow&sub=classes" class ="...
0 - 0 - <a href = "/" class = "nav level1"...
0 - 1 - <a href ="/main.php?page=e2004" class ="nav level1...
0 - 2 - <a href ="/main.php?page=wow" class ="nav level1"...
0 - 3 - <a href ="/main.php?page=stargate" class ="nav lev...
2 - 0 - <a href =" /main.php?page=wow&sub=races" class =" ...
2 - 1 - <a href =" /main.php?page=wow&sub=classes" class ="...
Id=0 parentid=0. That will cause you problems, you will continually loop through that one node, placing it inside itself.
Quote:
and if i want to specify which menu i want wouldnt i have to specify something like '0.2.1.3' and then have it split it, and the determine the node by which level its at???
ie:
level0: node = 0
level1: node = 2 (for parentID = 0)
level2: node = 1 (for parentID = 2, super-parentID = 0)
level3: node = 3 (for parentID = 1, super-parentID = 2, super-super-parentID = 0)
ie:
level0: node = 0
level1: node = 2 (for parentID = 0)
level2: node = 1 (for parentID = 2, super-parentID = 0)
level3: node = 3 (for parentID = 1, super-parentID = 2, super-super-parentID = 0)
I'm not entirely sure why you would need to know what level you are at at any one point? The idea of a tree i that you don't need the entire address. If you have the full address then you will start to get inconsistancies. #
Hang on I see what you have done, the Id's should be unique. For example
Quote:
parentID - ID - link
0 - 0 - <a href = "/" class = "nav level1"...
0 - 1 - <a href ="/main.php?page=e2004" class ="nav level1...
0 - 2 - <a href ="/main.php?page=wow" class ="nav level1"...
0 - 3 - <a href ="/main.php?page=stargate" class ="nav lev...
2 - 4 - <a href =" /main.php?page=wow&sub=races" class =" ...
2 - 5 - <a href =" /main.php?page=wow&sub=classes" class ="...
0 - 0 - <a href = "/" class = "nav level1"...
0 - 1 - <a href ="/main.php?page=e2004" class ="nav level1...
0 - 2 - <a href ="/main.php?page=wow" class ="nav level1"...
0 - 3 - <a href ="/main.php?page=stargate" class ="nav lev...
2 - 4 - <a href =" /main.php?page=wow&sub=races" class =" ...
2 - 5 - <a href =" /main.php?page=wow&sub=classes" class ="...
Because each node is an individual item. If you have lots of nodes with id=1, and a node with parentID=1, how can you know <B>which</B> node with ID 1 is the parent. If you have unique ID's you can get the full address by traversing the tree in to other way. Also check out the original example I showed you. The arrayindex is the same as the database ID. See how it is used in both cases.
having looked through your code and your menu, I can see a few other problems.
I now see that you need to know how many levels down you are to know which sections to have open. This makes things complicated but not as complicated as you have made them.
You are using both returns and global variables to build the $menuStr . Either method is viable, but you cannot use both at the same time. I chose the returns because it felt "neater" to me.
At the moment I think you are going to have to start with the node that you are at. Then work back up the tree to the very root node to get the address (as an array of Id's, call it $openId[] for eg). Then you modify the menu crawler to open the base menu, and any subsection node that is specified in the openId[] array.
I'll mock up an example, but I have no way of testing it because I am at work at the moment. Give me a bit and I'll edit it onto this post
PHP Code: [ Select ]
//Assume we know the current Node Id and it is held in the variable $thisPageId
$openId = array();
$openId[-1] = "";
getAddress($ThisPageId);
/*
This will giv us an array like:
-1 -> ""
2 -> ""
7 -> ""
which is the node Id=7 which has parentId=2, and 2 is a top level.
I've done it this way round, using keys instead of values so
that you can use isset().
*/
menu=makeMenu(-1);
function getAddress($currentNode){
global $openId;
$openId[$currentNode]=""; //Done this was so we can use isset later on
//Get parent node for this node from the database
if ($parentNode!=-1){
getAddress($parentNode);
}
}
function getMenu($parentId){
//Get all nodes with parentId=$parentId
global $openId;
$menuStr='';
foreach(/*results*/){
$menuStr+="<li>"./*link from DB*/;
if(isset($openId[/*currentIdfromDB*/])){
$menuStr+="<ul>";
$menuStr+=getMenu(/*currentIdfromDB*/);
$menuStr+="</ul>";
}
menuStr+="</li>"
}
}
$openId = array();
$openId[-1] = "";
getAddress($ThisPageId);
/*
This will giv us an array like:
-1 -> ""
2 -> ""
7 -> ""
which is the node Id=7 which has parentId=2, and 2 is a top level.
I've done it this way round, using keys instead of values so
that you can use isset().
*/
menu=makeMenu(-1);
function getAddress($currentNode){
global $openId;
$openId[$currentNode]=""; //Done this was so we can use isset later on
//Get parent node for this node from the database
if ($parentNode!=-1){
getAddress($parentNode);
}
}
function getMenu($parentId){
//Get all nodes with parentId=$parentId
global $openId;
$menuStr='';
foreach(/*results*/){
$menuStr+="<li>"./*link from DB*/;
if(isset($openId[/*currentIdfromDB*/])){
$menuStr+="<ul>";
$menuStr+=getMenu(/*currentIdfromDB*/);
$menuStr+="</ul>";
}
menuStr+="</li>"
}
}
- //Assume we know the current Node Id and it is held in the variable $thisPageId
- $openId = array();
- $openId[-1] = "";
- getAddress($ThisPageId);
- /*
- This will giv us an array like:
- -1 -> ""
- 2 -> ""
- 7 -> ""
- which is the node Id=7 which has parentId=2, and 2 is a top level.
- I've done it this way round, using keys instead of values so
- that you can use isset().
- */
- menu=makeMenu(-1);
- function getAddress($currentNode){
- global $openId;
- $openId[$currentNode]=""; //Done this was so we can use isset later on
- //Get parent node for this node from the database
- if ($parentNode!=-1){
- getAddress($parentNode);
- }
- }
- function getMenu($parentId){
- //Get all nodes with parentId=$parentId
- global $openId;
- $menuStr='';
- foreach(/*results*/){
- $menuStr+="<li>"./*link from DB*/;
- if(isset($openId[/*currentIdfromDB*/])){
- $menuStr+="<ul>";
- $menuStr+=getMenu(/*currentIdfromDB*/);
- $menuStr+="</ul>";
- }
- menuStr+="</li>"
- }
- }
As you can see we create an array of the nodes we wish to open and then make the menu, opening nodes that are specified in the array.
This is not necessarilly accurate, I've just knocked it together again, and I have left all the database bits out. Note that any top-level node needs to have parentId = -1 for this to work. Then each node has a <b>completely unique</b> id, starting at 0.
CSS website design tutorials
- Cae
- Expert


- Joined: Feb 25, 2004
- Posts: 734
- Status: Offline
- rtm223
- Mastermind


- Joined: Mar 24, 2004
- Posts: 1855
- Loc: Uk
- Status: Offline
not that hard to maintain. When you add a new page, you find the parent and you add the correct parent Id to it, which is basically how a normal filesystem works. Just think of the parents as directories.
Anyway the way your method falls down is if you want to alter your categorisation at a later date. think about moving a node in your system, you would have to manually edit the address of EVERY descendant node. My method, all of the descendants move automagically.
However, it is perfectly feasable to set up your method. First step would be to decide whether the string would be a global or a returned value. The best thing I can suggest is to do run through it manually, writing down exactly what is going on. Recursion can be a little bit difficult to understand, so it might be worth looking up some tutorials on it.
Anyway the way your method falls down is if you want to alter your categorisation at a later date. think about moving a node in your system, you would have to manually edit the address of EVERY descendant node. My method, all of the descendants move automagically.
However, it is perfectly feasable to set up your method. First step would be to decide whether the string would be a global or a returned value. The best thing I can suggest is to do run through it manually, writing down exactly what is going on. Recursion can be a little bit difficult to understand, so it might be worth looking up some tutorials on it.
CSS website design tutorials
- Cae
- Expert


- Joined: Feb 25, 2004
- Posts: 734
- Status: Offline
ok, i guess i didnt mean maintain... your way would be easier to maintain... but i think my way would be easier to use because you wouldnt have to remeber which ID goes with which menu... using my way, you just have to work your way down the chain... *shrug* that is what is easier to me anyway...
back to the code now...
using returns would make everything easier... so thats what im going to do... however, it is saying that there is an 'Invalid Argument' in my foreach loop, and im not sure why...
back to the code now...
PHP Code: [ Select ]
<?php
include('sqlQuery.php');
$nodes = NULL;
$level = '';
$index = NULL;
$menu = generateMenu('0.3');
echo($menu);
function generateMenu($address){
global $nodes;
$nodes = explode('.', $address);
$menu = menuCrawler();
return $menu;
}
function menuCrawler(){
global $level, $nodes, $index;
$level += '.' . $nodes[$index];
$current = $level;
$result = queryDB("SELECT address FROM menus WHERE address = '" . $current . "'", true);
for($i = 0; $i <= count($result) - 1; $i++){
$result = queryDB("SELECT ID, link FROM menus WHERE address = '" . $current . "'", true);
foreach($result as $key => $item){
$menuStr += "<li>" . $item[1];
if($item[0] == $nodes[$index + 1]){
$index++;
$menuStr += "<ul>" . menuCrawler($current) . "</ul>";
}
}
$menuStr += "</li>";
}
return $menuStr;
}
?>
include('sqlQuery.php');
$nodes = NULL;
$level = '';
$index = NULL;
$menu = generateMenu('0.3');
echo($menu);
function generateMenu($address){
global $nodes;
$nodes = explode('.', $address);
$menu = menuCrawler();
return $menu;
}
function menuCrawler(){
global $level, $nodes, $index;
$level += '.' . $nodes[$index];
$current = $level;
$result = queryDB("SELECT address FROM menus WHERE address = '" . $current . "'", true);
for($i = 0; $i <= count($result) - 1; $i++){
$result = queryDB("SELECT ID, link FROM menus WHERE address = '" . $current . "'", true);
foreach($result as $key => $item){
$menuStr += "<li>" . $item[1];
if($item[0] == $nodes[$index + 1]){
$index++;
$menuStr += "<ul>" . menuCrawler($current) . "</ul>";
}
}
$menuStr += "</li>";
}
return $menuStr;
}
?>
- <?php
- include('sqlQuery.php');
- $nodes = NULL;
- $level = '';
- $index = NULL;
- $menu = generateMenu('0.3');
- echo($menu);
- function generateMenu($address){
- global $nodes;
- $nodes = explode('.', $address);
- $menu = menuCrawler();
- return $menu;
- }
- function menuCrawler(){
- global $level, $nodes, $index;
- $level += '.' . $nodes[$index];
- $current = $level;
- $result = queryDB("SELECT address FROM menus WHERE address = '" . $current . "'", true);
- for($i = 0; $i <= count($result) - 1; $i++){
- $result = queryDB("SELECT ID, link FROM menus WHERE address = '" . $current . "'", true);
- foreach($result as $key => $item){
- $menuStr += "<li>" . $item[1];
- if($item[0] == $nodes[$index + 1]){
- $index++;
- $menuStr += "<ul>" . menuCrawler($current) . "</ul>";
- }
- }
- $menuStr += "</li>";
- }
- return $menuStr;
- }
- ?>
- Cae
- Expert


- Joined: Feb 25, 2004
- Posts: 734
- Status: Offline
well, i did it, it works! w00t!
i just though id share the final scripts incase anyone wants to use it... subsequently, i realized after the fact, that i made this using strings, not integers, or doubles...
so you dont have to have '00.03.00' menu codes and so forth... you can also use menu codes such as 'root.WoW.races' which is cool...
heres the final script:
and heres an example of the database (dashes denote next colom):
enjoy!
//yay! and thanks rtm223
i just though id share the final scripts incase anyone wants to use it... subsequently, i realized after the fact, that i made this using strings, not integers, or doubles...
heres the final script:
PHP Code: [ Select ]
function generateMenu($address){
global $nodes, $index, $level;
$index = 0;
$level = '';
$nodes = explode('.', $address);
$menu = menuCrawler();
return $menu;
}
function menuCrawler(){
global $level, $nodes, $index;
$menuStr = '';
if($index == 0){
$level = $nodes[0];
}
else{
$level = $level . '.' . $nodes[$index];
}
$current = $level;
$indent = $index;
$result = queryDB("SELECT ID, link FROM menus WHERE address = '" . $current . "' ORDER BY ID", 'menu');
for($i = 0; $i <= count($result) - 1; $i++){
$menuStr = $menuStr . str_replace("%level%", ('level' . $indent), $result[$i][1]) . chr(13) . chr(10);
if($result[$i][0] == $nodes[$index + 1]){
$index++;
$menuStr = $menuStr . menuCrawler($current);
}
};
return $menuStr;
}
global $nodes, $index, $level;
$index = 0;
$level = '';
$nodes = explode('.', $address);
$menu = menuCrawler();
return $menu;
}
function menuCrawler(){
global $level, $nodes, $index;
$menuStr = '';
if($index == 0){
$level = $nodes[0];
}
else{
$level = $level . '.' . $nodes[$index];
}
$current = $level;
$indent = $index;
$result = queryDB("SELECT ID, link FROM menus WHERE address = '" . $current . "' ORDER BY ID", 'menu');
for($i = 0; $i <= count($result) - 1; $i++){
$menuStr = $menuStr . str_replace("%level%", ('level' . $indent), $result[$i][1]) . chr(13) . chr(10);
if($result[$i][0] == $nodes[$index + 1]){
$index++;
$menuStr = $menuStr . menuCrawler($current);
}
};
return $menuStr;
}
- function generateMenu($address){
- global $nodes, $index, $level;
- $index = 0;
- $level = '';
- $nodes = explode('.', $address);
- $menu = menuCrawler();
- return $menu;
- }
- function menuCrawler(){
- global $level, $nodes, $index;
- $menuStr = '';
- if($index == 0){
- $level = $nodes[0];
- }
- else{
- $level = $level . '.' . $nodes[$index];
- }
- $current = $level;
- $indent = $index;
- $result = queryDB("SELECT ID, link FROM menus WHERE address = '" . $current . "' ORDER BY ID", 'menu');
- for($i = 0; $i <= count($result) - 1; $i++){
- $menuStr = $menuStr . str_replace("%level%", ('level' . $indent), $result[$i][1]) . chr(13) . chr(10);
- if($result[$i][0] == $nodes[$index + 1]){
- $index++;
- $menuStr = $menuStr . menuCrawler($current);
- }
- };
- return $menuStr;
- }
and heres an example of the database (dashes denote next colom):
Quote:
address - ID - link
00 - 03 - <a href ="/main.php?page=wow" class ="nav %level%"...
00.03 - 04 - <a href = "/main.php?page=wow&sub=basics" class = ...
00.03.00 - 00 - <a href = "/main.php?page=wow&sub=races&side=allia...
00.03.00 - 01 - <a href = "/main.php?page=wow&sub=races&side=horde...
00.03.00.00 - 00 - <a href = "main.php?page=wow&sub=races&side=allian...
00.03.00.00 - 01 - <a href = "main.php?page=wow&sub=races&side=allian...
00 - 03 - <a href ="/main.php?page=wow" class ="nav %level%"...
00.03 - 04 - <a href = "/main.php?page=wow&sub=basics" class = ...
00.03.00 - 00 - <a href = "/main.php?page=wow&sub=races&side=allia...
00.03.00 - 01 - <a href = "/main.php?page=wow&sub=races&side=horde...
00.03.00.00 - 00 - <a href = "main.php?page=wow&sub=races&side=allian...
00.03.00.00 - 01 - <a href = "main.php?page=wow&sub=races&side=allian...
enjoy!
//yay! and thanks rtm223
1, 2
To Reply to this topic you need to LOGIN or REGISTER. It is free.
Post Information
- Total Posts in this topic: 21 posts
- Users browsing this forum: No registered users and 125 guests
- You cannot post new topics in this forum
- You cannot reply to topics in this forum
- You cannot edit your posts in this forum
- You cannot delete your posts in this forum
- You cannot post attachments in this forum
