简体   繁体   English

在表中显示查询结果

[英]Display results of query in a table

I have table, 'mags_sold' in the database with 3 columns: 我在数据库中有3列的表'mags_sold'

date , name of magazine , number sold . datename of magazinenumber sold

Every day I run a cronjob that inserts 3 new lines with that days date the name of the magazine and how many were sold that day. 每天,我都会执行cronjob,并在该日期插入3行新内容,以注明杂志的名称以及当天的销售量。 (I have 3 magazines). (我有3本杂志)。

I would like to display the data in a table. 我想在表格中显示数据。

I've set it up with 4 columns. 我将其设置为4列。 Date and mag1 , mag2 , mag3 . Datemag1mag2mag3

I have a query SELECT * FROM 'mags_sold' but I don't know how to loop through the results to display them so the date only displays once, and the number sold shows under each heading. 我有一个查询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>

But this code shows a separate line for each date and sold count. 但是此代码为每个日期和销售数量显示了单独的一行。

var_dump using code: 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)

What you need is an MYSQL query which returns for each unique date the following line : 您需要一个MYSQL查询,该查询针对每个唯一日期返回以下行:

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

So where number_sold is equal to the COUNT of number sold where name_of_magazine is equal to magazine_1, 2 3 etc. 因此,如果number_sold等于已售出的数量COUNT,其中name_of_magazine等于magazine_1、2 3等。

At the moment your query is selecting all from the mags_sold table and then the foreach is doing exactly what it should in that it's returning each line seperately. 目前,您的查询是从mags_sold表中选择所有mags_sold ,然后foreach正是在做它应该做的事情,因为它分别返回了每一行。

See the below MYSQL fiddle : - 请参见下面的MYSQL小提琴:-

SQL FIDDLE SQL字段

SOLUTION

OK so you could try this : - OK,所以您可以尝试:-

<?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>

Replace the dummy magazine names, ie weekly_news, monthly_news with the name of your magazines and that should work. 用您的杂志名称替换虚拟杂志名称, weekly_news, monthly_news这样应该可以。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM