2013-10-09 84 views
0

我是學生,網絡程序員和我有一個任務我最近assigned.The以前的人誰拉着我的位置設立了一個新聞報道的網站,是一個小馬車的問題。不工作日期的領域正確

與網站的問題是,當您指定的日期範圍的新聞文章,以顯示它的日期,要麼恢復到他們先前否則將更改爲隨機。試圖在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是網站以防萬一有人對自己的問題感興趣。

在此先感謝!

+0

將$ _GET變量向前傳遞而不進行消毒不是很好的安全措施。這樣做會使您容易受到代碼注入的影響。看看EE的輸入類,以更安全的方式來處理:http://ellislab.com/expressionengine/user-guide/development/usage/input.html#Input::get – AllInOne

回答

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; 
} 
1

在代碼中的這一部分(注意在47行for()環路):

<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);}   
      } 

這部分(注意線106 for()環路):

<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>框。 <select>同樣適用於year2

如果您想支持更多的年份,只需將其值改爲3即可。

順便說一句,嘗試添加的開始日期< =結束日期的支票。我試了一下,雖然它沒有給我任何錯誤,但它只是顯示沒有結果,而不是說它是無效的日期範圍。

+0

我試着將3的值改爲某些東西其他但在2010年之前它仍然不適用。另外,我加了一張Cwissy之前建議的支票,但是,我仍然可以提交一個無效的日期,它會接受它,就像您提到的那樣發生在您身上。 – DemetriP

+0

你是否爲'year'和'year2'更改了3的值?嘗試將值更改爲5,然後查看生成的HTML的頁面源代碼,並確保您擁有''一直到'' 。我嘗試使用螢火蟲(在您的鏈接頁面中)添加這些選項,並在我提交頁面時工作。 – Sutandiono

+0

我很困惑,難道我只是在那個for循環中改變3的值?當我查看源代碼時,它會顯示選項值會像你說的一樣到2008年,但它仍會自動將2010年之前的任何一年的年份自動更改爲2013年。 – DemetriP