我有一个HTML菜单,我用CSS设置它以突出显示当前页面。因为我有超过8页使用相同的代码。我想做一些动态的东西,我为它创建了一个PHP函数,
这是我的代码..
<?php function hiddenmenu($activeicon1 = 0, $activeicon2 = 0, $activeicon3 = 0, $activeicon4 = 0,
$activeicon5 = 0, $activeicon6 = 0, $activeicon7 = 0, $activeicon8 = 0, $activeicon9 = 0) {
?>
<div class="icon-no-spacer">
<p><a id="<?php echo $activeicon1; ?>" href="index.php" title=""><img src="images/icons/48/display.png" alt="" />Dashboard</a></p>
</div>
<div class="icon-spacer">
<p><a id="<?php echo $activeicon2; ?>" href="post-news.php" title=""><img src="images/icons/48/text_rtf.png" alt="" />Post News</a></p>
</div>
<div class="icon-spacer">
<p><a id="<?php echo $activeicon3; ?>" href="news.php" title=""><img src="images/icons/48/wordprocessing.png" alt="" />News</a></p>
</div>
<div class="icon-spacer">
<p><a id="<?php echo $activeicon4; ?>" href="post-advertisement.php" title="" ><img src="images/icons/48/view_pim_news.png" alt="" />Post Ad</a></p>
</div>
<div class="icon-spacer">
<p><a id="<?php echo $activeicon5; ?>" href="advertisement.php" title=""><img src="images/icons/48/view_pim_tasks.png" alt="" />Advertise</a></p>
</div>
<div class="icon-spacer">
<p><a id="<?php echo $activeicon6; ?>" href="comments.html" title=""><img src="images/icons/48/spread.png" alt="" />Comments</a></p>
</div>
<div class="icon-spacer">
<p><a id="<?php echo $activeicon7; ?>" href="#" title=""><img src="images/icons/48/rss_tag.png" alt="" />Sponsors</a></p>
</div>
<div class="icon-spacer">
<p><a id="<?php echo $activeicon8; ?>" href="#" title=""><img src="images/icons/48/fileview_preview.png" alt="" />Video</a></p>
</div>
<div class="icon-spacer">
<p><a id="<?php echo $activeicon9; ?>" href="information.php" title="" ><img src="images/icons/48/help_about.png" alt="" />Information</a></p>
</div>
<?php
}
?>
代码工作正常。可以接受这个。 ??很多人可能会遇到这类问题我想知道最好的解决方案..
谢谢
答案 0 :(得分:2)
我至少会以不同的方式做这两件事:
这样的事情可以做到:
<?php
$choices = array(
1 => array("index.php", "display.png", "Dashboard"),
2 => array("post-news.php", "text_rtf.png", "Post News"),
3 => array("news.php", "wordprocessing.png", "News"),
4 => array("post-advertisement.php", "view_pim_news.png", "Post Ad"),
5 => array("advertisement.php", "view_pim_tasks.png", "Advertise"),
6 => array("comments.html", "spread.png", "Comments"),
7 => array("#", "rss_tag.png", "Sponsors"),
8 => array("#", "fileview_preview.png", "Video"),
9 => array("information.php", "help_about.png", "Information")
);
function hiddenmenu($iconIds) {
global $choices;
foreach ($iconIds as $iconId){
echo '<div class="icon-no-spacer">'
. '<p><a id="' . $iconId . '"'
. ' href="' . $choices[$iconId][0] . '"'
. ' title=""><img src="images/icons/48/' . $choices[$iconId][1] . '"'
. ' alt="" />' . $choices[$iconId][2] . '</a></p>' ."\n";
}
}
hiddenmenu(array(1,2,3,8,9));
答案 1 :(得分:1)
将代码和标记混合在一起被认为是不好的做法,如果非程序员不得不改变你的代码,他会破坏什么呢?更好的想法是使用模板引擎,这样你就有2个文件 - 一个读取模板文件(标记+指令)的PHP脚本(没有标记),并将它们一起用于输出HTML(+动态内容)。谷歌的 PHP模板引擎,有数十亿可供选择...
答案 2 :(得分:1)
我认为你想要的是这样的:
注意,我已经在这里和那里用省略号(...)缩写了代码。
<?php
function hiddenmenu($activeicon1 = 0, $activeicon2 = 0, ..., $activeicon9 = 0) {
if ($activeicon1) {
?>
<div class="icon-no-spacer">
<p><a id="activeicon1" href="index.php" title=""><img src="images/icons/48/display.png" alt="" />Dashboard</a></p>
</div>
<?php
}
if ($activeicon2) {
?>
<div class="icon-no-spacer">
<p><a id="activeicon2" href="post-news.php" title=""><img src="images/icons/48/text_rtf.png" alt="" />Post News</a></p>
</div>
<?php
}
...
}
?>
我不确定该函数的参数将被设置为什么,但大多数情况下,它可能会生成多个具有相同ID的div:0
,这是因为ID无效必须以字母AND开头,它们必须是唯一的。此外,您需要逻辑来打开和关闭这些菜单项。我上面使用的if
语句应该有帮助。
答案 3 :(得分:0)
猜猜身份过于复杂......
php代码:
<?php
function processTemplate($_file, $_vars)
{
//todo: check if file exists
extract($_vars);
ob_start();
include $_file;
return ob_get_clean();
}
function processArray($template, $choices) {
$output = '';
foreach ($choices as $choice) {
$output .= processTemplate($template, $choice);
}
return $output;
}
$choices = array(
1 => array(
'id' => '1',
'link' => "index.php",
'image' => "images/icons/48/display.png",
'text' => "Dashboard",
),
2 => array(
'id' => '2',
'link' => "post-news.php",
'image' => "images/icons/48/text_rtf.png",
'text' => "Post News",
),
//etc
);
$ids = array_flip(array(1,2));
$chosen = array_intersect_key($choices, $ids);
echo processArray('menu.phtml', $chosen);
模板文件:menu.phtml
<div class="icon-no-spacer">
<p>
<a id="<?php echo $id; ?>" href="<?php echo $link; ?>" title="">
<img src="<?php echo $image; ?>" alt="" />
<?php echo $text; ?>
</a>
</p>
</div>