显示根据发布日期的文章。

huangapple go评论56阅读模式
英文:

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 查询看起来没有问题,它正在查询发布日期早于或等于当前日期,并且文章状态为激活的文章。你的代码也检查了结果集是否包含任何文章,如果有的话,将文章数据存储在变量中供后续使用。如果你发现文章未按预期显示,可能原因之一是数据库中的文章数据或日期不正确。请确保数据库中的文章日期和状态正确,并且文章已正确插入数据库。

英文:
&lt;?php
  // Fetching all the Navbar Data
  require(&#39;./includes/nav.inc.php&#39;);

  $current_date = date(&quot;Y-m-d&quot;);
  error_reporting(0);
?&gt;

&lt;!-- Article List Container --&gt;
&lt;section class=&quot;py-1 category-list&quot;&gt;
  &lt;div class=&quot;container&quot;&gt;
    &lt;h2 class=&quot;headings&quot;&gt;Artikel
      &lt;?php
      echo &quot;&lt;h2&gt; $current_date &lt;/h2&gt;&quot;
      ?&gt;
    &lt;/h2&gt;
    &lt;div class=&quot;card-container&quot;&gt;
      &lt;?php

        // Article Query to fetch maximum 5 random articles
        $articleQuery = &quot; SELECT category.category_name, category.category_color, article.*
                          FROM category, article
                          WHERE article.category_id = category.category_id
                          AND article.article_date &lt;= {$current_date}
                          AND article.article_active = 1
                          ORDER BY RAND() LIMIT 5&quot;;
        // 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) =&gt; If any articles are present
        if($row &gt; 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[&#39;category_color&#39;];
            $category_name = $data[&#39;category_name&#39;];
            $category_id = $data[&#39;category_id&#39;];
            $article_id = $data[&#39;article_id&#39;];
            $article_title = $data[&#39;article_title&#39;];
            $article_image = $data[&#39;article_image&#39;];
            $article_desc = $data[&#39;article_description&#39;];
            $article_date = $data[&#39;article_date&#39;];

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.

is there a mistake with my sql query? any suggestion? 显示根据发布日期的文章。

答案1

得分: 0

日期是一个文本字段而不是数值字段,所以需要加引号... &lt;= \&quot;{$current_date}\&quot;

这会获取当前日期之前或当天的所有帖子。不确定这是否是您的意图。

英文:

date is a textual rather than a numeric field so needs to be quoted ... &lt;= \&quot;{$current_date}\&quot;

This gets all posts before or on the current date. Not sure if this was your intention.

huangapple
  • 本文由 发表于 2023年7月3日 23:21:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76606128.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定