我有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)
我不明白。 'mags_sold'是一個字符串 – Strawberry
你可以在foreach語句下面做一個'$ email_signup'的print_r併發布它嗎? – aphextwix