-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に基づいて要素内でループしますが、問題は、タイトルを有効にするためにページを2回リロードする必要があることです。

4

2 に答える 2

0

$ title変数にはpost関数内でのみアクセスできるため、ブログのタイトルを配列に保存しようとしても機能しません。

$title配列をpostreturnすることができます...

//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を出力する関数をクラスにpostします。

2)正しいページタイトルを返すタイトル関数を作成し、そのように呼び出します。

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

アップデート:

phpファイルで、options関数の後に次の関数を追加します。

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>

これは、タイトルが1つある場合は機能します。それ以外の場合は、最初のタイトルを使用します。<title id="title"><?php echo $result['title'][0]; ?></title>

于 2012-08-06T16:49:07.813 に答える