2015-05-04 167 views
0

我有3列的數據庫表,'mags_sold'顯示結果

datename of magazinenumber sold

我每天都會運行一個cronjob,在那天插入3行新的日期,這個雜誌的名字和當天銷售了多少。 (我有3本雜誌)。

我想在表格中顯示數據。

我已經設置了4列。 Datemag1,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

我不明白。 'mags_sold'是一個字符串 – Strawberry

+0

你可以在foreach語句下面做一個'$ email_signup'的print_r併發布它嗎? – aphextwix

回答

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等

當前您的查詢選擇全部從mags_sold表中,然後foreach正在做它應該在它正在返回每一行分開。

見下MYSQL小提琴: -

SQL FIDDLE

SOLUTION

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> 

更換假雜誌的名稱,即weekly_news, monthly_news與您的雜誌的名稱,並應該工作。

+0

謝謝。它給了我正確的數組,但3個相同的數組。還有。 $ sold_values-> todays_news。 '';給我一個白屏。我如何迴應結果。 – LTech

+0

@LTech - 您需要用您自己的雜誌名稱來替換'weekly_news,monthly_news,今天的新聞'。這只是爲了說明的目的。請在'foreach'循環中爲我提供'$ sold_values'數組的'var_dump'。 – aphextwix

+0

我完全使用了你的代碼(顯然使得這些更改對應於我的表格)。 var_dump在上面的問題中。當我回應'​​'時沒有任何顯示。 $計數 - > B。'';我認爲foreach循環導致顯示數據3次,但是如果我把查詢放在循環回聲'​​'之外。 $ counts-> Breaking_News_Videos。 '';顯示相同的值3倍 – LTech