【问题标题】:How to display the full content of a database on clicking on 'Read More' button如何在单击“阅读更多”按钮时显示数据库的全部内容
【发布时间】:2016-03-14 06:33:46
【问题描述】:

我创建了一个名为 Magazine 的数据库,它有一个表 social_media。该表有 5 列,分别是 ID、标题、作者、内容简短、内容。 Content Short 列包含整篇文章的较短版本(将在以下代码中显示) Content 列包含整篇文章(点击阅读更多按钮后将显示)。我创建了一个主页,其中 social_media 中的所有文章都显示在 Content_Left div(注意: 显示内容短列而不是内容)。在来自数据库的每个条目的末尾都有一个“阅读更多”按钮。单击该阅读更多按钮时,我希望用户被重定向到新页面“article.php”,其中相应文章的标题、作者和内容将位于显示(内容的较长版本,而不是内容短)。我该怎么做?我创建了以下网页:

<?php require_once('connections/connection.php'); ?>

<?php

session_start();

$stmt = $con->query("SELECT * FROM social_media");

?>

<!doctype html>
<html>
<head>
<link href="CSS/Layout.css" rel="stylesheet" type="text/css" />
<link href="CSS/menu.css" rel="stylesheet" type="text/css" />
<style type="text/css">
</style>
<meta charset="utf-8">
<title>Home</title>
</head>

<body style="background-color:#E0DDDD">
<div id="Container">
<div id="Header"></div>
<div id="NavBar">
<nav>
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">Social Media</a></li>
<li><a href="#">Tech</a></li>
<li><a href="#">Tips &amp; Tricks</a></li>
</ul>
</nav>
</div>
<div id="Content">
<div id="Content_Left">
  <h1><center>Social Media</center></h1>
  <table>
   <?php
   while($records=$stmt->fetch_assoc()) {
    $_SESSION["ID"] = $records['ID'];
    echo "<tr>";
    echo "<th><h3>".$records['Headline']."</h3></th>";
    echo "</tr>";
    echo "<tr>";
    echo "<td>By <strong>".$records['Author']."</strong></td>";
    echo "</tr>";
    echo "<tr>";
    echo "<td>".$records['Content_short']."  <a href='article.php?articleId=' " . $records['ID']. ">Read More...</a></td>";
    echo "</tr>";
   }
   ?>
  </table>
 </div>

 <div id="Content_Center">
  <h1>Header Here </h1>
  <p>Text Goes Here!</p>
 </div>

 <div id="Content_Right">
  <h1>Header Here </h1>
  <p>Text Goes Here!</p>
 </div>
 </div>
<div id="Footer">
<center>Your Copyright Message</center>
</div>
</div>
</body>
</html>

这里是 CSS 文件(Layout.css):

body{
    margin:0;
    padding: 0;
}
#Container {
    width: 980px;
    height:auto;
    margin-left:auto;
    margin-right:auto;
    margin-top:20px;
    margin-bottom:21px;
}
#Header {
    height:120px;
    background-image:url(../Assets/Untitled-1.png);
    background-repeat:no-repeat;
    margin-bottom:21px;
}
#NavBar {
    height:60px;
    background-color:#000000;
}

#Content {  
    background-color:#FFFFFF;
    margin-top: 20px;
    padding: 5px;
    overflow:hidden;
}

#Content_Left {
    height: auto;
    width: 210px;
    padding:5px;
    float:left;
    background-color: lightblue
}

#Content_Center {
    height: auto;
    padding:5px;
    width: 500px;
    float:left;
    margin-left: 10px;
    margin-right: 10px;
    background-color: lightblue
}

#Content_Right {
    height: auto;
    padding:5px;
    width: 210px;
    float: right;
    background-color: lightblue
} 

#Container #Content h1 {
    font-family: "Gill Sans", "Gill Sans MT", "Myriad Pro", "DejaVu Sans Condensed", Helvetica, Arial, sans-serif;
    font-style: normal;
    font-variant: normal;
    font-weight: bolder;
    text-shadow: 0px 0px;
}

#Footer {
    padding-top: 10px;
    height: 100px;
}

这里是 CSS 文件(menu.css):

nav ul {
    margin:0;
    padding:0;
}
nav ul li {
    list-style-type:none;
    display:block;
    width:150px;
    height:60px;
    float:left;
    text-align:center;
    line-height:55px;
    font-family:Baskerville, "Palatino Linotype", Palatino, "Century Schoolbook L", "Times New Roman", serif;
    font-size:17px;
}
nav ul li a {
    text-decoration:none;
    color:#FFF;
}
nav ul li:hover {
    background-color:#BBB5B5;
}
nav ul li:hover a {
    display:block;
    color:Black;
}

我想在新页面中显示该文章,但背景主题相同。或者有没有其他方法可以显示整篇文章?我希望这么多细节就足够了。如果需要更多信息,请询问。

编辑:根据其他成员的建议,我编辑了代码。我的 article.php 文件现在看起来像这样:

<?php require_once('connections/connection.php');?>

<?php
$articleId = $_GET['articleId'];

// you need to fetch only one record this time for showing only the article that wanted to be read. so use `where` condition
$stmt = $con->query("SELECT * FROM social_media WHERE id = $articleId ");
?>

<!doctype html>
<html>
<head>
<link href="CSS/Layout.css" rel="stylesheet" type="text/css" />
<link href="CSS/menu.css" rel="stylesheet" type="text/css" />
<style type="text/css">
</style>
<meta charset="utf-8">
<title>Home</title>
</head>

<body style="background-color:#E0DDDD">
<div id="Container">
<div id="Header"></div>
<div id="NavBar">
<nav>
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">Social Media</a></li>
<li><a href="#">Tech</a></li>
<li><a href="#">Tips &amp; Tricks</a></li>
</ul>
</nav>
</div>
<div id="Content">
<div id="Content_Full">
  <table>
   <?php
   while($records=$stmt->fetch_assoc()) {
    echo "<tr>";
    echo "<th><h1>".$records['Headline']."</h1></th>";
    echo "</tr>";
    echo "<tr>";
    echo "<td>By <strong>".$records['Author']."</strong></td>";
    echo "</tr>";
    echo "<tr>";
    echo "<td>".$records['Content']."</td>";
    echo "</tr>";
   }
   ?>
  </table>
 </div>
 </div>
<div id="Footer">
<center>Your Copyright Message</center>
</div>
</div>
</body>
</html>

但是,我收到以下错误:

致命错误:在非对象上调用成员函数 fetch_assoc()

我该如何解决这个问题?

【问题讨论】:

    标签: php html css


    【解决方案1】:

    改变一些东西

    在php文件中

    echo "<td>".$records['Content_short']."  <a href='article.php'>Read More...</a></td>";
    

    到

    echo "<td>".$records['Content_short']."  <a href='article.php?articleId= " . $records['ID']. "'>Read More...</a></td>";
    

    然后在你的 article.php

    <?php
    if(isset($_GET['articleId'])){
        $articleId = $_GET['articleId'];
    }else{
        echo "Invalid access"; die;
    }
    
    // you need to fetch only one record this time for showing only the article that wanted to be read. so use `where` condition
    $stmt = $con->query("SELECT * FROM social_media WHERE id = $articleId ");
    

    在article.php上关注您的文章列表页面的相同html代码

    【讨论】:

    • 我按照你说的做了,但它给出了以下错误:Fatal error: Call to a member function fetch_assoc() on a non-object
    • 您是否将&lt;?php require_once('connections/connection.php'); ?&gt; 包含在您的article.php 中
    • 是的,我已经做到了。我已经在上述问题中发布了更新的代码和 article.php 代码。请检查并帮助我。
    • 按照您提供的链接的答案,我收到以下错误:Fatal error: Using $this when not in object context
    【解决方案2】:

    改变你的下面一行

    echo "<td>".$records['Content_short']."  <a href='article.php'>Read More...</a></td>";
    

    收件人:

    echo "<td>".$records['Content_short']."  <a href='article.php?id=' " . $records['ID']. ">Read More...</a></td>";
    

    现在您必须通过 ID 获取详细信息并相应地显示数据。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-23
      • 2021-08-22
      • 1970-01-01
      • 1970-01-01
      • 2019-01-28
      • 1970-01-01
      相关资源
      最近更新 更多