英文:
showing an article based on date it was published
问题
$current_date = date("Y-m-d");
// Article Query to fetch maximum 5 random articles
$articleQuery = " SELECT category.category_name, category.category_color, article.*
FROM category, article
WHERE article.category_id = category.category_id
AND article.article_date <= {$current_date}
AND article.article_active = 1
ORDER BY RAND() LIMIT 5";
// Running Article Query
$result = mysqli_query($con,$articleQuery);
// Row stores the no of rows in the return data from Query
$row = mysqli_num_rows($result);
// If query has any result (records) => If any articles are present
if($row > 0) {
// Fetching the data of particular record as an Associative Array
while($data = mysqli_fetch_assoc($result)) {
// Storing the article data in variables
$category_color = $data['category_color'];
$category_name = $data['category_name'];
$category_id = $data['category_id'];
$article_id = $data['article_id'];
$article_title = $data['article_title'];
$article_image = $data['article_image'];
$article_desc = $data['article_description'];
$article_date = $data['article_date'];
}
}
你的 SQL 查询看起来没有问题,它正在查询发布日期早于或等于当前日期,并且文章状态为激活的文章。你的代码也检查了结果集是否包含任何文章,如果有的话,将文章数据存储在变量中供后续使用。如果你发现文章未按预期显示,可能原因之一是数据库中的文章数据或日期不正确。请确保数据库中的文章日期和状态正确,并且文章已正确插入数据库。
英文:
<?php
// Fetching all the Navbar Data
require('./includes/nav.inc.php');
$current_date = date("Y-m-d");
error_reporting(0);
?>
<!-- Article List Container -->
<section class="py-1 category-list">
<div class="container">
<h2 class="headings">Artikel
<?php
echo "<h2> $current_date </h2>"
?>
</h2>
<div class="card-container">
<?php
// Article Query to fetch maximum 5 random articles
$articleQuery = " SELECT category.category_name, category.category_color, article.*
FROM category, article
WHERE article.category_id = category.category_id
AND article.article_date <= {$current_date}
AND article.article_active = 1
ORDER BY RAND() LIMIT 5";
// Running Article Query
$result = mysqli_query($con,$articleQuery);
// Row stores the no of rows in the return data from Query
$row = mysqli_num_rows($result);
// If query has any result (records) => If any articles are present
if($row > 0) {
// Fetching the data of particular record as an Associative Array
while($data = mysqli_fetch_assoc($result)) {
// Storing the article data in variables
$category_color = $data['category_color'];
$category_name = $data['category_name'];
$category_id = $data['category_id'];
$article_id = $data['article_id'];
$article_title = $data['article_title'];
$article_image = $data['article_image'];
$article_desc = $data['article_description'];
$article_date = $data['article_date'];
i want my news portal to only appear for a given date and not show up before that date. like a scheduled article. so he can display past or current news. in this screenshot, that article was supposed to be published on august 3rd 2023.
答案1
得分: 0
日期是一个文本字段而不是数值字段,所以需要加引号... <= \"{$current_date}\"
这会获取当前日期之前或当天的所有帖子。不确定这是否是您的意图。
英文:
date is a textual rather than a numeric field so needs to be quoted ... <= \"{$current_date}\"
This gets all posts before or on the current date. Not sure if this was your intention.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论