如果日期不为空或日期有字符串“日期”

时间:2013-12-06 07:34:19

标签: php ajax arrays date

您好我有一个下拉列表,显示的第一个选项是我设置为DATE的选项..如果日期为空或者选择了第一个选项“DATE”,我需要运行if语句。 。知道我的AJAX只有在选择日期时才有效。

这是我的下拉

<?php
$dates = array();
$argez = (array( 'post_type' => 'latest_message'));
query_posts( $argez );
   if (have_posts())  : while (have_posts()) : the_post(); 
       $dates[] = get_the_date();
       $dates = array_unique($dates);
       print_r($datesun);

    endwhile; 
    echo '<option value="#">DATE</option>';
    foreach($dates as $date) {
     echo '<option value="' . $date . '">' . $date .'</option> ';
     }
    endif;
?>

这是我的ajax

global $post, $wpdb;
if ($_POST["series"]!=0 ) {
    $series = array($_POST['series']);
    } else {
         $series = get_terms( 'series', array('fields' => 'ids') );
         //$series = array(implode(', ',$series));
    }
if ($_POST["speaker"]!=0 ) {
    $speaker = array($_POST["speaker"]);
    } else {
         $speaker = get_terms( 'speaker', array('fields' => 'ids') );
         //$speaker = array(implode(', ',$speaker));
    }
if ($_POST["topic"]!=0 ) {
$topic = array($_POST["topic"]);
} else {
     $topic = get_terms( 'topic', array('fields' => 'ids') );
     //$topic = array(implode(', ',$topic));

}
if ($_POST["date"]!= NULL || $_POST["date"] == 'DATE') {
$dates =  strtotime($_POST["date"]);
}
else {

}
    wp_reset_query();

    $myquery= array(

        'post_type' => 'latest_message',
        'tax_query' => array(
        array(
            'taxonomy' => 'series',
            'terms' => $series,
            'field' => 'id'
        ),
        array(
            'taxonomy' => 'speaker',
            'terms' => $speaker,
            'field' => 'id',
        ),
        array(
            'taxonomy' => 'topic',
            'terms' => $topic,
            'field' => 'id',
        )
    ),
    'date_query' => array(
                'year' => date('Y', $dates),
                'month' => date('m', $dates),
                'day' => date('d', $dates),
        ),


    );

print_r($myquery);
    query_posts($myquery);

部分问题是日期数组在下面提供了这些信息。我如何让它仍然运行,因为没有任何信息匹配该日期..任何想法

[date_query] => Array ( [year] => 1970 [month] => 01 [day] => 01

2 个答案:

答案 0 :(得分:0)

为什么不简单地将第一个值设置为disabled selected,如下所示:

echo "<select>";
echo "<option value='#' disabled selected>DATE</option>";
foreach($dates as $date)
    echo "<option value='$date'>$date</option>";
echo "</select>";

来源:How do I make a placeholder for a 'select' box?

答案 1 :(得分:0)

将您的核对声明更改为此

if (isset($_POST["date"]) && $_POST["date"] != NULL && $_POST["date"] != '#') {
 // if the value is set and it's not equal null or # ; then treat it as passed in value
$dates =  strtotime($_POST["date"]);
}
else {
   // here where the user have selected "DATE"
}

我还建议您查看How to determine if value is a date in PHP