I'm trying to build a webpage with simple status posts. I've created foreach loop to query the database and load all the posts:
<?php
$user_id = Check::isLoggedIn();
$dbstories = DB::query('SELECT * FROM stories WHERE user_id=:user_id ORDER BY story_time DESC', array(':user_id'=>$user_id));
$post_story = "";
foreach($dbstories as $story) {
?>
<div>
<article>
<div>
<a href="profile.php">
<?php
// to pull user first name
$user_first = DB::query('SELECT user_first FROM users WHERE user_id=:user_id', array(':user_id'=>$user_id))[0]['user_first'];
echo $user_first;
?>
</a>
<div>
<time>
<?php
// to pull post datetime
$post_time = $story['story_time'];
echo '<time class="timeago" datetime="' . $post_time . '"></time>';
?>
</time>
</div>
</div>
</div>
<?php
// to pull post contents
$post_story = $story['story_body'];
if (strlen($post_story) <= 25) {
echo '<h3>' . $post_story . '</h3>';
} else {
echo '<p>' . $post_story . '</p>';
}
?>
<div>
<div>
<a href="#">
<svg class="thunder-icon"><use xlink:href="icons/icons.svg#thunder-icon"></use></svg>
<span>
<?php
// to pull number of likes
$post_likes = $story['story_likes'];
echo $post_likes;
?>
</span>
</a>
<!-- the same for comments and shares number goes here -->
</div>
</div>
<div>
<!-- This form over here should pull the story_id which is the post ID from the database like the other queries up there and post it next to the url then that will allow me to use it in isset($_GET['story_id']) and trigger a query to increment likes -->
<form action="stories.php?post_id=<?php $post_id = $story['story_id']; echo $post_id; ?>" method="post" id="like_story">
<a href="#" class="btn btn-control" id="post_like">
<svg class="like-post-icon"><use xlink:href="icons/icons.svg#thunder-icon"></use></svg>
</a>
</form>
<!-- the same for comment and share goes here -->
</div>
</article>
</div>
<?php
}
?>
*Please read the comments within the code
Everything works fine but, I still cant get each post's ID linked to the like button (like_story). It only access the most recent post's ID.
Any workaround to reference the post ID (story_id)?
Related
it's my first time modifying a blog from the theme editor (theme: Astra) and I managed to list my articles through filters with javascript (show / hide). However, it is not the correct way to do it. I found that I can do it with meta_query() or tax_query(), but I haven't managed to do it. To make the post list, I did it with WPQuery.
Thanks for your time!
As seen in the image, this is where you should apply the filters, search and by category
`<div class="container_b padding-botomm-lista">
<div id="OdontologiaBlock" class="row_b">
<div class="column100-b">
<?php
$query3 = new WP_Query('cat=7&order=DESC&orderby=date');
if ( $query3->have_posts() ) {
while ( $query3->have_posts() ) {
$query3->the_post();
print "
<div class='column30 margin'>
<div class='col-img'>";
the_post_thumbnail();
print "
</div>
<div class='col-container'>
<span class='catspan'>";
$category = get_the_category();
echo $category[0]->cat_name;
print "
</span>
<h5>
<a class='color-link-post' target='_blank' href='";
the_permalink();
print " '>";
the_title();
print " </a>
</h5>
<span class='datespan'>";
echo get_the_date('d/m/Y');
print "
</span>
</div>
</div>";
}
}
/* Restore original Post Data */
wp_reset_postdata();
?>
</div>
</div>
</div>`
I'm working on helpdesk system.
I created two tables contacts and contacts_comments.
Now I fetch contacts details by contact ID in view page - this working correct.
and now from view page I insert comments to second table contacts_comments.
To contacts_comments I insert message and current contact(ticket) ID.
And now I try display all this comments for current ticket ID.
<?php foreach ($comments as $comment): ?>
<div class="d-flex mb-4">
<div class="flex-shrink-0">
<img src="<?php echo base_url(); ?>assets/c_admin/assets/images/users/avatar-8.jpg" alt="" class="avatar-xs rounded-circle" />
</div>
<div class="flex-grow-1 ms-3">
<h5 class="fs-13"><?php echo $comment->ticket_client; ?> <small class="text-muted"><?php echo $comment->created_at; ?></small></h5>
<p class="text-muted"><?php echo $comment->comments_message; ?></p>
<i class="mdi mdi-reply"></i> Reply
</div>
</div>
<?php endforeach; ?>
But this function display for me all comments from table contacts_comments.
I try build function:
//get comments by id
public function get_comments_by_id($id)
{
$id = clean_number($id);
$this->db->where('ticket_id', $id);
$query = $this->db->get('contacts_comments');
return $query->row();
}
Can anyone help me how to add to this query in CI3 with result somelike:
DB WHERE contacts.id == contacts_comments.ticket_id
i am creating a CMS system. if post is not published then it will not render in html otherwise show in html. So if I fetch a associative array from database "cms" then it has some published and unpublished post. so how can i skip the unpublished posts and show only published posts in html.
i have tried if condition
<!-- First Blog Post -->
<h2 style = "color:green;">
<?php echo $post_title ;?></a>
</h2>
<p class="lead">
by <?php echo $post_author ;?></a>
</p>
<p><span class="glyphicon glyphicon-time"></span><?php $post_date ?></p>
<hr>
<img class="img-responsive" src="images/<?php echo $post_image ;?>" alt="">
<hr>
<p><?php echo $post_content ?></p>
<a class="btn btn-primary" href="#">Read More <span class="glyphicon glyphicon-chevron-right"></span></a>
<hr>
**<?php } ?> **
**<?php } **// <!--loop ends here so that we can fetch 'n' no. of posts and displaying each post like below HTML -->
?>
</div>
if post == published then only show it in html otherwise don't show it.
Check if post is not published at the start of the loop. then use the continue to skip through the rest of the loop.
while(items) {
if(!$post->published) continue;
// this part only runs if post is published
}
I'm creating a blog site and I got stuck in this phase where I want to allow authors to delete their own posts, they can already see the delete button below each of their posts. In the DB table I have an author, content and ID of the article. Now I need to get the specific ID of the article that they want to delete, but I have no idea how to do this, the delete button is the same for every article, so I can't use that for help. I have this code to get articles from my DB and the button click code ready. For better imagination here is an image of the site -
... previous code
<?php }
$articles->ArticlesConn();
$result = mysql_query("SELECT * FROM articles_table");
while($row = mysql_fetch_array($result)){ // loop to generate content
?>
<div class="container"> // responsive stuff
<div class="row"> // responsive stuff
<div class="twelve columns"> // responsive stuff
<?php
echo "<h3>" . $row['Title'] . "</h3>
<p>" . $row['Content'] . "</p>"; ?>
</div>
</div>
<div class="row">
<div class="eight columns">
<address><?php echo "Author - " . $row['Author']; ?> </address><br><br><br> /// nick of the author below each article
</div>
<div class="four columns">
<?php
if ($row['Author'] == $_SESSION["username"]) // show delete button if the logged user is an author of generated article
{ ?>
<input type="button" name="delete" value="Delete article"/>
<?php } ?>
</div>
</div>
</div>
<?php
}
?>
To solve your problem, you should add a form to wrap your input.
This form will call the page itself. In this form there is a hidden input with the value of the article id. So when you submit the form, you can get the article id via $_POST['name of the hidden input'].
I will show you the code :
<?php }
if (isset($_POST['article-id'])) {
// prevent SQL injection
$articleId = mysql_real_escape_string($_POST['article-id']);
$res = mysql_query('DELETE FROM articles_tab WHERE id='.$articleId);
if (!$res) {
die(mysql_error());
}
}
$articles->ArticlesConn();
$result = mysql_query("SELECT * FROM articles_table");
while($row = mysql_fetch_array($result)){ // loop to generate content
?>
<div class="container"> // responsive stuff
<div class="row"> // responsive stuff
<div class="twelve columns"> // responsive stuff
<?php
echo "<h3>" . $row['Title'] . "</h3>
<p>" . $row['Content'] . "</p>"; ?>
</div>
</div>
<div class="row">
<div class="eight columns">
<address><?php echo "Author - " . $row['Author']; ?> </address><br><br><br> /// nick of the author below each article
</div>
<div class="four columns">
<?php
if ($row['Author'] == $_SESSION["username"]) // show delete button if the logged user is an author of generated article
{ ?>
<form method="post">
<input type="hidden" name="article-id" value="<?php echo $row['ID'] ?>"/>
<input type="submit" value="Delete article"/>
</form>
<?php } ?>
</div>
</div>
</div>
<?php
}
But you should use mysqli or PDO instead of the old mysql extension, look at this : http://php.net/manual/en/function.mysql-query.php
I am creating a blog in PHP (trying to keep it close to OOP) and am struggling with getting pagination to work on the page that displays all posts.
The code for the blog.php in question is
<?php
require_once("includes/init.php");
$pagetitle = "Blog";
//include header.
require_once("includes/template/header.php");
// initialise script
$blog = new Blog($db);
$parsedown = new Parsedown();
// load blog posts
$posts = $blog->get_posts();
?>
<div class="container">
<div class="row">
<div class="col-lg-12">
<h1 class="page-header">Blog Home</h1>
</div>
</div>
<div class="row">
<div class="col-lg-8">
<?php foreach ($posts as $post) { ?>
<h1><?php echo $post['title']; ?>
</h1>
<p class="lead">by <?php echo $post['author']; ?>
</p>
<hr>
<p><i class="fa fa-clock-o"></i> Posted on <?php echo date("d/m/Y", strtotime($post['date'])); ?> At <?php echo date("H:i", strtotime($post['date'])); ?> in <?php
$categories = $blog->get_categories($post['id']);
$links = array();
foreach ($categories as $category) {
$links[] = "<a href='blog-categories.php?id=".$category['category_slug']. "'>".$category['category_name']."</a>";
}
echo implode(", ", $links); ?></p>
<hr>
<p><?php $content = $parsedown->parse($post['content']); echo substr($content,0,445) ; ?></p>
<a class="btn btn-primary" href="<?php echo $post['slug']; ?>">Read More <i class="fa fa-angle-right"></i></a>
<a class="btn btn-primary" href="<?php echo $post['slug']; ?>#disqus_thread"><i class="fa fa-angle-right"></i></a>
<hr>
<?php }
?>
</div>
What can I add either to the Blog class or put in a new class to allow blog.php to only show the first 5 results and then give a 'next' link to view the next set of 5? I'm not worried about displaying the total number of pages.
As the class is very large, it can be viewed at http://pastebin.com/qN5ii2Ta.
You are currently using a method get_posts(). You could redefine this method to accept a starting point to begin gathering posts from. With this starting point defined you can LIMIT how many posts are returned in your SQL query.