0

I have created a database named Magazine and it has a table social_media. The table has 5 columns namely ID, Headline, Author, Content Short, Content. The Content Short column contains the shorter version of the entire article(which will be displayed in the following code) and the Content column contains the entire article(which will be displayed on clicking the Read More button). I have created a homepage where all the articles from the social_media are displayed in the Content_Left div(NOTE: The Content Short column is displayed and not Content). At the end of each entry from the database there is a 'Read More' button. On clicking on that read more button I want the user to be redirected to a new page 'article.php' where the Headline, Author and Content of the corresponding article will be displayed(The Longer version of the content and not the Content Short). How do I do it? I have created the following webpage:

<?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>

And here is the CSS file(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;
}

And here is the CSS file(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;
}

I want to display that article in the new page but with the same background theme. Or is there any other way to display the entire article? I hope this much detail is enough. In case any more information is needed please ask.

EDIT: As per the suggestions of the fellow members I have edited the code. My article.php file now looks like this:

<?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>

However, I am getting the following error:

Fatal error: Call to a member function fetch_assoc() on a non-object

How do I resolve this problem?

HoldOffHunger
  • 18,769
  • 10
  • 104
  • 133

2 Answers2

1

Change few thing

in php file

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

to

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

then in your 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 ");

Follow same html code of your article listing page on article.php

urfusion
  • 5,528
  • 5
  • 50
  • 87
  • I did as you said but it's giving the following error: `Fatal error: Call to a member function fetch_assoc() on a non-object` – Bitan Basak Mar 14 '16 at 13:14
  • have you include `` in your `article.php` – urfusion Mar 14 '16 at 13:18
  • Yes, I have done that. I have posted the updated code and the article.php code in the above question. Please check and help me out. – Bitan Basak Mar 14 '16 at 13:24
  • http://stackoverflow.com/questions/5121027/fatal-error-call-to-a-member-function-fetch-assoc-on-a-non-object – urfusion Mar 14 '16 at 13:28
  • On following the answers to the link you provided I am getting the following error: `Fatal error: Using $this when not in object context` – Bitan Basak Mar 14 '16 at 13:39
  • I noticed something. On clicking on the 'Read More' button the URL address changes to `http://localhost/Magazine/article.php?articleId=`. Now If I am not wrong there should be the ID of the article at the end of the URL(Example: http://localhost/Magazine/article.php?articleId=1). But in my case the ID is not appearing in the URL. – Bitan Basak Mar 14 '16 at 13:45
  • check the updated code `` – urfusion Mar 14 '16 at 14:06
  • Yes! It did the trick! Works perfectly now! So, what was wrong with the earlier code? – Bitan Basak Mar 14 '16 at 14:11
  • just `'` misplaced. :D – urfusion Mar 14 '16 at 14:12
0

Change your below line

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

To:

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

Now you've to get details by ID and display data accordingly.

Ravindra Bhalothia
  • 1,720
  • 2
  • 13
  • 16