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

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

Post Reply
pietercdevries

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

Post by 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>";
}

seandisanti
php-forum Fan User
php-forum Fan User
Posts: 973
Joined: Mon Oct 01, 2012 12:32 pm

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

Post by 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

Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests