我怎樣才能使此代碼更有效率?網豆說我的功能太多了。也就是說,如果我有我的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>";
}
?>
的一種方式。你應該改用['MySQLi'](http://www.php.net/mysqli)或['PDO'](http://www.php.net/PDO)。 – Tularis
如果你想讓別人查看你的代碼,你應該在[codereview.se]上發帖 – 2014-03-31 04:31:22