0

我是一名学生网络程序员,最近分配给我的任务遇到了问题。担任我职位的前任创建了一个有点错误的新闻报道网站。

该网站的问题在于,当您指定要显示新闻文章的日期范围时,日期将恢复到以前的状态,或者会随机更改。尝试在 2010 年之前设置一个日期是让这个问题每次都发生的唯一方法。有时它会在出现问题之前用于多种用途。

澄清一下,我不是在寻找/希望有人给我一个直接的答案/为我做我的工作,特别是因为我真的在努力学习和提高我的技能。如果有人能指出我解决这个问题的正确方向,我将不胜感激。

以下是我正在查看的文件中对日期的所有引用:

// Create dates
$year = ( isset( $get['year'] ) ) ? $get['year'] : date("Y");
$year2 = ( isset( $get['year2'] ) ) ? $get['year2'] : date("Y");
$month = ( isset( $get['month'] ) ) ? $get['month'] : date("m");
$month2 = ( isset( $get['month2'] ) ) ? $get['month2'] : date("m");
$day = ( isset( $get['day'] ) ) ? $get['day'] : date("d");
$day2 = ( isset( $get['day2'] ) ) ? $get['day2'] : date("d");

//create first and second dates for range
$t_current = mktime(0, 0, 0, $month, $day, $year); // Current month Unix timestamp
$t_next = mktime(0, 0, 0, $month2, $day2+1, $year2); // date2 unix timestamp for date range

// Criteria
if ( $author_url) {
    $this->EE->db -> where ( $author_name_field_id, $author_name);

$this->EE->db
    -> where ("exp_channel_titles.entry_date >= $t_current")
    -> where ("exp_channel_titles.entry_date <= $t_next");

}

else {
    $this->EE->db
    -> where ("exp_channel_titles.entry_date >= $t_current")
    -> where ("exp_channel_titles.entry_date <= $t_next");
}

模板:

<div class="datelists">
    <select id="month" name="month" style="display:none">
<?php
//lists months
            for ($i = 0; $i <= 11; ++$i) 
            {
                $time = strtotime(sprintf('+%d months', $i));
                $value = date('m', $time);
                $label = date('F', $time);

//if month is set stay on that month    
                if($month==$value)
                    { printf('<option value="%s" selected="selected">%s</option>' , $value, $label);
                    }
                else
                    {printf('<option value="%s">%s</option>', $value, $label);}
            }

  ?>
//first month selected instead of blank
        $("#target option:first")

    </select>
    <select id="day" name="day" style="display:none">   
<?php
//lists days
            for ($i = 0; $i <= 31; ++$i) 
            {

                $time = strtotime(sprintf('+%d days', $i));
                $value = date('d', $time);
                $label = date('d', $time);
//if day is set stay on that day            
                if($day==$value)
                    { printf('<option value="%s" selected="selected">%s</option>' , $value, $label);
                    }
                else
                    {printf('<option value="%s">%s</option>', $value, $label);} 
            }           
//first year selected instead of blank
  ?>
    $("#target option:first")
    </select>
    <select id="year" name="year" style="display:none"> 
<?php   
//lists years
            for ($i = 0; $i <= 3; ++$i) 
            {                 
                $time = strtotime(sprintf('-%d years', $i));
                $value = date('Y', $time);
                $label = date('Y', $time);  
//if year is set stay on that year          
                if($year==$value)
                    { printf('<option value="%s" selected="selected">%s</option>' , $value, $label);
                    }
                else
                    {printf('<option value="%s">%s</option>', $value, $label);}         
            }

//first year selected instead of blank
  ?>
    $("#target option:first")
    </select>
    <input type="hidden" id="datepicker" />
    <select id="month2" name="month2" style="display:none">
<?php
//lists months
            for ($i = 0; $i <= 11; ++$i) 
            {
                $time = strtotime(sprintf('+%d months', $i));
                $value = date('m', $time);
                $label = date('F', $time);
//if month is set stay on that month        
                if($month2==$value)
                    { printf('<option value="%s" selected="selected">%s</option>' , $value, $label);
                    }
                else
                    {printf('<option value="%s">%s</option>', $value, $label);}
            }
  ?>
//first month selected instead of blank
        $("#target option:first")
    </select>
    <select id="day2" name="day2" style="display:none">
<?php
//lists days
            for ($i = 0; $i <= 31; ++$i) 
            {     
                $time = strtotime(sprintf('+%d days', $i));
                $value = date('d', $time);
                $label = date('d', $time);
//if day is set stay on that day            
                if($day2==$value)
                    { printf('<option value="%s" selected="selected">%s</option>' , $value, $label);
                    }
                else
                    {printf('<option value="%s">%s</option>', $value, $label);}     
            }
//first year selected instead of blank
  ?>
    $("#target option:first")
    </select>
    <select id="year2" name="year2" style="display:none">
<?php
//lists years
            for ($i = 0; $i <= 3; ++$i) 
            {             
                $time = strtotime(sprintf('-%d years', $i));
                $value = date('Y', $time);
                $label = date('Y', $time);  
//if year is set stay on that year          
                if($year2==$value)
                    { printf('<option value="%s" selected="selected">%s</option>' , $value, $label);
                    }
                else
                    {printf('<option value="%s">%s</option>', $value, $label);}             
            }               
//first year selected instead of blank
  ?>
    $("#target option:first")
    </select>
    <input type="hidden" id="datepicker" />  
</div>
<label for="from">From</label>
<input type="text" id="from" value="<?php echo $month."/".$day."/".$year; ?>" />
<label for="to">to</label>
<input type="text" id="to" value="<?php echo $month2."/".$day2."/".$year2; ?>" />               
<input type="submit" value="Filter" />
        </form>
               </p>
</div>     
<!--<h1><?php echo "$month/$day/$year - $month2/$day2/$year2" . ' <br/>'?></h1>-->
<?php
    if ( isset($get['institute']) && is_numeric( $get['institute'] )  ) {

        echo "<h2><center>" . $institute_cat . "</center></h2>";
    }

    if ( empty($entries) ) {
        echo "<h2><center>No articles found</center></h2>";
    }
    else {
?>  

Javascript:

<!-- javascript datepicker -->
<link rel="stylesheet" type="text/css" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" /></link>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<link rel="stylesheet" type="text/css" href="http://expeng.anr.msu.edu/css/sitewide/datepicker.css" />  </link>
<script type="text/javascript">
$(document).ready(function(){
    $('#from').datepicker({
        defaultDate: "+1w",
        changeMonth: true,
        numberOfMonths: 1,
        onSelect: function(dateText, inst) {
                //dateText comes in as MM/DD/YY
                var datePieces = dateText.split('/');
                var month = datePieces[0];
                var day = datePieces[1];
                var year = datePieces[2];
                //define select option values for
                $('select#month').val(month);
                $('select#day').val(day);
                $('select#year').val(year);
        },                  
    }); 
    $('#to').datepicker({
        defaultDate: "+1w",
        changeMonth: true,
        numberOfMonths: 1,
        onSelect: function(dateText, inst) {
                //dateText comes in as MM/DD/YY
                var datePieces = dateText.split('/');
                var month2 = datePieces[0];
                var day2 = datePieces[1];
                var year2 = datePieces[2];
                //define select option values for
                $('select#month2').val(month2);
                $('select#day2').val(day2);
                $('select#year2').val(year2);
         },     
    }); 
});
</script>   

http://msue.anr.msu.edu/news/report是一个网站,以防万一有人有兴趣自己查看问题。

提前致谢!

4

2 回答 2

1

在这部分代码中(注意第for()47 行的循环):

    <select id="year" name="year" style="display:none"> 
<?php   
//lists years
            for ($i = 0; $i <= 3; ++$i)    // Try to change this to: for ($i = 0; $i <= 5; ++$i)
            {                 
                $time = strtotime(sprintf('-%d years', $i));
                $value = date('Y', $time);
                $label = date('Y', $time);  
//if year is set stay on that year          
                if($year==$value)
                    { printf('<option value="%s" selected="selected">%s</option>' , $value, $label);
                    }
                else
                    {printf('<option value="%s">%s</option>', $value, $label);}         
            }

本节(注意第for()106 行的循环):

    <select id="year2" name="year2" style="display:none">
<?php
//lists years
            for ($i = 0; $i <= 3; ++$i)    // Try to change this to: for ($i = 0; $i <= 5; ++$i) 
            {             
                $time = strtotime(sprintf('-%d years', $i));
                $value = date('Y', $time);
                $label = date('Y', $time);  
//if year is set stay on that year          
                if($year2==$value)
                    { printf('<option value="%s" selected="selected">%s</option>' , $value, $label);
                    }
                else
                    {printf('<option value="%s">%s</option>', $value, $label);}             
            }               
//first year selected instead of blank
  ?>

它正在创建一个下拉选择,其中包含从当年到三年前的年份。因此,当您指定超出该年份的年份(2010 年之前)时,该年份不是该<select>框的一部分。对于 for 也是<select>如此year2

如果您想支持更多年,只需将 3 的值更改为其他值。

顺便说一句,尝试添加检查开始日期 <= 结束日期。我试过了,虽然它没有给我任何错误,但它只是没有显示任何结果,而不是说它是无效的日期范围。

于 2013-10-09T15:05:56.583 回答
0

此时代码中:

//create first and second dates for range
$t_current = mktime(0, 0, 0, $month, $day, $year); // Current month Unix timestamp
$t_next = mktime(0, 0, 0, $month2, $day2+1, $year2); // date2 unix timestamp for date range

我肯定会添加一个检查以确保 $t_next 晚于 $t_current 因为这将是我要寻找的第一个逻辑问题。

if($t_current>$t_next){
    // swap them over
    $tmp=$t_current;
    $t_current=$t_next;
    $t_next=$tmp;
}
于 2013-10-09T14:23:02.097 回答