Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Everyone on this forum has helped me tremendously. Thank you. I now have a page that functions the way I want it but my code seems to be cumbersome. To figure out a string to echo I have a long series of if else if statements that I can't figure out how to streamline.

Here is the code in full:

      <h1 class="ongoing_events">Ongoing</h1>

  <?php // This is the loop for quering an individual page's events and upcoming events
  date_default_timezone_set('America/New_York');
  $todaysDate = date('Y/m/d'); // Get today's date in the right format
  $today = date("Y-m-d 00:00");  // set today's date with eastern timezone
  $todayevening = date("Y-m-d 17:00");// set limit for "today" events extending to 5 pm same day
  $tonight = date("Y-m-d 03:00", strtotime("+1 day"));// set limit for "tonight" events extending to 3:00 am next day
  $tomorrow = date("Y-m-d 03:00", strtotime("+2 days"));// set limit for "tonight" events extending to 3:00 am next next day
  $lastWeek = date("Y-m-d", strtotime("-7 days"));
  $nextWeek = date("Y/m/d", strtotime("+7 days"));

  $eventsloop = new WP_Query('post_type=event&location=' . $whatCity .'&meta_key=end_date&meta_compare=>=&meta_value=' . $todaysDate . '&orderby=meta_value&order=ASC'); // Displays the events that have not ended yet

  while($eventsloop->have_posts()) : $eventsloop->the_post(); // begin the loop after sorting the posts by location date and removing old end dates

  /* Let's get all the meta date in one call rather then so many queries */
  $event_custom_meta=get_post_custom($post->ID); // Get all the data 
  if  ( $event_custom_meta )
  {
  $event_start_date = $event_custom_meta['start_date'][0]; 
  $event_end_date = $event_custom_meta['end_date'][0];
  $event_opening_time = $event_custom_meta['opening_time'][0];
  $event_closing_time = $event_custom_meta['closing_time'][0];
  $event_artist_talk_time = $event_custom_meta['artist_talk_time'][0];
  $event_lecture_time = $event_custom_meta['lecture_time'][0];
  $event_panel_time = $event_custom_meta['panel_time'][0];
  $event_special_event_time = $event_custom_meta['special_event_time'][0];
  $event_workshop_time = $event_custom_meta['workshop_time'][0];
  $event_custom_event_venue = $event_custom_meta['custom_event_venue'][0];
  $event_venue_info = $pieces = explode("***", $event_custom_meta[$prefix.'event_venue'][0]);
  $event_featured_event = $event_custom_meta['featured_event'][0];
  }   /* The test works. Alright, now I can echo these variables anyplace that I want */
  ?>

  <div class="ongoing_event">
  <div class="row">
  <div class="grid_4">

  <a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title('<h2>', '</h2>'); ?></a>

  <a href="<?php echo $event_venue_info[1]; ?>" title="<?php echo $event_venue_info[0]; ?>">
  <h3>
  <?php  
      if ($event_venue_info[0] != 'other') { 
          echo $event_venue_info[0];
          }
      if ($event_venue_info[0] == 'other') {
          echo $event_custom_event_venue;
          }
  ?>
  </h3></a> 

  <?php // Format the running dates
  $writestart = preg_split ( '/\//', $event_start_date );
  $writeend = preg_split ( '/\//', $event_end_date ); 
  ?>

  <h4>
  <?php // and print nicely
  if ($event_end_date <= $nextWeek) {
      echo '<span class="last_chance">Last Chance: </span>';    
  }
  echo date('F j', mktime( 0, 0, 0, $writestart[1], $writestart[2], $writestart[0] ) );
  echo ' through ';
  echo date('F j, Y', mktime( 0, 0, 0, $writeend[1], $writeend[2], $writeend[0] ) );
  ?>
  </h4>     

  <?php // Check if Opening or Closing dates exist and echo the information     
  $convertedtime2 = "F j, Y | g:i a"; // convert to 12 hour clock and minutes       
  $opening_time_formated = date($convertedtime2, strtotime($event_opening_time));   
  $closing_time_formated = date($convertedtime2, strtotime($event_closing_time));   
  $artist_talk_time_formated = date($convertedtime2, strtotime($event_artist_talk_time));   
  $lecture_time_formated = date($convertedtime2, strtotime($event_lecture_time));   
  $panel_time_formated = date($convertedtime2, strtotime($event_panel_time));   
  $special_event_time_formated = date($convertedtime2, strtotime($event_special_event_time));   
  $workshop_time_formated = date($convertedtime2, strtotime($event_workshop_time)); 
  $end_date_formated = date('F j, Y', mktime( 0, 0, 0, $writeend[1], $writeend[2], $writeend[0] ) );

  if ($event_opening_time >= $today) {
      echo '<h5>';
      if ($event_opening_time <= $todayevening) {
          echo '<span class="last_chance">Today: </span>';  
      }
      else if ($event_opening_time > $todayevening && $event_opening_time <= $tonight) {
          echo '<span class="last_chance">Tonight: </span>';    
      }
      else if ($event_opening_time > $tonight && $event_opening_time <= $tomorrow) {
          echo '<span class="last_chance">Tomorrow: </span>';
      }
      echo '<span class="strong">Opening: </span>'.$opening_time_formated .'</h5>';             
  }

  if ($event_closing_time >= $today) {
      echo '<h5>';
      if ($event_closing_time <= $todayevening) {
          echo '<span class="last_chance">Today: </span>';  
      }
      else if ($event_closing_time > $todayevening && $event_closing_time <= $tonight) {
          echo '<span class="last_chance">Tonight: </span>';    
      }
      else if ($event_closing_time > $tonight && $event_closing_time <= $tomorrow) {
          echo '<span class="last_chance">Tomorrow: </span>';
      }
      echo '<span class="strong">Closing: </span>'.$closing_time_formated .'</h5>';     
  }

  if ($event_artist_talk_time >= $today) {
          echo '<h5>';
      if ($event_artist_talk_time <= $todayevening) {
          echo '<span class="last_chance">Today: </span>';  
      }
      else if ($event_artist_talk_time > $todayevening && $event_artist_talk_time <= $tonight) {
          echo '<span class="last_chance">Tonight: </span>';    
      }
      else if ($event_artist_talk_time > $tonight && $event_artist_talk_time <= $tomorrow) {
          echo '<span class="last_chance">Tomorrow: </span>';
      }
      echo '<span class="strong">Artist Talk: </span>'.$artist_talk_time_formated .'</h5>';     
  }

  if ($event_lecture_time >= $today) {
      echo '<h5>';
      if ($event_lecture_time <= $todayevening) {
          echo '<span class="last_chance">Today: </span>';  
      }
      else if ($event_lecture_time > $todayevening && $event_lecture_time <= $tonight) {
          echo '<span class="last_chance">Tonight: </span>';    
      }
      else if ($event_lecture_time > $tonight && $event_lecture_time <= $tomorrow) {
          echo '<span class="last_chance">Tomorrow: </span>';
      }
      echo '<span class="strong">Lecture: </span>'.$lecture_time_formated .'</h5>'; 
  }

  if ($event_panel_time >= $today) {    
      echo '<h5>';
      if ($event_panel_time <= $todayevening) {
          echo '<span class="last_chance">Today: </span>';  
      }
      else if ($event_panel_time > $todayevening && $event_panel_time <= $tonight) {
          echo '<span class="last_chance">Tonight: </span>';    
      }
      else if ($event_panel_time > $tonight && $event_panel_time <= $tomorrow) {
          echo '<span class="last_chance">Tomorrow: </span>';
      }
      echo '<span class="strong">Panel: </span>'.$panel_time_formated .'</h5>';
  }

  if ($event_special_event_time >= $today) {
      echo '<h5>';
      if ($event_special_event_time <= $todayevening) {
          echo '<span class="last_chance">Today: </span>';  
      }
      else if ($event_special_event_time > $todayevening && $event_special_event_time <= $tonight) {
          echo '<span class="last_chance">Tonight: </span>';    
      }
      else if ($event_special_event_time > $tonight && $event_special_event_time <= $tomorrow) {
          echo '<span class="last_chance">Tomorrow: </span>';
      }
      echo '<span class="strong">Special Event: </span>'.$special_event_time_formated .'</h5>'; 
  }

  if ($event_workshop_time >= $today) {
      echo '<h5>';
      if ($event_workshop_time <= $todayevening) {
          echo '<span class="last_chance">Today: </span>';  
      }
      else if ($event_workshop_time > $todayevening && $event_workshop_time <= $tonight) {
          echo '<span class="last_chance">Tonight: </span>';    
      }
      else if ($event_workshop_time > $tonight && $event_workshop_time <= $tomorrow) {
          echo '<span class="last_chance">Tomorrow: </span>';
      }
      echo '<span class="strong">Workshop: </span>'.$workshop_time_formated .'</h5>';
  }

  the_excerpt();
  ?>

  </div><!-- end .grid_4-->
  <div class="grid_4"> 

  <a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php echo get_the_post_thumbnail($post_id, 'half_postcard'); ?></a> 

  </div> <!-- end .grid_4-->
  </div><!-- end .row-->                
  </div><!-- end .ongoing_event-->
  <?php endwhile; ?>

  </div> <!-- end .grid_8--><!--end ongoing events listing -->

Here are the inefficient if statements:

 if ($event_opening_time >= $today) {
      echo '<h5>';
      if ($event_opening_time <= $todayevening) {
          echo '<span class="last_chance">Today: </span>';  
      }
      else if ($event_opening_time > $todayevening && $event_opening_time <= $tonight) {
          echo '<span class="last_chance">Tonight: </span>';    
      }
      else if ($event_opening_time > $tonight && $event_opening_time <= $tomorrow) {
          echo '<span class="last_chance">Tomorrow: </span>';
      }
      echo '<span class="strong">Opening: </span>'.$opening_time_formated .'</h5>';             
  }

  if ($event_closing_time >= $today) {
      echo '<h5>';
      if ($event_closing_time <= $todayevening) {
          echo '<span class="last_chance">Today: </span>';  
      }
      else if ($event_closing_time > $todayevening && $event_closing_time <= $tonight) {
          echo '<span class="last_chance">Tonight: </span>';    
      }
      else if ($event_closing_time > $tonight && $event_closing_time <= $tomorrow) {
          echo '<span class="last_chance">Tomorrow: </span>';
      }
      echo '<span class="strong">Closing: </span>'.$closing_time_formated .'</h5>';     
  }

  if ($event_artist_talk_time >= $today) {
          echo '<h5>';
      if ($event_artist_talk_time <= $todayevening) {
          echo '<span class="last_chance">Today: </span>';  
      }
      else if ($event_artist_talk_time > $todayevening && $event_artist_talk_time <= $tonight) {
          echo '<span class="last_chance">Tonight: </span>';    
      }
      else if ($event_artist_talk_time > $tonight && $event_artist_talk_time <= $tomorrow) {
          echo '<span class="last_chance">Tomorrow: </span>';
      }
      echo '<span class="strong">Artist Talk: </span>'.$artist_talk_time_formated .'</h5>';     
  }

  if ($event_lecture_time >= $today) {
      echo '<h5>';
      if ($event_lecture_time <= $todayevening) {
          echo '<span class="last_chance">Today: </span>';  
      }
      else if ($event_lecture_time > $todayevening && $event_lecture_time <= $tonight) {
          echo '<span class="last_chance">Tonight: </span>';    
      }
      else if ($event_lecture_time > $tonight && $event_lecture_time <= $tomorrow) {
          echo '<span class="last_chance">Tomorrow: </span>';
      }
      echo '<span class="strong">Lecture: </span>'.$lecture_time_formated .'</h5>'; 
  }

  if ($event_panel_time >= $today) {    
      echo '<h5>';
      if ($event_panel_time <= $todayevening) {
          echo '<span class="last_chance">Today: </span>';  
      }
      else if ($event_panel_time > $todayevening && $event_panel_time <= $tonight) {
          echo '<span class="last_chance">Tonight: </span>';    
      }
      else if ($event_panel_time > $tonight && $event_panel_time <= $tomorrow) {
          echo '<span class="last_chance">Tomorrow: </span>';
      }
      echo '<span class="strong">Panel: </span>'.$panel_time_formated .'</h5>';
  }

  if ($event_special_event_time >= $today) {
      echo '<h5>';
      if ($event_special_event_time <= $todayevening) {
          echo '<span class="last_chance">Today: </span>';  
      }
      else if ($event_special_event_time > $todayevening && $event_special_event_time <= $tonight) {
          echo '<span class="last_chance">Tonight: </span>';    
      }
      else if ($event_special_event_time > $tonight && $event_special_event_time <= $tomorrow) {
          echo '<span class="last_chance">Tomorrow: </span>';
      }
      echo '<span class="strong">Special Event: </span>'.$special_event_time_formated .'</h5>'; 
  }

  if ($event_workshop_time >= $today) {
      echo '<h5>';
      if ($event_workshop_time <= $todayevening) {
          echo '<span class="last_chance">Today: </span>';  
      }
      else if ($event_workshop_time > $todayevening && $event_workshop_time <= $tonight) {
          echo '<span class="last_chance">Tonight: </span>';    
      }
      else if ($event_workshop_time > $tonight && $event_workshop_time <= $tomorrow) {
          echo '<span class="last_chance">Tomorrow: </span>';
      }
      echo '<span class="strong">Workshop: </span>'.$workshop_time_formated .'</h5>';
  }

Thank you in advance for your help. Excuse my lack of ability to use php efficiently.

share|improve this question

closed as off topic by onetrickpony, t31os, Rarst Jan 23 '12 at 12:16

Questions on WordPress Development Stack Exchange are expected to relate to WordPress within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

4  
What makes you think it's inefficient? And how is this question useful to other community members? –  t31os Dec 22 '11 at 14:35
    
Excuse my question if it is not helpful. I am trying to learn and will be more careful before I ask a question. –  John Bentwin Dec 22 '11 at 16:50

1 Answer 1

up vote 3 down vote accepted

This question probably belongs to Code Review.

Anyway, you can fill your event properties inside an array, which you can iterate:

$events = array(
  'opening'  => array(
                 'time'             => '...time here...',
                 'formatted_time'   => '...time here...',
                 'label'            => __('Opening:'),      // or ucfirst($type) if you want...
                 ),

  'closing'  => array(
                 'time'             => '...time here...',
                 'formatted_time'   => '...time here...',
                 'label'            => __('Clsoing:'),
                 ),

  'artist_talk'  => array(
                 'time'             => '...time here...',
                 'formatted_time'   => '...time here...',
                 'label'            => __('Artist Talk:'),
                 ),

  // and so on...
);



foreach($events as $type => $properties){
 if($properties['time'] >= $today) {
      echo '<h5>';

      if ($properties['time'] <= $todayevening) {
          echo '<span class="last_chance">Today: </span>';
      }
      elseif ($properties['time'] > $todayevening && $properties['time'] <= $tonight) {
          echo '<span class="last_chance">Tonight: </span>';
      }
      elseif ($properties['time'] > $tonight && $properties['time'] <= $tomorrow) {
          echo '<span class="last_chance">Tomorrow: </span>';
      }

      echo '<span class="strong">'.$properties['label'].' </span>'.$properties['formatted_time'] .'</h5>';
  }
}
share|improve this answer
    
Thank you. I will restrain from asking trivial questions in the future. –  John Bentwin Dec 22 '11 at 16:50

Not the answer you're looking for? Browse other questions tagged or ask your own question.