太多的如果和其他的如果的??-肯定是更好的方法

时间:2011-12-22 作者:John Bentwin

这个论坛上的每个人都给了我极大的帮助。非常感谢。我现在有了一个按我想要的方式运行的页面,但我的代码似乎很繁琐。为了找出要回音的字符串,我有一长串if else if 我不知道如何简化的语句。

以下是完整的代码:

      <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 -->
下面是效率低下的if 声明:

 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>\';
  }
提前感谢您的帮助。请原谅我缺乏有效使用php的能力。

1 个回复
最合适的回答,由SO网友:onetrickpony 整理而成

这个问题可能属于Code Review.

无论如何,您可以在数组中填充事件属性,可以对其进行迭代:

$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>\';
  }
}

结束

相关推荐

更改函数.php和.htaccess文件

如果我改变了我的Wordpress网站,我会弄糟吗functions.php 自定义主题的文件?有一次我给它添加了一些代码,它给了我一些PHP错误,我的网站无法访问。幸运的是,这是在网站的早期阶段,所以我刚刚重新安装了Wordpress。所以,我的实际问题是functions.php 文件对任何其他文件或数据库或网站的任何其他部分有任何影响?如果我有备份functions.php 文件,我可以通过FTP来替换这个搞糟的文件吗,或者我必须做些其他事情吗?同样的问题也适用于.htaccess 文件-如果我添加