-3

我有一个博客,我希望 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 在元素中循环它,但问题是我必须重新加载页面两次才能使标题生效。

4

2 回答 2

0

Trying to save the blog title in an array isnt't work for you because the $title variable is only accessible within the post function.

You could have post return the $title array...

//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;

Then in the page, you'd replace

<?php post();?>

With

<?php $title = post();?>

However, You need $title BEFORE you the call to post. So that won't really help you.

I would suggest...

1) Create a Class Object with a global title variable. Have an initialize function to handle the database call. Then make post a function on the class that prints out the page HTML.

2) Create a title function that returns the correct page title and call it like so..

<title id="title"><?php get_page_title(); ?></title>

Update:

In your php file add the following function after the options function:

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
}
于 2012-08-03T20:09:06.053 回答
0

这是需要修复的代码片段:<title id="title"><?php echo $result['title']; ?></title>

如果有一个标题,这将起作用,否则取第一个:<title id="title"><?php echo $result['title'][0]; ?></title>

于 2012-08-06T16:49:07.813 回答