2016-11-20 33 views
0

我正在嘗試計算訪問者日期,以瞭解每月有多少次訪問&將結果發佈到jQuery圖表中。使用php/mysql按月訪問訪問者

好,所以下面的代碼「有效」,但它以某種方式計算一切* 2.如果有一個訪問者這個月它會輸出2,爲什麼?

我也想知道如何讓這個代碼更小? $ jan $ feb等似乎不是這樣做的方式,但我是一個初學者,所以我不知道如何使這個代碼更小。有人可以幫助我嗎?

數據庫:

date 
2016-11-17 16:36:12 

PHP的:

$jan = ''; $feb = ''; $maa = ''; $apr = ''; $mei = ''; $jun = ''; $jul = ''; $aug = ''; $sep = ''; $okt = ''; $nov = ''; $dec = ''; 

    foreach($dates as $date){      
      $month = date_parse_from_format("Y-m-d H:i:s", $date->date); 
      if($month ["month"] == '01'){$jan .= $month ["month"];} 
      if($month ["month"] == '02'){$feb .= $month ["month"];} 
      if($month ["month"] == '03'){$maa .= $month ["month"];} 
      if($month ["month"] == '04'){$apr .= $month ["month"];} 
      if($maand["month"] == '05'){$mei .= $month ["month"];} 
      if($maand["month"] == '06'){$jun .= $month ["month"];} 
      if($maand["month"] == '07'){$jul .= $month ["month"];} 
      if($maand["month"] == '08'){$aug .= $month ["month"];} 
      if($maand["month"] == '08'){$sep .= $month ["month"];} 
      if($maand["month "] == '10'){$okt .= $month ["month"];} 
      if($maand["month "] == '11'){$nov .= $month ["month"];} 
      if($maand["month "] == '12'){$dec .= $month ["month"];} 
    } 
+0

你從數據庫中獲取這些數據嗎? –

+0

是的,來自mysql數據庫。 –

回答

0

試試這個:

$arr = array() 
foreach($dates as $date){ 
    $month = date('m',strtotime($date->date)); 
    $arr[$month][] = $date; 
} 

下面這段代碼創建一個數組的花費每月鍵,該月的附加數據值。

+0

你好,Parth,我如何迴應?我試圖回聲$日期,但它給了我錯誤,也許我不應該回聲$日期。請幫忙。 –

+0

嘗試print_r($ date),你不能回顯數組,要麼使用var_dump()或print_r() –

0
$visitsPerMonth = []; 
foreach ($dates as $date) { 
    $month = date('m', strtotime($date->date)); 
    $visitsPerMonth[$month]++; 
} 

如果$日期是源自SQL數據庫,而是使用GROUP BY查詢。

如果您的jQuery圖表需要JSON輸入,請在$ visitsPerMonth上使用json_encode()。

+0

thx我要試試這個! –

0

按你的要求,如果你是存儲在數據庫中訪問數據,那麼你可以從數據庫查詢的次數爲每月這樣

SELECT COUNT(*) FROM visitors YEAR(visited_date) = '2016' GROUP BY MONTH(visited_date) 

如果這個月它會輸出2 1位訪客, 這是爲什麼?

這是因爲您要連接字符串並計算字符串的長度。這個月有2個字符,所以每次迭代時它都會連接每個迭代2個字符。見例如波紋管

案例1:$數據有1個日期2016年11月17日16時36分十二秒

$nov = ''; // at start 
$nov .= $month ["month"]; // date 2016-11-17 16:36:12 
=> $nov .= '11'; //shorthand concatenation php 
=> $nov = $nov . '11'; 
=> $nov = '' . '11'; // as $nov = '' at start 

count($nov); // $nov have value '11' which has 2 character so output will be 2 

案例2:$數據有2個日期2016年11月17日16 :36:12 2016年11月18日16時36分十二秒

$nov = ''; // at start 
$nov .= $month ["month"]; // date 2016-11-17 16:36:12 
=> $nov .= '11'; //shorthand concatenation php 
=> $nov = $nov . '11'; 
=> $nov = '' . '11'; // as $nov = '' at start 

現在$十一月在它具有價值11。

$nov .= $month ["month"]; // date 2016-11-18 16:36:12 
=> $nov .= '11'; //shorthand concatenation php 
=> $nov = $nov . '11'; 
=> $nov = '11' . '11'; // as $nov has vale 11 in it 

count($nov); // $nov have value '1111' which has 4 character so output will be 4 

如果你希望你的代碼工作做這樣

$jan = 0; $feb = 0; $maa = 0; $apr = 0; $mei = 0; $jun = 0; $jul = 0; $aug = 0; $sep = 0; $okt = 0; $nov = 0; $dec = 0; 

    foreach($dates as $date){      
      $month = date_parse_from_format("Y-m-d H:i:s", $date->date); 
      if($month ["month"] == '01'){$jan++} 
      if($month ["month"] == '02'){$feb++;} 
      if($month ["month"] == '03'){$maa++;} 
      if($month ["month"] == '04'){$apr++;} 
      if($maand["month"] == '05'){$mei++;} 
      if($maand["month"] == '06'){$jun++;} 
      if($maand["month"] == '07'){$jul++;} 
      if($maand["month"] == '08'){$aug++;} 
      if($maand["month"] == '08'){$sep++;} 
      if($maand["month "] == '10'){$okt++;} 
      if($maand["month "] == '11'){$nov++;} 
      if($maand["month "] == '12'){$dec++;} 
    } 

但我會建議你在數組做這樣

$visitorData = ['01'=>0,'02'=>0,'03'=>0,'04'=>0,'05'=>0,'06'=>0'07'=>0,'08'=>0,'09'=>0,'10'=>0,'11'=>0,'12'=>0]; 
foreach ($dates as $date) { 
    $month = date('m', strtotime($date->date)); 
    $visitorData[$month]++; 
} 

然後JSON數組給它編碼到jquery

希望這會有所幫助