I have been busy with a blog system and i am currently trying to output the sql data order by id desc into different divs (cols) like the latest 3 blogs, the other older blogs has to be on the page order by id desc below. I hope someone can help me.
<?php
$postRow = $db->query("SELECT postID, postTitle, postDesc, postDate, imgBackground, imgNewspost FROM blog_posts WHERE postID = '$postID'");
?>
First code, connection to the database is OK. The first 3 cols at the top of the page are like these:
<a href="index.html" class="collink">
<div class="sub-col col">
<?php
try {
$stmt = $db->query('SELECT postID, postTitle, postDesc, postDate, imgBackground, imgNewspost FROM blog_posts WHERE postID ="7" ');
while($row = $stmt->fetch()){
echo "<img class='imgtopnews'src='".$row['imgNewspost']."'>";
echo '<div>';
echo '<h1><a href="viewpost.php?id='.$row['postID'].'">'.$row['postTitle'].'</a></h1>';
echo '<p>Gepost op '.date('jS M Y H:i:s', strtotime($row['postDate'])).'</p>';
echo '<p>'.$row['postDesc'].'</p>';
echo '<p><a href="viewpost.php?id='.$row['postID'].'">Lees verder</a></p>';
echo '</div>';
}
} catch(PDOException $e) {
echo $e->getMessage();
}
?>
</div>
</a>
Last but not least all older blogs are at the page below the 3 cols:
<?php
try {
$stmt = $db->query('SELECT postID, postTitle, postDesc, postDate, imgBackground, imgNewspost FROM blog_posts ORDER BY postID DESC');
while($row = $stmt->fetch()){
echo '<div class="nieuwscol">';
echo '<div class="newstitle"><p><a href="viewpost.php?id='.$row['postID'].'">'.$row['postTitle'].'</a></p></div>';
echo '<div class="newsdate"><p>Gepost op '.date('jS M Y H:i:s', strtotime($row['postDate'])).'</p></div>';
echo "<img class='imgnews'src='".$row['imgNewspost']."'>";
echo '<p>'.$row['postDesc'].'</p>';
echo '<p><a href="viewpost.php?id='.$row['postID'].'">Lees verder</a></p>';
echo '</div>';
}
} catch(PDOException $e) {
echo $e->getMessage();
}
?>
So here's how i'd do what you're trying to do:
I'd firstly set up a class file for blog ie (class.blog.php)
You need to extend the database connect class which I have branded DBClass currently for demo purposes. You'll also need to require_once()
the file of the database connection file.
Here is the blog class:
class blog extends DBClass
{
public function __construct()
{
parent::__contruct();
}
public function getBlog()
{
$stmt = "SELECT count(*) FROM `blog_posts`";
$res = $this->prepare($stmt);
$res->execute();
$count = $res->fetchColumn();
if ($count > 0) {
$stmt = "SELECT * FROM `blog_posts` ORDER BY `postID` DESC";
$res = $this->prepare($stmt);
$res->execute();
while ($row = $res->fetch(PDO::FETCH_OBJ)) {
$rows[] = $row;
}
return $rows;
}
}
}
Then within the blog file you'd need to do the following (my demo purpose here) I'm sure you'll be able to adjust accordingly to suit:
require_once('class.blog.php');
$blog = new blog();
$blogInfo = $blog->getBlog();
foreach ($blogInfo as $row)
{
echo
"<table>
<tr>
<td>
$row->imgNewpost
</td>
</tr>
<tr>
<td>
<h1>
<a href='viewpost.php?id=$row->postID'>$row->postTitle</a>
</h1>
</td>
</tr>
</table>";
}
If you want these as a <div>
just remove the table and echo out the divs instead.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.