【问题标题】:Redirecting to a dynamic page重定向到动态页面
【发布时间】:2013-11-04 06:50:37
【问题描述】:

我有一个显示博客文章的页面 [latest_posts.php] 和另一个显示单个博客文章的页面 [blog.php] 。我打算在 latest_posts.php 中链接图像标题,以便它重定向到 blog.php,它会显示被点击的特定帖子。

latest_posts.php ---->

    <div class="main blog">

        <!-- Header -->
        <h2 class="underline">
            <span>What&#039;s new</span>
            <span></span>
        </h2>
        <!-- /Header -->

        <!-- Posts list --> 
        <ul class="post-list post-list-1">


                <?php

                /* Fetches Date/Time, Post Content and title */

                include 'dbconnect.php';

                $sql = "SELECT * FROM wp_posts";
                $res = mysql_query($sql);


                while ( $row = mysql_fetch_array($res) ) {
                ?>  


            <!-- Post #1 -->
            <li class="clear-fix">

                <!-- Date -->
                <div class="post-list-date">
                    <div class="post-date-box">


                        <?php 
                            //Timestamp broken down to show accordingly
                            $timestamp = $row['post_date'];
                            $datetime = new DateTime($timestamp);
                            $date = $datetime->format("d");
                            $month = $datetime->format("M");
                    ?> 
                            <h3>    <?php echo $date; ?>    </h3>
                            <span>  <?php echo $month; ?>   </span>

                    </div>
                </div>
                <!-- /Date -->

                <!-- Image + comments count -->
                <div class="post-list-image">
                    <!-- Image -->
                    <div class="image image-overlay-url image-fancybox-url">
                        <a href="post.php" class="preloader-image">
                            <?php 
                                                echo '<img src="', $row['image'], '" alt="' , $row['post_title'] , '\'s Blog Image" />'; 
                            ?>
                        </a>
                    </div>
                    <!-- /Image -->
                </div>
                <!-- /Image + comments count -->

                <!-- Content -->
                <div class="post-list-content">
                    <div>
                        <!-- Header -->
                        <h4> <a href="post.php? . $row['ID'] . "> <?php echo $row['post_title']; ?> </a> </h4>



                        <!-- /Header -->

                        <!-- Excerpt -->
                        <p>
                            <?php echo $row ['post_content'];  }?>
                        </p>
                        <!-- /Excerpt -->

                    </div>

                </div>
                <!-- /Content -->

            </li>
            <!-- /Post #1 -->

        </ul> 
        <!-- /Posts list -->


        <a href="blog.php" class="button-browse">Browse All Posts</a>

    </div>

    <?php require_once('include/twitter_user_timeline.php'); ?>

blog.php --->

    <?php require_once('include/header.php'); ?>

    <body class="blog">

        <?php require_once('include/navigation_bar_blog.php'); ?>

        <div class="blog">

            <div class="main">

                <!-- Header -->
                <h2 class="underline">
                    <span>What&#039;s new</span>
                    <span></span>
                </h2>
                <!-- /Header -->

                <!-- Layout 66x33 -->
                <div class="layout-p-66x33 clear-fix">

                    <!-- Left column -->
                    <!-- <div class="column-left"> -->

                        <!-- Posts list -->
                        <ul class="post-list post-list-2">

                        <?php

                        /* Fetches Date/Time, Post Content and title with Pagination */

                        include 'dbconnect.php';

                        //sets to default page
                        if(empty($_GET['pn'])){
                            $page=1;
                        } else {
                            $page = $_GET['pn'];

                        }
                        // Index of the page
                        $index = ($page-1)*3;

                        $sql = "SELECT * FROM `wp_posts` ORDER BY `post_date` DESC LIMIT " . $index . " ,3";
                        $res = mysql_query($sql);

                        //Loops through the values
                        while ( $row = mysql_fetch_array($res) ) {
                        ?>




                            <!-- Post #1 -->
                            <li class="clear-fix">
                                <!-- Date -->
                                <div class="post-list-date">
                                    <div class="post-date-box">

                                    <?php 
                                            //Timestamp broken down to show accordingly
                                            $timestamp = $row['post_date'];
                                            $datetime = new DateTime($timestamp);
                                            $date = $datetime->format("d");
                                            $month = $datetime->format("M");
                                    ?> 
                                            <h3>    <?php echo $date; ?>    </h3>
                                            <span>  <?php echo $month; ?>   </span>

                                    </div>
                                </div>
                                <!-- /Date -->

                                <!-- Image + comments count -->
                                <div class="post-list-image">

                                    <!-- Image -->
                                    <div class="image image-overlay-url image-fancybox-url">
                                        <a href="post.php" class="preloader-image">
                                        <?php echo '<img src="', $row['image'], '" alt="' , $row['post_title'] , '\'s Blog Image" />'; ?>
                                        </a>
                                    </div>
                                    <!-- /Image -->
                                </div>
                                <!-- /Image + comments count -->
                                <!-- Content -->
                                <div class="post-list-content">
                                    <div>

                                        <!-- Header -->
                                        <h4> <a href="post.php"> <?php echo $row['post_title']; ?> </a> </h4>
                                        <!-- /Header -->
                                        <!-- Excerpt -->
                                        <p> <?php echo $row ['post_content'];  ?> </p>
                                        <!-- /Excerpt -->
                                    </div>
                                </div>
                                <!-- /Content -->
                            </li>
                            <!-- /Post #1 -->



                        <?php } // close while loop ?>

                        </ul>
                        <!-- /Posts list -->



                        <div><!-- Pagination -->

                            <ul class="blog-pagination clear-fix">

                                <?php 

                                //Count the number of rows
                                $numberofrows = mysql_query("SELECT COUNT(ID) FROM `wp_posts`");
                                //Do ciel() to round the result according to number of posts
                                $postsperpage = 4;
                                $numOfPages = ceil($numberofrows / $postsperpage); 


                                for($i=1; $i < $numOfPages; $i++) { 

                                    //echos links for each page
                                    $paginationDisplay = '<li><a href="blog.php?pn=' . $i . '">' . $i . '</a></li>';
                                    echo $paginationDisplay;


                                }



                                ?>
                                <!--
                                <li><a href="#" class="selected">1</a></li>
                                <li><a href="#">2</a></li>
                                <li><a href="#">3</a></li>
                                <li><a href="#">4</a></li>
                                -->                     

                            </ul>
                        </div><!-- /Pagination -->

                    <!-- /div> -->
                    <!-- Left column -->


                </div>
                <!-- /Layout 66x33 -->

            </div>

        </div>

        <?php require_once('include/twitter_user_timeline.php'); ?>
        <?php require_once('include/footer_blog.php'); ?>

提前致谢。

【问题讨论】:

  • 到目前为止你做了什么?
  • 你能告诉我们你的代码吗?
  • 很想看看你在这个问题中有标签的作品
  • 哈哈!!没看过代码,有2个答案!!!

标签: php mysql html phpmyadmin


【解决方案1】:

没有看到您问题的代码,我估计逻辑是这样的......

在你的 latest_posts.php 上你会做这样的事情(假设你有一个像 $post 这样包含 ID 和标题的变量,或者类似的模式):

<a href="blog.php?id=<?php echo $post["id"]; ?>"><?php echo $post["title"]; ?></a>

然后在 blog.php 上,您需要从 URL 中获取 ID 并以某种方式查找它..

<?php
$id = $_GET["id"];

$post = lookup_post_somehow($id);

if($post) {
  // render post
} else {
  // 404, blog post not found..
}
?>

【讨论】:

  • 谢谢!不过,您的答案具有相同的目的:)如果我在写我的之前看到了您的答案,我就不会发布了。
  • 我将不胜感激更详细的解决方案
  • 看起来您已经链接到 post.php 并且帖子的 id 为:"

    "。您当前的 blog.php 看起来显示了多个帖子,是这样吗?
【解决方案2】:

您需要在谷歌上搜索教程。 :) 周围有一些很棒的东西。

希望您的博客在数据库中。这些数据库记录都应该有一个唯一的 ID。

在您的 latest_blogs 页面上添加 ?id=(只是一个示例)以在单击链接时传递 idover。

现在,在您的博客页面上,php 变量 $_GET['id'] 将包含博客 ID。

希望能有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-04
    • 1970-01-01
    • 2012-10-11
    • 1970-01-01
    • 2010-12-27
    • 2013-12-12
    相关资源
    最近更新 更多