php - 在表中显示查询结果

标签 php mysql wordpress

我的数据库中有一个包含 3 列的表 'mags_sold':

日期杂志名称已售数量

每天我都会运行一个 cronjob,插入 3 个新行,其中包含当天的日期、杂志的名称以及当天售出的杂志数量。 (我有 3 本杂志)。

我想在表格中显示数据。

我将其设置为 4 列。 日期mag1mag2mag3

我有一个查询SELECT * FROM 'mags_sold',但我不知道如何循环显示结果,因此日期仅显示一次,并且已售数量显示在每个标题下。

<?php
$values = $wpdb->get_results($wpdb->prepare("SELECT * FROM 'mags_sold'"));
?>
<table id="email_subscription">
<tr>
<th>Date</th>
<th>mag1</th>   
<th>mag2</th>
<th>mag3</th>
</tr>
<?php
foreach($values as $email_signup){
$id = $email_signup->id;
$list_name = array($email_signup->list_name);
$date = $email_signup->date;
echo       '<tr><td>' . $date . '</td>';
$sold_count = array($email_signup->sold_count);
       echo '<td>' . $sold_count[0] . '</td>';
       echo '<td>' . $sold_count[1] . '</td>';  
       echo '<td>' . $sold_count[2] . '</td>';  
 echo    '</tr>';
 }
 ?>   
</table>

但是此代码为每个日期和销售数量显示单独的一行。

var_dump 使用代码:

array (size=3)
  0 => 
    object(stdClass)[637]
      public 'date' => string '2015-05-05' (length=10)
      public 'B' => string '132467' (length=6)
      public 'D' => string '2' (length=1)
      public 'F' => string '15330' (length=5)
  1 => 
    object(stdClass)[549]
      public 'date' => string '2015-05-04' (length=10)
      public 'B' => string '132467' (length=6)
      public 'D' => string '2' (length=1)
      public 'F' => string '15330' (length=5)
  2 => 
    object(stdClass)[547]
      public 'date' => string '2015-05-03' (length=10)
      public 'B' => string '132467' (length=6)
      public 'D' => string '2' (length=1)
      public 'F' => string '15330' (length=5)
array (size=3)
  0 => 
    object(stdClass)[548]
      public 'date' => string '2015-05-05' (length=10)
      public 'B' => string '132462' (length=6)
      public 'D' => string '98577' (length=5)
      public 'F' => string '15343' (length=5)
  1 => 
    object(stdClass)[467]
      public 'date' => string '2015-05-04' (length=10)
      public 'B' => string '132462' (length=6)
      public 'D' => string '98577' (length=5)
      public 'F' => string '15343' (length=5)
  2 => 
    object(stdClass)[468]
      public 'date' => string '2015-05-03' (length=10)
      public 'B' => string '132462' (length=6)
      public 'D' => string '98577' (length=5)
      public 'F' => string '15343' (length=5)
array (size=3)
  0 => 
    object(stdClass)[547]
      public 'date' => string '2015-05-05' (length=10)
      public 'B' => string '132468' (length=6)
      public 'D' => string '6' (length=1)
      public 'F' => string '15349' (length=5)
  1 => 
    object(stdClass)[549]
      public 'date' => string '2015-05-04' (length=10)
      public 'B' => string '132468' (length=6)
      public 'D' => string '6' (length=1)
      public 'F' => string '15349' (length=5)
  2 => 
    object(stdClass)[637]
      public 'date' => string '2015-05-03' (length=10)
      public 'B' => string '132468' (length=6)
      public 'D' => string '6' (length=1)
      public 'F' => string '15349' (length=5)

最佳答案

您需要的是一个 MYSQL 查询,它为每个唯一日期返回以下行:

date         magazine_1      magazine_2      magazine_3
2015-04-05   *number_sold*  *number_sold*    *number_sold*

因此,其中 number_sold 等于已售数量 COUNT,其中 name_of_magazine 等于 magazine_1、2 3 等。

目前,您的查询正在从 mags_sold 表中选择所有内容,然后 foreach 正在执行其应该执行的操作,因为它单独返回每一行。

请参阅下面的 MYSQL fiddle :-

SQL FIDDLE

解决方案

好的,你可以试试这个:-

<?php
$values = $wpdb->get_results($wpdb->prepare("SELECT DISTINCT `date` FROM mags_sold"));
?>
<table id="email_subscription">
<tr>
<th>Date</th>
<th>mag1</th>   
<th>mag2</th>
<th>mag3</th>
</tr>
<?php

    foreach($values as $email_signup){

        $date = $email_signup->date;

        $sold_values = $wpdb->get_results($wpdb->prepare(
        "SELECT DISTINCT `date`,
        (select number_sold from mags_sold where name_of_magazine = 'weekly news' AND date = $date) as 'weekly_news',
        (select number_sold from mags_sold where name_of_magazine = 'monthly news' AND date = $date) as 'monthly_news',
        (select number_sold from mags_sold where name_of_magazine = 'todays news' AND date = $date) as 'todays_news'
        from mags_sold  
        WHERE date = $date "));

        echo '<tr>';
            echo '<td>' . $date . '</td>';
            echo '<td>' . $sold_values->weekly_news. '</td>';
            echo '<td>' . $sold_values->monthly_news . '</td>';  
            echo '<td>' . $sold_values->todays_news . '</td>';  
        echo '</tr>';

    }
 ?>   
</table>

将虚拟杂志名称(即 weekly_news、monthly_news)替换为您的杂志名称,这样就可以了。

关于php - 在表中显示查询结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30027078/

相关文章:

php - 通过单击按钮或链接(javascript 和/或 php?)将服务器端文件设置为输入文件类型的值

javascript - 我搞砸了 JSON 对象、数组和字符串

php - 如何使用标签表和对象表查找 "This value is also used moSTLy with that value"?

php从mysql返回值2次

jquery - 循环内的 WordPress 循环无法正常工作

php - 除了 root 之外,Laravel 路由不起作用

javascript - 单击提交按钮并将数据插入 php 中的数据库表时如何显示隐藏表?

mysql - 远程数据库错误: connect ECONNREFUSED (node-mysql/UBuntu/VirtualBox)

php - 在 WordPress 主页上显示评论

php - WordPress 插件 : a safe way to store mySQL query contitions