Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

How to limit Nested Block (if and case etc.) in one function

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

How to limit Nested Block (if and case etc.) in one function

Postby pietercdevries » Sun Mar 30, 2014 9:12 pm

Hello everyone I have a huge function and I want to limit the amount of nested block could someone help me.

My code:

Code: Select all
function dispalyEvent($weekNr, $week, $year){
    echo "<p>";
   
    $gendate = new DateTime();
    $gendate->setISODate($year,$week,$weekNr);
    $event_query = mysql_query("SELECT * FROM calendar ORDER BY starttime");
   
    //Go through all event in the database
    while($event = mysql_fetch_array($event_query)) {    
   
    //Create a range for starting date and ending date
    $date1 = new DateTime($event['startyear'].$event['startmonth'].$event['startdate']);
    $date2 = new DateTime($event['endyear'].$event['endmonth'].$event['enddate']);
    $date2->modify('+1 day');
       
    $period = new DatePeriod($date1, new DateInterval('P1D'), $date2);

     $title = $event['title'];
     $name = $event['name'];
       
        $recur_query = mysql_query("SELECT * FROM recur WHERE title = '$title' AND name = '$name'");
        $recur = mysql_fetch_array($recur_query);
       
        $recurring = $recur['type'];
       
            //Find day of starting recurring event and ending day 
            if (!$recurring == "None"){
                $starttime = explode("/",$recur['startdate']);
                $startdate = new DateTime();
                $startdate->setDate($starttime[2], $starttime[0], $starttime[0]);
               
                $endtime = explode("/",$recur['enddate']);
                $enddate = new DateTime();
                $enddate->setDate($endtime[2], $endtime[0], $endtime[0]);

            }
            else {
                $startdate = new DateTime();
                $enddate = new DateTime();
            }
           
            //Put the dates in integer to find if it is out of range
            $displaydate = intval($gendate->format("Ymd"));
            $startdate = intval($startdate->format("Ymd"));
            $enddate = intval($enddate->format("Ymd"));
            settype($displaydate, "integer");
            settype($startdate, "integer");
            settype($enddate, "integer");
   
        //Go through each date in the range
        foreach ($period as $savedDate) {   
            //Check if the Item is Approved
            if ($event['Approved'] == "Approved"){
           
                switch($recurring){
         
                    Case 'None':
   
                        //If the date in the range is the same as the displaydate
                        if ($gendate->format("Y-m-d") == $savedDate->format('Y-m-d')){
               
                            //Create event
                            renderEvent($event['ad'], $event['starttime'], $event['title'], $event['endtime'], $event['location'], $event['address'], $event['price'], $event['description']);
                        }
                    break 1;
                   
                    Case 'Daily':

                        //Check margin between start and end date of recurring event
                        if ($displaydate > $startdate and !$displaydate < $enddate){
                         
                            //Check if the day number is the same
                            if ($recur['day']-1 ==  $gendate->format("w")){
                               
                                //Create event
                                renderEvent($event['ad'], $event['starttime'], $event['title'], $event['endtime'], $event['location'], $event['address'], $event['price'], $event['description']);
                            }     
                        }
                    break 1;
                   
                    Case 'Weekly':
                        //Check margin between start and end date of recurring event
                        if ($displaydate > $startdate and !$displaydate < $enddate){
                           
                            //Find the amount of weeks between two dates
                            $weekRange = datediffInWeeks($recur['startdate'], $recur['enddate']);
                           
                            //Round down to the possible amount to display
                            $weeks = ceil($weekRange / $recur['day']);
                           
                            //Returns the week cuurent week to display
                            $currentWeek = $gendate->format("W");
                           
                            //Loop for every #(1, 2, 3, 4) of weeks
                            for ($n=0; $n<$weeks; $n++) {
                                 
                                //Display event if weeks are the same
                                if ($n == $currentWeek) {

                                    //Put days in array
                                    $days = explode(",",$recur['weekday']);
                                   
                                    //If number day of the week is the same display event
                                    foreach ($days as $day) {

                                        //Check if the day number is the same
                                        if ($day == $gendate->format("w")) {

                                                //Create event
                                                renderEvent($event['ad'], $event['starttime'], $event['title'], $event['endtime'], $event['location'], $event['address'], $event['price'], $event['description']);
                                        }
                                    }
                                }   
                            }
                        }
                    break 1;
                }
            }
        } 
    }
    echo "</p>";
}
pietercdevries
New php-forum User
New php-forum User
 
Posts: 5
Joined: Tue Feb 18, 2014 2:38 pm

Re: How to limit Nested Block (if and case etc.) in one func

Postby seandisanti » Thu Apr 03, 2014 9:08 pm

Your problem is that you're querying incorrectly. Write out the logic and the structures and I'll write a query for you that covers it. I'm sorry, it's a little late in the evening for me to piece it together myself from teh code you've written. feel free to pm me if you want
seandisanti
php-forum Fan User
php-forum Fan User
 
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm


Return to PHP coding => General

Who is online

Users browsing this forum: yetticaretti and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.