我有一个博客,我希望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的元素循环遍历它,但问题是我必须重新加载页面两次才能使标题生效。
答案 0 :(得分:0)
尝试将博客标题保存在数组中并不适合您,因为$ title变量只能在post函数中访问。
您可以回复$ title数组...
//initialize the array at the beginning of the post function like so
$title = array();
//return the array at the end of the function like so
return $title;
然后在页面中,您将替换
<?php post();?>
使用
<?php $title = post();?>
但是,在您发布电话之前,您需要$ title。所以这对你没有帮助。
我建议......
1)使用全局标题变量创建一个类对象。有一个初始化函数来处理数据库调用。然后在打印出HTML页面的类上发布一个函数。
2)创建一个标题函数,返回正确的页面标题并像这样调用它。
<title id="title"><?php get_page_title(); ?></title>
更新
在你的php文件中,在选项功能之后添加以下功能:
public function get_page_title() {
//add code to get the page title
// not sure what title you want since you are showing multiple posts on one page
}
答案 1 :(得分:0)
以下是需要修复的代码段:<title id="title"><?php echo $result['title']; ?></title>
如果有一个标题,这将有效,否则采取第一个:<title id="title"><?php echo $result['title'][0]; ?></title>