繁体   English   中英

使用PHP条件语句在不同页面上切换HTML内容

[英]Using PHP conditional statements to switch HTML content on different pages

我是一个熟悉但很少使用PHP的前端开发人员。 我正在开发一个个人项目,我主要使用包括将PHP文件链接在一起。 这是我的整体基本页面结构:

<?php include('header.php'); ?>
<?php include('pagetitle.php'); ?>

Page content goes here.

<?php include('footer.php'); ?>

在pagetitle.php上,我有一个<h1><h2>和背景图片,与您所在的页面有关。

我的问题是,如何使用条件语句将所有页面标题/副标题放在pagetitle.php上,并根据您所在的页面切换它们? 所以,例如,我想要

<div id="about">
<h1>About</h1>
<h2>About page subheading</h2>
</div>

出现在about.php上,和

<div id="contact">
<h1>Contact Me</h1>
<h2>Contact page subheading</h2>
</div>

显示在contact.php等等...但只在这些页面上使用pagetitle.php。

该网站并不庞大。 它不会超过10页。 此外,我确实知道我可以在相应的页面上使用该页面标题段,但如果可能的话,我想尝试一下。

谢谢!

我会做这样的事情(没有经过测试,但应该用很少的,如果有的话,改变。)
(每个人都说,对吧?:D):

<?php
    /*
    create a map that contains the information for each page.
    the name of each page maps to an array containing the
    (div id, heading 1, & subheading for that page).
    */
    $pageinfo = array(
        "about.php" => array ("about", "About", "About Page Subheading"),
        "contact.php" => array ("contact", "Contact Me", "Contact Page Subheading"),
        );

    function printinfo($pagename) {
        /*
        This function will print the info for the current page.
        */

        global $pageinfo;

        $pagename = basename($pagename);

        #make sure we have info for this page
        if (!array_key_exists($pagename, $pageinfo) {
            echo "<p><b>You did not supply info for page $pagename</b></p>";
            return;
            }

        #we do have info ... continue
        $info = $pageinfo[$pagename];

        #let's print the div (with its custom id),
        echo "<div id='" . $info[0] . "'>\n";

        #print the headings
        echo "<h1>" . $info[1] . "</h1>\n";
        echo "<h2>" . $info[2] . "</h2>\n";

        #close the div
        echo "</div>\n";
        }
    ?>

然后在您想要div的每个页面中,您将放置以下代码:

printinfo($_SERVER['PHP_SELF']);

其他:

  • 这种方式比其他方式更灵活,牺牲了无条件陈述。 (您特别要求具有条件语句的解决方案;但是,为了灵活性和可维护性,此示例不使用switch语句或if语句。)

  • 因为没有条件语句,所以维护的代码较少。 当然,您必须使用信息设置数组,但如果您决定将<h2>更改为<h3> ,则必须仅在一个位置进行更改,等等。

在你的pagetitle.php上你可以做这样的事情

<?php

$scriptname = basename($_SERVER["PHP_SELF"]); 

if ($scriptname == "about.php") {
     echo "<h1>About Page</h1>";
}else if($scriptname == "contact.php"){
    echo "<h1>Contact Us</h1>";
}

?>

您必须在域名后面获取字符串,以便您可以使用$_SERVER['REQUEST_URI']

$page = $_SERVER['REQUEST_URI']; 

if ($page == "about.php") {
     echo "<h1>About</h1>"."<br />";
     echo "<h2>About page subheading</h2>"."<br />";
}else if($page == "contact.php"){
     echo "<h1>Contact Me</h1>"."<br />";
     echo "<h2>Contact page subheading</h2>"."<br />";
}
$page = $_SERVER['REQUEST_URI']; 
switch($page){
case 'about.php':

     echo "<h1>About</h1>";
     echo "<h2>About page subheading</h2>";

break;
case 'contact.php':
      echo "<h1>Contact Me</h1>";
      echo "<h2>Contact page subheading</h2>";
break;
default:
      echo "Invalid Request";
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM