2017-03-02 81 views
0

我有下面的代碼輸出每個迭代的對象。如何使輸出從第二次迭代開始?如何從foreach循環的第二次迭代開始輸出?

<?php foreach ($data['restos'] as $lid => $resto):?> 
<?php 
          $time_arr = sic_get_start_expire_date_for_user_resto($this->current_user->ID, $lid); 
          $is_expired_class = ''; 
          if (isset($time_arr['expire_time']) && time()>strtotime($time_arr['expire_time'])){                  
           $is_expired_class = 'sic-expired-resto'; 
          } 
         ?>       
<?php if (!empty($data['restos_metas']['sic_restos_on']) && !empty($level['resto_image_url'])):?> 
          <div class="resto-outer-wrapper <?php echo $is_expired_class;?>"><img src="<?php echo $resto['resto_image_url'];?>" class="resto" title="<?php echo $resto['label'];?>" /></div> 
         <?php elseif (!empty($resto['label'])):?> 
          <div class="sic-above <?php echo $is_expired_class;?>"><?php echo $resto['label'];?></div> 
         <?php endif;?> 
        <?php endforeach;?> 

回答

0

最簡單的方法是將封閉輸出

if ($uniqueVar) { 
    // OUTPUT 
} else { 
    $uniquVar = true; 
} 

這對於你的代碼看起來像:

<?php foreach ($data['restos'] as $lid => $resto):?> 
    <?php 
     $time_arr = sic_get_start_expire_date_for_user_resto($this->current_user->ID, $lid); 
     $is_expired_class = ''; 
     if (isset($time_arr['expire_time']) && time()>strtotime($time_arr['expire_time'])){                  
      $is_expired_class = 'sic-expired-resto'; 
     } 
    ?> 

//---------------------------------- 
    <?php if ($notFirstIteration):?> 
//---------------------------------- 

     <?php if (!empty($data['restos_metas']['sic_restos_on']) && !empty($level['resto_image_url'])):?> 
      <div class="resto-outer-wrapper <?php echo $is_expired_class;?>"> 
       <img src="<?php echo $resto['resto_image_url'];?>" class="resto"  title="<?php echo $resto['label'];?>" /> 
      </div> 
     <?php elseif (!empty($resto['label'])):?> 
      <div class="sic-above <?php echo $is_expired_class;?>"><?php echo $resto['label'];?> 
      </div> 
     <?php endif;?> 

//-------------------------------- 
    <?php else:?> 
     <?php $notFirstIteration = true;?> 
    <?php endif;?> 
//-------------------------------- 

<?php endforeach;?> 

這將是真正的第二次迭代及以後。

+0

除了缺少php標籤,我認爲你是對的。 – cErApHiM

+0

@cErApHiM我明白你的意思,謝謝你建議編輯。我看過去了! – DallasO