2015-10-17 124 views
1

我試圖創建一個函數來用事件填充ajax日曆。 我在我的wordpress數據庫中查詢自定義字段的日期(格式爲Ymd)。按值排序查詢的結果

 $metakey2 = 'jour'; 
     $jours = $wpdb->get_col($wpdb->prepare("SELECT DISTINCT meta_value FROM $wpdb->postmeta WHERE meta_key = %s ORDER BY meta_value ASC", $metakey2)); 
     if ($jours) { 
      $years = substr($jours, 0, 4); // OUTPUT YEAR FROM DATE 
      $months = substr($jours, 5, 2); // OUTPUT MONTH FROM DATE 
      $days = substr($jours, 7, 2); // OUTPUT DAY FROM DATE 

       foreach $years as $year { 
        foreach $months as $month { 

         if ($y == $year AND $m == $month) { 
          $events = array(
           foreach $days as $day { 
            $day => array(
             0 => array(
              "0" => "Lorem ipsum dolor 111", 
             ) 
            ); 
           } 
          ); 
         } 
        } 
       } 
     } 

    ` 

我絕對不是專家,我一直在努力使這項工作了幾個小時,現在...有人能指出我朝着正確的方向嗎? 謝謝 Manue

+0

您是否使用custom_post?你查詢什麼類型的帖子來獲取post_meta? 你能解釋它是如何工作的嗎? – Nozifel

+0

我使用來自woocommerce的產品與自定義字段。對於每個產品,我都會在自定義字段中輸入日期。我需要的是在日曆中輸出這些產品,因此是陣列。 – Tiwelle

回答

0
$metakey2 = 'jour'; 
$jours = $wpdb->get_col($wpdb->prepare("SELECT DISTINCT meta_value FROM $wpdb->postmeta WHERE meta_key = %s ORDER BY meta_value ASC", $metakey2)); 

// Chaeck the data returned isn't null, blank, 0-length array, etc. 
if (!empty($jours)) { 
    // Initialize events array 
    $events = array(); 

    // Loop through the data 
    foreach ($jours as $jour) { 
     $year = substr($jour, 0, 4); // OUTPUT YEAR FROM DATE 
     $month = substr($jour, 5, 2); // OUTPUT MONTH FROM DATE 
     $day = substr($jour, 7, 2); // OUTPUT DAY FROM DATE 

     // PROCESS YOUR DATA HERE 
     ... 
    } 
} 

的關鍵是空管檢查後foreach循環。你需要循環從數據庫返回的數據作爲數組。