如何在点击“阅读更多”时显示数据库的完整内容'按键

时间:2016-03-14 06:33:46

标签: php html css

我创建了一个名为 Magazine 的数据库,它有一个表 social_media 。该表有5列,即 ID,标题,作者,内容简短,内容 内容 列包含整篇文章的较短版本(将在以下代码中显示)并且 内容 列包含整篇文章(单击“阅读更多”按钮时将显示该文章)。我创建了一个主页,其中 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()

如何解决此问题?

2 个答案:

答案 0 :(得分: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 ");

html

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

答案 1 :(得分:0)

更改以下内容

var result = currentData.filter(function(d, yr){
                      return $("." + d.state.replace(/\s|\(|\)|\'|\,+/g, '')).attr("fill") != "#cccccc"
                    })

致:

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

现在您需要按ID获取详细信息并相应地显示数据。