dongtan8979 2013-09-06 22:31
浏览 39
已采纳

使用PHP变量进行网站导航

I've used this method on a few website builds with no obvious problems. But I'm wondering if it's an okay/proper way to utilize PHP variables as website navigation/page indicators so that site-wide changes can be easily managed in 1 file (upper.php)? Are there any drawbacks? Maybe too many requests to the server, load times, improper coding or negative S.E.O. ramifications?

I've omitted surrounding code such as < body >, etc. for simplicity and indicated where more code would be with "..."

index.php

<?php $urhere="home"; include ("upper.php"); ?>
    <div>This is the Home page content</div>   
... 

about.php

<?php $urhere="about"; include ("upper.php"); ?>
    <div>This is the About page content</div>
...    

contact.php

<?php $urhere="contact"; include ("upper.php"); ?>
    <div>This is the Contact page content</div> 
...

upper.php

...

<meta name="description" content="
    <?php
        if ($urhere=="home") echo "Home page description";
        if ($urhere=="about") echo "About page description";
        if ($urhere=="contact") echo "Contact page description";
    ?>

...

<title>
    <?php
        if ($urhere=="home") echo "Home page Title";
        if ($urhere=="about") echo "About page Title";
        if ($urhere=="contact") echo "Contact page Title"; 
    ?>
</title>

...

<div id="nav">
    <ul>
      <li><a href="/home"<?php if ($urhere=="home") echo " class=\"urhere\""; ?>>Home</a></li>
      <li><a href="/about"<?php if ($urhere=="about") echo " class=\"urhere\""; ?>>About</a></li>
      <li><a href="/contact"<?php if ($urhere=="contact") echo " class=\"urhere\""; ?>>Contact</a></li>
    </ul>
</div>
  • 写回答

2条回答 默认 最新

  • doulan2827 2013-09-06 23:26
    关注

    I see some serious maintenance problems with this approach.

    Every time you add a new page, you have to add three (for now) if-statements.

    upper.php knows too much about other pages -- their window titles and metadata.

    I would suggest that instead of using the $urhere variable to make all the decisions in upper.php, you set variables, like window title and metadata, which can be directly used by upper.php. Here's an example:

    upper.php

    <meta name="description" content="<?php echo $description ?>"/>
    ...
    <title><?php echo $title ?></title>
    ...
    <?php
    $pageTypeToLinkMap = array(
        'home' => array(
            'url' => '/home',
            'linkText' => 'Home'
        ),
        'about' => array(
            'url' => '/about',
            'linkText' => 'About',
            'sublinks' => array(
                'who-we-are' => array(
                    'url' => '/who-we-are',
                    'linkText' => 'Who We Are'
                ),
                'what-we-do' => array(
                    'url' => '/what-we-do',
                    'linkText' => 'What We Do',
                    'sublinks' => array(
                        'business' => array(
                            'url' => '/business',
                            'linkText' => 'Business'
                        ),
                        'technology' => array(
                            'url' => '/technology',
                            'linkText' => 'Technology'
                        )
                    )
                )
            )
        ),
        'contact' => array(
            'url' => '/contact',
            'linkText' => 'Contact'
        )
    );
    
    function getLinkElement($pageType, array $linkData)
    {
        $class = $urhere == $pageType ? 'urhere' : '';
        $anchorElement = "<a href=\"$linkData[url]\" class=\"$class\">$linkData[linkText]</a>";
    
        if (isset($linkData['sublinks']))
        {
            $sublinkElements = array();
    
            foreach ($linkData['sublinks'] as $sublinkPageType => $sublinkData)
                $sublinkElements[] = getLinkElement($sublinkPageType, $sublinkData);
    
            $sublinksListElement = '<ul>' . implode($sublinkElements) . '</ul>';
        }
        else
            $sublinksListElement = '';
    
        return "<li>$anchorElement$sublinksListElement</li>";
    }
    ?>
    <div id="nav">
        <ul>
            <?php foreach ($pageTypeToLinkMap as $pageType => $link) echo getLinkElement($pageType, array $link); ?>
        </ul>
    </div>
    

    index.php

    <?php
    $pageTitle = "Home";
    $description = "This is home.";
    $urhere = "home"; // I guess you still need this variable to make decisions in the "nav" div.
    ?>
    <div>This is the Home page content</div>
    

    about.php

    <?php
    $pageTitle = "About";
    $description = "About us";
    $urhere = "about";
    ?>
    <div>This is the About page content</div>
    

    EDIT

    In response to your comment, here's how you can improve your upper.php while keeping the same original structure:

    upper.php

    <?php
    $titles = array(
        'home' => 'Home page Title',
        'about' => 'About page Title',
        'contact' => 'Contact page Title'
    );
    
    $descriptions = array(
        'home' => 'Home page description',
        'about' => 'About page description',
        'contact' => 'Contact page description'
    );
    
    $linkClasses = array_fill_keys(array_keys($titles), '');
    $linkClasses[$urhere] = 'urhere';
    ?>
    
    ...
    
    <meta name="description" content="<?php echo $descriptions[$urhere] ?>"/>
    
    ...
    
    <title><?php echo $titles[$urhere] ?></title>
    
    ...
    
    <div id="nav">
        <ul>
            <li><a href="/home" class="<?php echo $linkClasses['home'] ?>">Home</a></li>
            <li><a href="/about" class="<?php echo $linkClasses['about'] ?>">About</a></li>
            <li><a href="/contact" class="<?php echo $linkClasses['contact'] ?>">Contact</a></li>
        </ul>
    </div>
    

    There you go, if-less code.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line