有没有办法让这个PHP代码更有效?

时间:2012-08-02 22:11:40

标签: php html css

我正在尝试在网页上制作一些“动态”链接。基本上我想删除我所在页面的链接。 (例如,如果我在主页上,我不希望显示主页链接,但如果我在FAQ页面上,将显示主页链接,而FAQ链接将不会显示。)此代码按我希望的方式工作工作,我只是认为必须有一个更好的方法,不能复制如此多的代码,但在我有限的编程经验中,我可以看到它。

<div class= "links">
<?php
    switch ($page_title) {
            case 'Home':
            echo '<p class= "who">'.
                    '<a href="whois.php" tabindex="1"><img src=     "images/WhoIs.png" alt= "Who is BeeCharmer" /></a>'.
                 '</p>';
            echo '<p class= "contact">'.
                    '<a href="contact.php" tabindex="2"><img src= "images/ContactUs.png" alt= "Contact Us" /></a>'.
             '</p>';
        echo '<p class= "what">'.
                '<a href="whatwedo.php" tabindex="3"><img src= "images/WhatWeDo.png" alt= "What Does BeeCharmer Do" /></a>'.
             '</p>';
        echo '<p class ="FAQ">'.
                '<a href="faq.php" tabindex="4"><img src= "images/FAQ.png" alt= "Frequently Asked Questions" /></a>'.
             '</p>';
        echo '<p class= "pics">'.
                '<a href="gallery.php" tabindex="5"><img src= "images/gallery.png" alt= "Photo Gallery" /></a>'.
             '</p>';
        echo '<p class= "pollination">'.
                 '<a href="pollination.php" tabindex="6"><img src= "images/Pollination.png" alt= "Pollination Services" /></a>'.
             '</p>';
        echo '<p class= "pay">'.
                 '<a href="payus.php" tabindex="7"><img src= "images/PayUs.png" alt= "Pay BeeCharmer" /></a>'.
             '</p>';
        break;


    case 'Who is BeeCharmer':
        echo '<p class= "home">'.
                '<a href="index.php" tabindex="1"><img src= "images/HomePage.png"  alt= "Home Page" /></a>'.
              '</p>';
        echo '<p class= "contact">'.
                '<a href="contact.php" tabindex="2"><img src= "images/ContactUs.png" alt= "Contact Us" /></a>'.
             '</p>';
        echo '<p class= "what">'.
                '<a href="whatwedo.php" tabindex="3"><img src= "images/WhatWeDo.png" alt= "What Does BeeCharmer Do" /></a>'.
             '</p>';
        echo '<p class ="FAQ">'.
                '<a href="faq.php" tabindex="4"><img src= "images/FAQ.png" alt= "Frequently Asked Questions" /></a>'.
             '</p>';
        echo '<p class= "pics">'.
                '<a href="gallery.php" tabindex="5"><img src= "images/gallery.png" alt= "Photo Gallery" /></a>'.
             '</p>';
        echo '<p class= "pollination">'.
                 '<a href="pollination.php" tabindex="6"><img src= "images/Pollination.png" alt= "Pollination Services" /></a>'.
             '</p>';
        echo '<p class= "pay">'.
                 '<a href="payus.php" tabindex="7"><img src= "images/PayUs.png" alt= "Pay BeeCharmer" /></a>'.
             '</p>';
        break;

    case 'Contact Us':
        echo '<p class= "home">'.
                '<a href="index.php" tabindex="1"><img src= "images/HomePage.png"  alt= "Home Page" /></a>'.
              '</p>';
        echo '<p class= "who">'.
                '<a href="whois.php" tabindex="2"><img src= "images/WhoIs.png" alt= "Who is BeeCharmer" /></a>'.
             '</p>';
        echo '<p class= "what">'.
                '<a href="whatwedo.php" tabindex="3"><img src= "images/WhatWeDo.png" alt= "What Does BeeCharmer Do" /></a>'.
             '</p>';
        echo '<p class ="FAQ">'.
                '<a href="faq.php" tabindex="4"><img src= "images/FAQ.png" alt= "Frequently Asked Questions" /></a>'.
             '</p>';
        echo '<p class= "pics">'.
                '<a href="gallery.php" tabindex="5"><img src= "images/gallery.png" alt= "Photo Gallery" /></a>'.
             '</p>';
        echo '<p class= "pollination">'.
                 '<a href="pollination.php" tabindex="6"><img src= "images/Pollination.png" alt= "Pollination Services" /></a>'.
             '</p>';
        echo '<p class= "pay">'.
                 '<a href="payus.php" tabindex="7"><img src= "images/PayUs.png" alt= "Pay BeeCharmer" /></a>'.
             '</p>';
        break;

    case 'What We Do':
        echo '<p class= "home">'.
                '<a href="index.php" tabindex="1"><img src= "images/HomePage.png"  alt= "Home Page" /></a>'.
              '</p>';
        echo '<p class= "who">'.
                '<a href="whois.php" tabindex="2"><img src= "images/WhoIs.png" alt= "Who is BeeCharmer" /></a>'.
             '</p>';
        echo '<p class= "contact">'.
                '<a href="contact.php" tabindex="3"><img src= "images/ContactUs.png" alt= "Contact Us" /></a>'.
             '</p>';
        echo '<p class ="FAQ">'.
                '<a href="faq.php" tabindex="4"><img src= "images/FAQ.png" alt= "Frequently Asked Questions" /></a>'.
             '</p>';
        echo '<p class= "pics">'.
                '<a href="gallery.php" tabindex="5"><img src= "images/gallery.png" alt= "Photo Gallery" /></a>'.
             '</p>';
        echo '<p class= "pollination">'.
                 '<a href="pollination.php" tabindex="6"><img src= "images/Pollination.png" alt= "Pollination Services" /></a>'.
             '</p>';
        echo '<p class= "pay">'.
                 '<a href="payus.php" tabindex="7"><img src= "images/PayUs.png" alt= "Pay BeeCharmer" /></a>'.
             '</p>';
        break;

    case 'FAQ':
        echo '<p class= "home">'.
                '<a href="index.php" tabindex="1"><img src= "images/HomePage.png"  alt= "Home Page" /></a>'.
              '</p>';
        echo '<p class= "who">'.
                '<a href="whois.php" tabindex="2"><img src= "images/WhoIs.png" alt= "Who is BeeCharmer" /></a>'.
             '</p>';
        echo '<p class= "contact">'.
                '<a href="contact.php" tabindex="3"><img src= "images/ContactUs.png" alt= "Contact Us" /></a>'.
             '</p>';
        echo '<p class= "what">'.
                '<a href="whatwedo.php" tabindex="4"><img src= "images/WhatWeDo.png" alt= "What Does BeeCharmer Do" /></a>'.
             '</p>';
        echo '<p class= "pics">'.
                '<a href="gallery.php" tabindex="5"><img src= "images/gallery.png" alt= "Photo Gallery" /></a>'.
             '</p>';
        echo '<p class= "pollination">'.
                 '<a href="pollination.php" tabindex="6"><img src= "images/Pollination.png" alt= "Pollination Services" /></a>'.
             '</p>';
        echo '<p class= "pay">'.
                 '<a href="payus.php" tabindex="7"><img src= "images/PayUs.png" alt= "Pay BeeCharmer" /></a>'.
             '</p>';
        break;

    case 'Photo Gallery':
        echo '<p class= "home">'.
                '<a href="index.php" tabindex="1"><img src= "images/HomePage.png"  alt= "Home Page" /></a>'.
              '</p>';
        echo '<p class= "who">'.
                '<a href="whois.php" tabindex="2"><img src= "images/WhoIs.png" alt= "Who is BeeCharmer" /></a>'.
             '</p>';
        echo '<p class= "contact">'.
                '<a href="contact.php" tabindex="3"><img src= "images/ContactUs.png" alt= "Contact Us" /></a>'.
             '</p>';
        echo '<p class= "what">'.
                '<a href="whatwedo.php" tabindex="4"><img src= "images/WhatWeDo.png" alt= "What Does BeeCharmer Do" /></a>'.
             '</p>';
        echo '<p class ="FAQ">'.
                '<a href="faq.php" tabindex="5"><img src= "images/FAQ.png" alt= "Frequently Asked Questions" /></a>'.
             '</p>';
        echo '<p class= "pollination">'.
                 '<a href="pollination.php" tabindex="6"><img src= "images/Pollination.png" alt= "Pollination Services" /></a>'.
             '</p>';
        echo '<p class= "pay">'.
                 '<a href="payus.php" tabindex="7"><img src= "images/PayUs.png" alt= "Pay BeeCharmer" /></a>'.
             '</p>';
        break;

    case 'Pollination Services':
        echo '<p class= "home">'.
                '<a href="index.php" tabindex="1"><img src= "images/HomePage.png"  alt= "Home Page" /></a>'.
              '</p>';
        echo '<p class= "who">'.
                '<a href="whois.php" tabindex="2"><img src= "images/WhoIs.png" alt= "Who is BeeCharmer" /></a>'.
             '</p>';
        echo '<p class= "contact">'.
                '<a href="contact.php" tabindex="3"><img src= "images/ContactUs.png" alt= "Contact Us" /></a>'.
             '</p>';
        echo '<p class= "what">'.
                '<a href="whatwedo.php" tabindex="4"><img src= "images/WhatWeDo.png" alt= "What Does BeeCharmer Do" /></a>'.
             '</p>';
        echo '<p class ="FAQ">'.
                '<a href="faq.php" tabindex="5"><img src= "images/FAQ.png" alt= "Frequently Asked Questions" /></a>'.
             '</p>';
        echo '<p class= "pics">'.
                '<a href="gallery.php" tabindex="6"><img src= "images/gallery.png" alt= "Photo Gallery" /></a>'.
             '</p>';
        echo '<p class= "pay">'.
                 '<a href="payus.php" tabindex="7"><img src= "images/PayUs.png" alt= "Pay BeeCharmer" /></a>'.
             '</p>';
        break;

    case 'Pay Us':
        echo '<p class= "home">'.
                '<a href="index.php" tabindex="1"><img src= "images/HomePage.png"  alt= "Home Page" /></a>'.
              '</p>';
        echo '<p class= "who">'.
                '<a href="whois.php" tabindex="2"><img src= "images/WhoIs.png" alt= "Who is BeeCharmer" /></a>'.
             '</p>';
        echo '<p class= "contact">'.
                '<a href="contact.php" tabindex="3"><img src= "images/ContactUs.png" alt= "Contact Us" /></a>'.
             '</p>';
        echo '<p class= "what">'.
                '<a href="whatwedo.php" tabindex="4"><img src= "images/WhatWeDo.png" alt= "What Does BeeCharmer Do" /></a>'.
             '</p>';
        echo '<p class ="FAQ">'.
                '<a href="faq.php" tabindex="5"><img src= "images/FAQ.png" alt= "Frequently Asked Questions" /></a>'.
             '</p>';
        echo '<p class= "pics">'.
                '<a href="gallery.php" tabindex="6"><img src= "images/gallery.png" alt= "Photo Gallery" /></a>'.
             '</p>';
        echo '<p class= "pollination">'.
                 '<a href="pollination.php" tabindex="7"><img src= "images/Pollination.png" alt= "Pollination Services" /></a>'.
             '</p>';
        break;
}

&GT?;

这是定制它的CSS。

div.links { width: 40%;
                    position: absolute;
                    top: 280px;
                    left: 2%;  }
        div.links p img { border: none; 
                          position: relative;
                          top: -29px;
                          left: 5px; }
        div.links p { background-image: url("../images/yellowSlidingDoorleft.png"),
                                        url("../images/yellowSlidingDoorRight.png"),
                                        url("../images/yellowSlidingDoorMiddle.png");
                      background-position: top left, top right, top center;
                      background-repeat: no-repeat, no-repeat, repeat; 
                      height: 38px;
                      width: 520px;
                      border: none;
                      overflow: hidden; }
        /*div.links p.home { position: relative;
                top: 0px;
                left: 0px; }
        div.links p.who { position: relative;
                top: 50px;
                left: 0px; }
        div.links p.contact { position: relative;
                top: 100px;
                left: 0px; }
        div.links p.what { position: relative;
                top: 150px;
                left: 0px; }
        div.links p.FAQ { position: relative;
                top: 200px;
                left: 0px; }
        div.links p.pics { position: relative;
                top: 250px;
                left: 0px; }
        div.links p.pollination { position: relative;
                top: 300px;
                left: 0px; }
        div.links p.pay { position: relative;
                top: 350px;
                left: 0px; }*/
        div.links p:hover img { position: relative;
                top: 5px;
                left: 5px; }
        div.links p:hover { background-image: url("../images/blackSlidingDoorleft.png"),
                                              url("../images/blackSlidingDoorRight.png"),
                                              url("../images/blackSlidingDoorMiddle.png");
                            background-position: top left, top right, top center;
                            background-repeat: no-repeat, no-repeat, repeat; }

你们能给予的任何帮助都会非常感激

5 个答案:

答案 0 :(得分:3)

我认为你想要的是这样的:

<div class= "links">
<?php
    if ( $page_title != 'home') {
       echo '<p class= "home">'.
            '<a href="index.php" tabindex="1"><img src= "images/HomePage.png"  alt= "Home Page" /></a>'.
            '</p>';
    }
    if ( $page_title != 'Who is BeeCharmer') {
       echo '<p class= "who">'.
            '<a href="whois.php" tabindex="2"><img src=     "images/WhoIs.png" alt= "Who is BeeCharmer" /></a>'.
            '</p>';
    }

//and so on...

答案 1 :(得分:1)

如果你真的为每个页面创建一个单独的列表(如switch($page_title)似乎暗示的那样),那么简单地将该页面的链接列表作为HTML包含在该页面上会更有效率。

答案 2 :(得分:1)

您可以通过功能删除冗余的HTML代码。 (在这种情况下工作,因为所有链接的格式完全相同,只有某些元素,如class,href,tabindex,imgsrc和img alt不同)

function displayLinks ($pClass, $aHref, $tabIndex, $imgSRC, $alt) {
echo '<p class="' . $pClass . '"><a href="' . $aHref . '.php" tabindex="' . $tabIndex . '"><img src="' . $imgSRC . '" alt= "' . $alt . '" /></a>'</p>';
}

并且会被称为

displayLinks('who','whois',1,'images/WhoIs.png','Who is BeeCharmer');
displayLinks('contact','contact',2,'images/ContactUs.png','Who is ContactUs');

等等

虽然由于你的大多数链接都是相同的,我会更进一步,只保留变化链接的条件。

what, faq, pics, pollination, pay 

除了Home之外都存在并联系我们。因此,如果

,他们不需要重复或封闭

答案 3 :(得分:1)

您已走上正轨,意识到您当前的解决方案并非动态。许多缺乏经验的程序员似乎犯了在这种情况下使用switch语句的错误,当你真正想要的是动态代码访问数组时,正如@Torsten提出的那样。你也不需要重复的if语句,如PeterVR的答案,或重复的函数调用,如DaiYoukai的。将重复放入数组中可以为您提供灵活,动态的结构,您可以以编程方式访问和操作。

尝试这样的事情(添加数组所需的任何其他键值对):

$nav_items = array(

  'home' => array(

    'class' => 'home',

    'href' => "index.php",

    'src' => "HomePage"

    'label' => "Home Page"

  ),
  // home


  'whois' => array(

    'class' => 'who',

    'href' => "...",

    'src' => "..."

    'label' => "..."

  ),
  // whois

  ...

);
// $nav_items


$nav_sets = array(

  'home' => array(

    'whois',

    'contact',

    'whatwedo',

    'faq',

    'gallery',

    'pollination',

    'payus'

  ),


  'whois' => array(

    'home',

    'contact',

    'whatwedo',

    'faq',

    'pics',

    'pollination'

  ),

  ...

);
// $nav_sets


foreach ( $nav_sets[ $page_title ] as $nav_id ) {

  $nav_item = $nav_items[ $nav_id ];

  echo <<<DOCHERE

<p class="{$nav_item[ 'class' ]}">
<a href="{$nav_item[ 'href' ]}" tabindex="1"><img src="images/{$nav_item[ 'src' ]}.png" alt= "{$nav_item[ 'label' ]}" /></a>
</p>

DOCHERE;

}
// foreach

这与您对$page_title的使用不完全匹配 - 我只是在每个页面中添加一个变量,例如$nav_id,与$nav_items中的键匹配}和$nav_sets数组。

在这里,您可以获得每个导航项的一个数据副本(可以显示在任意数量的页面上),以及一个用于为所有项目生成HTML输出的模板。这消除了有问题的重复。此示例包含明确的项目集,以防您需要那么多控件。如果您只需要在每个页面上显示除当前页面对应的项目之外的所有项目,那么您可以循环遍历$nav_items并排除当前页面。

我还消除了字符串连接的可怕使用,以建立输出。

答案 4 :(得分:0)

您不必,也可能不得不直接回复HTML。由于我认为这是一个视图文件,我会将导航文件放入其自己的文件中,然后执行当前链接所在的require('nav_links.php');,其中nav_links.php的内容如下所示

nav_links.php

<div class= "links">
<?php if($page_title != 'home'): ?>
<p class= "home">.
    <a href="index.php" tabindex="1"><img src= "images/HomePage.png"  alt= "Home Page" /></a>.
</p>
<?php endif; ?>
<?php if($page_title != 'Who is BeeCharmer'): ?>
 <p class= "who">
    <a href="whois.php" tabindex="2"><img src=     "images/WhoIs.png" alt= "Who is BeeCharmer" /></a>
 </p>
<?php endif; ?>
<?php if($page_title != 'Contact Us'): ?>
 <p class= "contact">
        <a href="contact.php" tabindex="3"><img src= "images/ContactUs.png" alt= "Contact Us" /></a>
 </p>
<?php endif; ?>
<?php if($page_title !=  'What We Do'): ?>
<p class= "what">
    <a href="whatwedo.php" tabindex="4"><img src= "images/WhatWeDo.png" alt= "What Does BeeCharmer Do" /></a>
</p>
<?php endif; ?>
<?php if($page_title != 'FAQ'): ?>
 <p class ="FAQ">
    <a href="faq.php" tabindex="5"><img src= "images/FAQ.png" alt= "Frequently Asked Questions" /></a>
 </p>
<?php endif; ?>
<?php if($page_title != 'Photo Gallery'): ?>
 <p class= "pics">
    <a href="gallery.php" tabindex="6"><img src= "images/gallery.png" alt= "Photo Gallery" /></a>
 </p>
<?php endif; ?>
<?php if($page_title != 'Pollination Services'): ?>
 <p class= "pollination">
     <a href="pollination.php" tabindex="7"><img src= "images/Pollination.png" alt= "Pollination Services" /></a>
 </p>
<?php endif; ?>
<?php if($page_title != 'Pay Us'): ?>
 <p class= "pay">.
     <a href="payus.php" tabindex="8"><img src= "images/PayUs.png" alt= "Pay BeeCharmer" /></a>
 </p>
<?php endif; ?>