+ 2

What is the most efficient way to create php nested div menu using multi-dimension associative arrays.

I am trying to create a menu calling specific array values using foreach loops with a fixed number of items (3) and each one going a tier deeper into an inner-associative array that has the information like the text to display, the background image, and the link. I have seen semi-similar questions asked around other forums but nothing that touches this particular span. What I have so far: ARRAY.PHP [code]<?php $myarray = array( 'item1'=>array( 'text'=>'Item 1', 'image'=>'image1.png', 'link'=>'site.com/link1.php' ), 'item2'=>array( 'text'=>'Item 2', 'image'=>'image2.png', 'link'=>'site.com/link2.php' ), 'item3'=>array( 'text'=>'Item 3', 'image'=>'image3.png', 'link'=>'site.com/link3.php' ) ); ?>[/code] PAGE.PHP [code]<head> <link rel="stylesheet" type="text/css" href="page.css" media="screen"> </head> <?php include 'array.php'; foreach ($myarray as $menuitem) { function createitem() { echo '<div class="backgrounddiv"><div= class="textdiv" onclick="location.href='<!--respective link for each item-->';" style="cursor: pointer;"><!--respective text for each item--></div></div>' }; }; ?>[/code] PAGE.CSS (relevant classes) [code].backgrounddiv { position: absolute; width: 100px; height: 100px; background-image:<!--respective image for each item-->; } .textdiv { position: absolute; width: 100px; height: 100px; }[/code] I have tried both echoing and printing the keys from the arrays but I keep getting an error that says "cannot redeclare createitem" (the function line) and that it is previously declared 8 lines later on the line with the last closing div tag. I am not really sure what is being redeclared in this case considering that is the only iteration

23rd Jun 2017, 2:38 PM
Lucas Mills
Lucas Mills - avatar
3 Answers
+ 4
Try this: <?php include 'array.php'; $control_first = 1; $control_second = 1; foreach ($myarray as $menuitem) { foreach ($menuitem as $item) { echo '<div class="backgrounddiv_' . $control_first . $control_second . '"><div class="textdiv" onclick="location.href=' . $item["link"] . ';" style="cursor: pointer;">' . $item["text"] . '</div></div>'; $control_second++; }; $control_first++; }; ?> include your css inside the tag <head> main document and try this: <style> <?php $control_first = 1; $control_second = 1; foreach ($myarray as $menuitem): foreach ($menuitem as $item): ?> .backgrounddiv_ <?php echo $control_first . $control_second; ?> { position: absolute; width: 100px; height: 100px; background-image: url(<?php echo $item['image']; ?>); } <?php $control_second++; endforeach; $control_first++; endforeach; ?> .textdiv { position: absolute; width: 100px; height: 100px; } </style>
23rd Jun 2017, 3:23 PM
Edson
Edson - avatar
+ 1
It keeps cutting off, so to finish the question: ...iteration of the function on there. I understand there are a few more lines that need to be added to finish the loop but assuming the links and includes are pointing to the right files (and my programming syntax is most likely incorrect to handle this particular issue), can anyone tell me the proper way to go about what I am aiming for or maybe even suggest a better way of achieving this? Preferably without databases, but maybe including Javascript?
23rd Jun 2017, 2:38 PM
Lucas Mills
Lucas Mills - avatar
+ 1
Thanks for replying! So I looked at your code and ended up just taking mine down a tier, using a single array instead of using multi dimensional arrays. Since each of the menu items were labeled the same thing I was going for within the next dimension. I just used a foreach loop to call each index and look for the image with each word at the end or link to the site.com/index. It was a lot simpler and much more organized than I expected in the end. I may revisit this concept if I need to do something more in-depth later on. I appreciate you helping me out.
24th Jun 2017, 2:44 PM
Lucas Mills
Lucas Mills - avatar