我有一个博客,我希望 HTML 文档的 head 部分中的标记使用 PHP 动态更改。这是我的PHP代码:
<?php
session_start();
if(isset($_SESSION['admin']) and !empty($_SESSION['admin'])){
echo '<a href="logout.php" style="color:yellow;">Logout</a>';
}
require('mysql2.php');
//Displaying the information from the DB to the user
//with pagination, i.e different pages
function post(){
$sp_query = mysql_query("SELECT COUNT(id) FROM posts");
$pages = ceil(mysql_result($sp_query, 0) / 1);
$page = (isset ($_GET['page'])) ? (int)$_GET['page'] : 1;
$start = ($page - 1) * 1;
$query = mysql_query("SELECT id,title, article, datetime
FROM posts ORDER BY id DESC LIMIT $start, 1 ");
//Check if there are any rows in the DB
if(mysql_num_rows($query)){
while($result = mysql_fetch_assoc($query)){
//displaying the results
echo '<article class="blog-post">
<div class="blog-info">
'.stripslashes($result['title']).' ';
echo
stripslashes($result['datetime']).'<div>';
echo
nl2br(stripslashes($result['article'])).'</article>';
$title[] = $result['title'];
$id =
mysql_real_escape_string((int)$result['id']);
echo '<input type="hidden"
value="'.$result['id'].'" />';
$_SESSION['post_id'] = $id;
//If the admin is logged in, session variables
//for editing and deleting a post must be set
//and the conforming links should be displayed
if(isset($_SESSION['admin']) and
!empty($_SESSION['admin'])){
//$_SESSION['post_id'] = $id;
$_SESSION['edit_post'] = $id;
echo '<article class="blog-post">';
echo '<a href="delete.php"
id="delete" onclick="return del();">Delete this post</a>';
echo '<br /><a
href="edit_post.php">Edit this post</a>';
echo '</article>';
}
}
}
//The drop down menu
function options(){
$new_query = mysql_query("SELECT title FROM posts
ORDER BY id DESC");
$num = 1;
while($array = mysql_fetch_assoc($new_query)){
echo '<option
value="blog.php?page='.$num++.'">'.$array['title'].'</a></option>';
}
}
?>
这是HTML:
<?php require('mysql2.php');
require('blog_process.php');
?>
<!DOCTYPE html>
<html>
<head>
<!--Meta Data-->
<meta charset="utf-8">
<meta name="description" content="About Chris Shilts">
<meta name="author" content="Chris Shilts">
<meta name="keywords" content="chris, shilts">
<meta name="viewport" content="width=device-width, initial-scale=1,
maximum-scale=1">
<meta http-equiv="X-UA-Compatible" content="IE=9" />
<!--css-->
<link rel="stylesheet" href="reset.css">
<link rel="stylesheet" href="style.css">
<!-- Javascript -->
<script type="text/javascript" src="delete.js"></script>
<!-- Favicons-->
<link rel="shortcut icon" href="favicon.ico">
<!--Title-->
<title id="title"><?php //php code should go here?></title>
</head>
<body>
<!--Contains all content-->
<div id="container">
<!--Content at start of page, generally the same-->
<header>
<div id="logo">
Hello There!
</div>
<!--Primary Navigation-->
<nav>
<a href="#">Link</a>
<a href="#">Link</a>
<a href="#">Link</a>
</nav>
</header>
<!--Blog Posts-->
<?php post();?>
<!-- The navigation bar for the blog posts -->
<select onclick="navigation();" id="select">
<option value="" selected="selected"></option>
<?php options(); ?>
</select>
<!--First Footer-->
<footer id="footer-one">
Site Design By <a href = "#">Chris Shilts</a> | Programming by
<a href = "#">Stefany Dyulgerova</a>
</footer>
<!--Second Footer-->
<footer id="footer-two">
<a href="http://www.w3.org/html/logo/">
<img id="html5" src="http://www.w3.org/html/logo/badge
with CSS3 / Styling">
</a>
</footer>
<!--/container-->
</div>
</body>
</html>
我也有一个 mysql 数据库,这里是那里的字段:
id title article dateime
请帮我!
我试图将 $result['title'] 放入这样的数组中
$title[] = $result['title'];
然后根据 $_SESSION['post_id'] 中的 id 在元素中循环它,但问题是我必须重新加载页面两次才能使标题生效。