我在数据库中有3个列的表'mags_sold'
:
date
,name of magazine
,number sold
。
每天我都会运行一条cronjob,其中插入3条新线,当天就是杂志的名称和那天销售的数量。 (我有3本杂志)。
我想在表格中显示数据。
我已经设置了4列。 Date
和mag1
,mag2
,mag3
。
我有一个查询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)
答案 0 :(得分:1)
您需要的是一个MYSQL查询,它为每个唯一日期返回以下行:
date magazine_1 magazine_2 magazine_3
2015-04-05 *number_sold* *number_sold* *number_sold*
所以 number_sold 等于name_of_magazine
等于magazine_1,2 3等的销售数量的COUNT。
当你的查询从mags_sold
表中选择所有内容时,foreach正在完全按照它应该单独返回每一行的方式。
见下面的MYSQL小提琴: -
<强>解强>
好的,你可以试试这个: -
<?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
替换为杂志的名称,这应该有效。