我最近在这里寻求帮助:https://codereview.stackexchange.com/questions/58544/forum-sign-up-and-sign-in-software/58552#58552并且有人重组我的代码以使其更有条理。我想知道如何使用这个为我的每个页面添加标题:
的index.php:
<?php
# Select the title
switch(trim($_SERVER['SCRIPT_FILENAME'])){
case "members": $title = "Members"; break;
case "sign_up": $title = "Sign Up"; break;
case "sign_in": $title = "Sign In"; break;
case "change_theme": $title = "Change Theme"; break;
case "contact_us": $title = "Contact Us"; break;
case "help": $title = "Help"; break;
case "rules": $title = "Rules"; break;
default: $title = "Forums"; break;
}
# Define the page name and check for empty value
$arr = explode("/", $_SERVER['PHP_SELF']); $page_name = array_pop($arr);
if(empty($page_name)) $page_name = "index.php";
# Select the sidecase
$sidecase = "";
switch(trim($_SERVER['SCRIPT_FILENAME'])){
case 'index': {
$sidecase .= '<h1 class="h1_marg_b15">Forums</h1>';
$sidecase .= '<h3 id="category_title">Categories</h3>';
} break;
case 'members': {
$sidecase .= '<h1 class="h1_marg_b15">Members</h1>';
} break;
case 'sign_up': {
$sidecase .= '<h1 class="h1_marg_b15" style="text-align: center;">Sign Up</h1>';
} break;
case 'sign_in': {
$sidecase .= '<h1 class="h1_marg_b15" style="text-align: center;">Sign In</h1>';
} break;
case 'change_theme': {
$sidecase .= '<h1 class="h1_marg_b15">Change Theme</h1>';
} break;
case 'contact_us': {
$sidecase .= '<h1 class="h1_marg_b15">Contact Us</h1>';
} break;
case 'help': {
$sidecase .= '<h1 class="h1_marg_b15">Help</h1>';
} break;
case 'rules': {
$sidecase .= '<h1 style="margin-bottom: 10px;">Rules</h1>';
$sidecase .= '<p style="margin-bottom: 15px;">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin eu nibh turpis. Nunc sit amet auctor elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. In malesuada lobortis tempus. Integer auctor condimentum sapien, non scelerisque eros cursus et. In vel leo elementum, iaculis tellus sit amet, vestibulum quam. Etiam dapibus pulvinar risus, vestibulum rhoncus sapien commodo vitae. Etiam sit amet ultrices dui. Suspendisse luctus fringilla eros. Nam vitae metus porttitor, sagittis arcu eleifend, malesuada odio. Aliquam erat volutpat.</p>';
$sidecase .= '<p style="margin-bottom: 15px;">Pellentesque id velit a elit porttitor sollicitudin et vulputate nisl. Donec eu purus non libero porta malesuada et non lorem. Vestibulum ultrices vitae elit vitae accumsan. Quisque euismod, quam sed ornare ultrices, magna mi posuere massa, vel placerat ipsum est quis erat. Aliquam non libero mauris. Etiam ligula velit, commodo et feugiat ac, porta eu orci. Donec laoreet ipsum in urna auctor, vitae malesuada nibh consequat. Donec sit amet libero vitae erat rhoncus venenatis. Maecenas nec pretium justo, eget fermentum tellus. Ut aliquet tellus venenatis posuere fermentum. Fusce mattis velit et tellus suscipit consectetur.</p>';
} break;
}
?>
<!DOCTYPE html>
<html lang="en-us">
<head>
<meta charset="UTF-8">
<title>Test - <?php echo $title; ?></title>
<link href="css/style.css" rel="stylesheet" type="text/css">
<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css" rel="stylesheet" type="text/css">
<style>
.h1_marg_b15{
margin-bottom: 15px;
}
</style>
</head>
<body>
<!-- TOP BAR -->
<div id="top_bar">
<div class="wrapper">
<div id="top_bar_links">
<ul>
<li id="home">
<a href="../">Home</a>
</li>
<li>
<a class="<?php echo ($page_name=="index.php")?"active":"";?>" href=".">Forums</a>
</li>
<li>
<a class="<?php echo ($page_name=="members.php")?"active":"";?>" href="members.php">Members</a>
</li>
</ul>
</div>
</div>
</div>
<!-- HEADER -->
<div id="header">
<div class="wrapper">
<h1 id="logo">
<a href=".">Test</a>
</h1>
<div id="member_links">
<ul>
<li id="sign_up">
<a href="sign_up.php">Sign Up</a>
</li>
<li id="sign_in">
<a href="sign_in.php">Sign In</a>
</li>
</ul>
</div>
</div>
</div>
<!-- CONTAINER -->
<div class="wrapper">
<div id="container">
<div id="breadcrumb_top">
<div class="breadcrumb_links">
<ul>
</ul>
</div>
</div>
<?php echo $sidecase; ?>
<div id="breadcrumb_bottom">
<div class="breadcrumb_links">
<ul>
</ul>
</div>
</div>
</div>
</div>
<!-- FOOTER -->
<div class="wrapper">
<div id="footer">
<div id="footer_links">
<ul>
<li id="change_theme">
<a href="change_theme.php">Change Theme</a>
</li>
<li id="contact_us">
<a href="contact_us.php">Contact Us</a>
</li>
<li id="help">
<a href="help.php">Help</a>
</li>
<li id="rules">
<a href="rules.php">Rules</a>
</li>
</ul>
</div>
<p id="footer_copyright">Forum software coded by Dylan - 2014</p>
</div>
</div>
</body>
</html>
目前,我的所有网页都有标题&#34;测试 - 论坛&#34;。
感谢。
答案 0 :(得分:1)
这很简单。
您可以使用__FILE__
。
使用下面的代码替换第一个第一个Switch()语句,但要确保正确显示标题,case语句应该等于page的值。例如,如果您在rules.php中,则标题将显示为&#34;测试 - 规则&#34;
$page = basename(__FILE__, ".php");
switch($page) {
case "members": $title = "Members"; break;
case "sign_up": $title = "Sign Up"; break;
case "sign_in": $title = "Sign In"; break;
case "change_theme": $title = "Change Theme"; break;
case "contact_us": $title = "Contact Us"; break;
case "help": $title = "Help"; break;
case "rules": $title = "Rules"; break;
default: $title = "Forums"; break;
}
答案 1 :(得分:0)
您的代码效率似乎不高。你可以使用
basename(__FILE__, '.php')
作为你的开关案例。此外,您有2个切换语句,可以打开几乎相同的参数。尝试将此作为切换语句
完成PHP
这是您的complete code