简体   繁体   English

使我的功能更好

[英]Making my function better

How could I make this code more efficient? 我如何才能使此代码更高效? Net-beans is saying that I have too many lines in my functions. Net-beans表示我的功能行太多。 Also it is saying that where I have my if statement I should introduce a new function. 也就是说,如果我有if语句,则应该引入一个新函数。 Could some body help me with creating a good function and making my function smaller 某些身体可以帮助我创建良好的功能并使我的功能更小吗

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

Aside from switching to MySQLi or PDO for safety and support reasons; 除了出于安全和支持原因而切换到MySQLiPDO之外; you can also clear most of your unused code. 您还可以清除大部分未使用的代码。

For example: you don't actually have to create new variables for every array-value. 例如:您实际上不必为每个数组值创建新变量。

Here's what I would make of it: 这就是我要做的:

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

You shouldn't take NetBeans warning that seriously, not that kind at least. 您不应该认真对待NetBeans警告,至少不要这样。 Most of them are configurable and you can change them. 它们中的大多数是可配置的,您可以更改它们。 Those are useful when working with a team, if you want to "force" your team to follow certain rules you set those rules to help you verify the code. 与团队合作时,这些功能很有用,如果您想“强迫”团队遵循某些规则,则可以设置这些规则来帮助您验证代码。

Other than what Tularis said I don't see what you can do. 除了图拉里斯说的话,我看不到你能做什么。 Please remember that a good code is not always short, code that is easily understandable is as good as it gets. 请记住,好的代码并不总是简短的,易于理解的代码就已经足够好了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM