gpt4 book ai didi

javascript - 让我的功能变得更好

转载 作者:行者123 更新时间:2023-11-28 19:55:11 24 4
gpt4 key购买 nike

如何使这段代码更高效? Net-beans 说我的函数中有太多行。它还说,在我有 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>";
}
?>

最佳答案

除了切换到 MySQLiPDO出于安全和支持原因;您还可以清除大部分未使用的代码。

例如:您实际上不必为每个数组值创建新变量。

这就是我的想法:

<?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>";
}
?>

关于javascript - 让我的功能变得更好,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22724827/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com