2014-03-28 57 views
0

我怎樣才能使此代碼更有效率?網豆說我的功能太多了。也就是說,如果我有我的if語句,我應該引入一個新的函數。可能一些身體幫我創造了良好的作用,使我的功能更小使我的功能更好

function dispalyEvent($weekNr, $week, $year){ 
    echo "<p>"; 
    $gendate = new DateTime(); 
    $gendate->setISODate($year,$week,$weekNr); 

    $month = $gendate->format('m'); 
    $day = $gendate->format('d'); 
    $event_query = mysql_query("SELECT * FROM calendar ORDER BY starttime"); 

    while($event = mysql_fetch_array($event_query)) { 
    $startYear = $event['startyear']; 
    $startMonth = $event['startmonth']; 
    $startDay = $event['startdate']; 
    $endYear = $event['endyear']; 
    $endMonth = $event['endmonth']; 
    $endDay = $event['enddate']; 

    $period = new DatePeriod(
      new DateTime($startYear.$startMonth.$startDay), 
      new DateInterval('P1D'), 
      new DateTime($endYear.$endMonth.$endDay +1) 
    ); 

    $currentDate = $year."-".$month."-".$day; 

     foreach ($period as $savedDate) { 

      if ($currentDate == $savedDate->format('Y-m-d')){ 
       buildEvent($event['ad'], $event['starttime'], $event['title'], $event['endtime'], $event['location'], $event['address'], $event['price'], $event['description']); 
      }  
      if ($event['Approved'] == "Approved"){ 
        buildEvent($event['ad'], $event['starttime'], $event['title'], $event['endtime'], $event['location'], $event['address'], $event['price'], $event['description']); 
      } 

     } 

    } 
    echo "</p>"; 
} 
?> 
+1

的一種方式。你應該改用['MySQLi'](http://www.php.net/mysqli)或['PDO'](http://www.php.net/PDO)。 – Tularis

+0

如果你想讓別人查看你的代碼,你應該在[codereview.se]上發帖 – 2014-03-31 04:31:22

回答

1

你不應該採取的NetBeans警告說,嚴重的是,不是那種至少。他們大多數是可配置的,你可以改變它們。 與團隊一起工作時,這些功能非常有用,如果您想「強制」團隊按照某些規則設置這些規則來幫助您驗證代碼。

除了Tularis說的我看不出你能做什麼。請記住,一個好的代碼並不總是簡短的,易於理解的代碼就如它所能得到的那樣好。

2

出於安全和支持的原因,除了切換到MySQLiPDO之外;您也可以清除大部分未使用的代碼。

例如:您實際上不必爲每個數組值創建新變量。

這裏就是我會做的是:改進它是停止使用'mysql`延伸,因爲它正式棄用的

<?php 
function dispalyEvent($weekNr, $week, $year){ 
    echo "<p>"; 
    $currentDate = new DateTime()->setISODate($year,$week,$weekNr)->format('Y-m-d'); 

    $event_query = mysql_query("SELECT * FROM calendar ORDER BY starttime");   
    while($event = mysql_fetch_array($event_query)) { 
     $period = new DatePeriod(
      new DateTime($event['startyear'].$event['startmonth'].$event['startday']), 
      new DateInterval('P1D'), 
      new DateTime($event['endyear'].$event['endmonth'].(event['$endday']+1)) 
     ); 

     foreach ($period as $savedDate) { 
      if ($currentDate == $savedDate->format('Y-m-d') || $event['Approved'] == "Approved"){ 
       buildEvent($event['ad'], $event['starttime'], $event['title'], $event['endtime'], $event['location'], $event['address'], $event['price'], $event['description']); 
      } 
     } 
    } 
    echo "</p>"; 
} 
?> 
+0

我在$ currentDate = new DateTime() - > setISODate($ year,$ week,$ weekNr) - > format('Ym -d'); –