多维关联数组嵌套div菜单 [英] Multi-dimensional associative array nested div menu

查看:97
本文介绍了多维关联数组嵌套div菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用具有固定数量的项目(3)的foreach循环创建一个调用特定数组值的菜单,并且每个菜单都更深入到一个内部关联数组中,该数组具有要显示的文本等信息,背景图片和链接。我已经看到围绕其他论坛提出的半类似问题,但没有涉及这个特定范围的任何问题。



我已尝试回显并从阵列打印密钥但我保留得到一个错误,上面写着不能重新创建createitem(函数行),并且之前在最后一个结束div标记的行上声明了8行。考虑到这是函数的唯一迭代,我不确定在这种情况下重新声明了什么。



我知道还有一些需要更多的行要添加完成循环,但假设链接和包含指向正确的文件(我的编程语法很可能不正确处理这个特定的问题),任何人都可以告诉我正确的方法来实现我的目标或者甚至可能建议更好的方法来实现这一目标?最好没有数据库,但可能包括Javascript?



我尝试过:



ARRAY.PHP

 <?php  
$ myarray =数组(
' item1' => ;数组(
' text' => ' 项目1'
' image' => ' image1.png'
' link' => ' < span class =code-string> site.com/link1.php'
),
' item2' =>数组(
' text' => ' 项目2'
' image' => ' image2.png'
' link' => ' site.com/link2.php'
),
' item3' =>数组(
' < span class =code-string> text' => ' Item 3'
' image' => ' <温泉n class =code-string> image3.png'
' link' => ' site.com/link3.php'

);
?>







PAGE.PHP

 <   head  >  
< link rel = stylesheet type = text / css href = page.css media = screen >
< / head >
<? php
包括' array.php';
foreach ($ myarray as $ menuitem)
{
function createitem()
{
echo ' < div class =backgrounddiv>< div = class =textdivonclick =location.href ='<! - 各自的链接 for 每个项目 - > ' ; style =cursor:pointer;><! - 每个项目的相应文本 - >< / div>< / div>'
};
};
?>







PAGE.CSS

 backgrounddiv  
{
position 绝对;
width 100px;
height 100px;
background-image <! - 每个项目的相应图片 - > ;;
}
textdiv
{
position 绝对;
w ^宽度 100px;
高度 100px;
}

解决方案

myarray = array(
' item1' =>数组(
' text' => ; ' 项目1'
' image' => ' image1.png '
' link' => ' site.com/link1.php'
),
' item2' =>数组(
' text' => ' Item 2'
' image' => ' image2.png'
' link' => ' site.com/link2.php'
),
' item3' =>数组(
' text' => ' 项目3'
' image' => < span class =code -string>' image3.png'
' link' => ' site.com/link3.php'

);
?>







PAGE.PHP

 <   head  >  
< link rel = stylesheet type = text / css href = page.css media = screen >
< / head >
<? php
包括' array.php';
foreach


myarray as

menuitem)
{
function createitem()
{
echo ' < div class =backgrounddiv>< div = class =textdivonclick =location.href ='<! - 各自的链接 每个项目 - > ' ; style =cursor:pointer;><! - 每个项目的相应文本 - >< / div>< / div>'
};
};
?>







PAGE.CSS

 backgrounddiv  
{
position 绝对;
width 100px;
height 100px;
background-image <! - 每个项目的相应图片 - > ;;
}
textdiv
{
position 绝对;
w ^宽度 100px;
高度 100px;
}


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.

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 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?

What I have tried:

ARRAY.PHP

<?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'
          )
     );
?>




PAGE.PHP

<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>'
          };
     };
?>




PAGE.CSS

.backgrounddiv
{
     position: absolute;
     width: 100px;
     height: 100px;
     background-image:<!--respective image for each item-->;
}
.textdiv
{
     position: absolute;
     width: 100px;
     height: 100px;
}

解决方案

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' ) ); ?>




PAGE.PHP

<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>' }; }; ?>




PAGE.CSS

.backgrounddiv
{
     position: absolute;
     width: 100px;
     height: 100px;
     background-image:<!--respective image for each item-->;
}
.textdiv
{
     position: absolute;
     width: 100px;
     height: 100px;
}


这篇关于多维关联数组嵌套div菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆