blackhill24
blackhill24

Reputation: 452

PHP for each - else always returns else value

Im using the code below to send a report however when i add the else statement that is all that is returned even when the if statement is true.

can anyone explain why this? i dont think ive ever had this issue before?

foreach ( $result as $page ) {
 $date1 = new DateTime($page->start_date);
 $date2 = new DateTime($page->end_date);

 if (strtotime($page->start_date) >= strtotime('today') && strtotime($page->start_date) < strtotime('tomorrow')) {

     $email_content .=  '<span style=" background-colour: #777777; font-size: 1em; font-family: arial, sans-serif; color:#202020;"><strong>' . $page->post_title . ' </strong></span>';

     $email_content .=  '<span style=" font-size: 1em; font-family: arial, sans-serif; color:#00b200;"><strong>Order By '  . $date1->format('d-m-y') . ' ' . '</strong></span>';

     $email_content .=  '<div style="  font-size: 1em; font-family: arial, sans-serif; color:#cc0000;"><strong>For Delivery '  . $date2->format('d-m-y') . '</strong></div><br>' . '<br>';

 }else {
     $email_content =  '<span style=" background-colour: #777777; font-size: 1em; font-family: arial, sans-serif; color:#202020;"><strong>tester </strong></span>';
 }
}    

thanks for reading :)

Upvotes: 1

Views: 71

Answers (1)

tjfo
tjfo

Reputation: 1189

As you loop through, your $email_content variable is getting overwritten in the else statement. You are appending in the if with the . but overwriting in the else. Try updating your code to this:

        foreach ( $result as $page ) {
     $date1 = new DateTime($page->start_date);
     $date2 = new DateTime($page->end_date);

     if (strtotime($page->start_date) >= strtotime('today') && strtotime($page->start_date) < strtotime('tomorrow')) {

         $email_content .=  '<span style=" background-colour: #777777; font-size: 1em; font-family: arial, sans-serif; color:#202020;"><strong>' . $page->post_title . ' </strong></span>';

         $email_content .=  '<span style=" font-size: 1em; font-family: arial, sans-serif; color:#00b200;"><strong>Order By '  . $date1->format('d-m-y') . ' ' . '</strong></span>';

         $email_content .=  '<div style="  font-size: 1em; font-family: arial, sans-serif; color:#cc0000;"><strong>For Delivery '  . $date2->format('d-m-y') . '</strong></div><br>' . '<br>';
    }else {
         $email_content .=  '<span style=" background-colour: #777777; font-size: 1em; font-family: arial, sans-serif; color:#202020;"><strong>tester </strong></span>';

 }
}  

Upvotes: 1

Related Questions