我正在尝试创建一个用事件填充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 :(得分:1)
$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循环。您需要循环遍历从数据库返回的数据作为数组。