[PHP] Arrays help

2007-01-05 Thread William Stokes
Hello,

I'm making a menu script that uses mysql, php and javascript to build a on 
mouse over dropdown menu to a page. I ran into some problems and would need 
help to get this working. (This is just the top level of the menusystem)

1. Get the toplevel links from DB, create array and put values there.

$sql =SELECT * FROM x_menu WHERE menulevel = '1' ORDER BY 'id' ASC;
$result=mysql_query($sql);
$num = mysql_num_rows($result);
$cur = 1;
while ($num = $cur) {
$row = mysql_fetch_array($result);
$id = $row[id];
$menulevel = $row[menulevel];
$linktext = $row[linktext];
$linkurl = $row[linkurl];
$toplevel =array($id, $menulevel, $linktext, $linkurl);
$cur ++;
}

The first problem comes here. How can I create a different array at every 
iteration of the loop? Or how this should be done if the toplevel objects 
are echoed with foreach to the browser like this:

$TopLevelCounter = 1;
foreach ($toplevel as $value){
print menuSyS.addItem('labelItem', '$toplevel[2]', $TopLevelCounter, 
$width, '$colour1', '#aa', '$colour2');\n;
$TopLevelCounter ++;
}

Now, because I just fill the same array again and again in the DB query all 
top level items finally contain the same text. So my question is how to 
query the DB and create the arrays so that it would be easy to refer to the 
data in the arrays when printing to screen. Do I have to make 
multidimensional arrays? If so how to implement them here and how they 
should be referred to when printing to browser?

Thanks
-Will 

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Arrays help

2007-01-05 Thread Robert Cummings
On Fri, 2007-01-05 at 18:48 +0200, William Stokes wrote:
 Hello,
 
 I'm making a menu script that uses mysql, php and javascript to build a on 
 mouse over dropdown menu to a page. I ran into some problems and would need 
 help to get this working. (This is just the top level of the menusystem)
 
 1. Get the toplevel links from DB, create array and put values there.
 
 $sql =SELECT * FROM x_menu WHERE menulevel = '1' ORDER BY 'id' ASC;

Yeesh... should be using parent ID references. How does a sub-menu item
know to which menu item it belongs? It has a parent right? What
determines a root menu entry? No parent (or root node parent)... What
is this wierd menulevel field?

 $result=mysql_query($sql);
 $num = mysql_num_rows($result);
 $cur = 1;
 while ($num = $cur) {
 $row = mysql_fetch_array($result);
 $id = $row[id];
 $menulevel = $row[menulevel];
 $linktext = $row[linktext];
 $linkurl = $row[linkurl];
 $toplevel =array($id, $menulevel, $linktext, $linkurl);
 $cur ++;
 }
 
 The first problem comes here. How can I create a different array at every 
 iteration of the loop? Or how this should be done if the toplevel objects 
 are echoed with foreach to the browser like this:

Just create the array at each iteration of the loop...

$foo = array( /* put some data in it */ )

 
 $TopLevelCounter = 1;
 foreach ($toplevel as $value){
 print menuSyS.addItem('labelItem', '$toplevel[2]', $TopLevelCounter, 
 $width, '$colour1', '#aa', '$colour2');\n;
 $TopLevelCounter ++;
 }
 
 Now, because I just fill the same array again and again in the DB query all 
 top level items finally contain the same text. So my question is how to 
 query the DB and create the arrays so that it would be easy to refer to the 
 data in the arrays when printing to screen. Do I have to make 
 multidimensional arrays?

Yes.

  If so how to implement them here and how they 
 should be referred to when printing to browser?

Use a foreach loop for the menu array. When you come across a menu item
with children, use another foreach loop. Alternatively you can use
recursion.

Cheers,
Rob.
-- 
..
| InterJinn Application Framework - http://www.interjinn.com |
::
| An application and templating framework for PHP. Boasting  |
| a powerful, scalable system for accessing system services  |
| such as forms, properties, sessions, and caches. InterJinn |
| also provides an extremely flexible architecture for   |
| creating re-usable components quickly and easily.  |
`'

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php