2015-10-08 60 views
1

我想在一个条件中添加这个多个条件使其变得简单,下面是我的代码。我希望如果“footer_widgets_columns”的值是4,然后显示所有4列,如果3然后只显示等等,这个代码符合我的要求,但希望在一种情况下使它更紧凑,而不是一次又一次地写如何在一个条件中添加多个条件在php中

<section class="columns columns-<?php echo $data['footer_widgets_columns']; ?>"> 

       <?php if($data['footer_widgets_columns']== 4){ 
        $footer_col=3; 
       ?> 
       <article class="col-md-<?php echo $footer_col ?>"> 
       <?php 
       if (!function_exists('dynamic_sidebar') || !dynamic_sidebar('Footer Widget 1')): 
       endif; 
       ?> 
       </article> 

       <article class="col-md-<?php echo $footer_col ?>"> 
       <?php 
       if (!function_exists('dynamic_sidebar') || !dynamic_sidebar('Footer Widget 2')): 
       endif; 
       ?> 
       </article> 

       <article class="col-md-<?php echo $footer_col ?>"> 
       <?php 
       if (!function_exists('dynamic_sidebar') || !dynamic_sidebar('Footer Widget 3')): 
       endif; 
       ?> 
       </article> 

       <article class="col-md-<?php echo $footer_col ?> last"> 
       <?php 
       if (!function_exists('dynamic_sidebar') || !dynamic_sidebar('Footer Widget 4')): 
       endif; 
       ?> 
       </article> 
       <?php 
       } 
       ?> 

       <?php if($data['footer_widgets_columns']== 3){ 
        $footer_col=4; 
       ?> 
       <article class="col-md-<?php echo $footer_col ?>"> 
       <?php 
       if (!function_exists('dynamic_sidebar') || !dynamic_sidebar('Footer Widget 1')): 
       endif; 
       ?> 
       </article> 

       <article class="col-md-<?php echo $footer_col ?>"> 
       <?php 
       if (!function_exists('dynamic_sidebar') || !dynamic_sidebar('Footer Widget 2')): 
       endif; 
       ?> 
       </article> 

       <article class="col-md-<?php echo $footer_col ?>"> 
       <?php 
       if (!function_exists('dynamic_sidebar') || !dynamic_sidebar('Footer Widget 3')): 
       endif; 
       ?> 
       </article> 

       <?php 
       } 
       ?> 

       <?php if($data['footer_widgets_columns']== 2){ 
        $footer_col=6; 
       ?> 
       <article class="col-md-<?php echo $footer_col ?>"> 
       <?php 
       if (!function_exists('dynamic_sidebar') || !dynamic_sidebar('Footer Widget 1')): 
       endif; 
       ?> 
       </article> 

       <article class="col-md-<?php echo $footer_col ?>"> 
       <?php 
       if (!function_exists('dynamic_sidebar') || !dynamic_sidebar('Footer Widget 2')): 
       endif; 
       ?> 
       </article> 

       <?php 
       } 
       ?> 

       <?php if($data['footer_widgets_columns']== 1){ 
        $footer_col=12; 
       ?> 
       <article class="col-md-<?php echo $footer_col ?>"> 
       <?php 
       if (!function_exists('dynamic_sidebar') || !dynamic_sidebar('Footer Widget 1')): 
       endif; 
       ?> 
       </article> 

       <?php 
       } 
       ?> 

      </section> 
+0

'为($ I = 0; $ I <$数据[ 'footer_widgets_columns']; $ I ++){...}'是你的朋友 – Inurosen

回答

0

你可以做这样的事情

<?php 
$footer_col = 12/$data["footer_widgets_columns"]; 
for($i=1; $i <= $data["footer_widgets_columns"]; $i++) 
{ 
if($i != 4){ 
?> 
<article class="col-md-<?php echo $footer_col ?>"> 
<?php 
if (!function_exists('dynamic_sidebar') || !dynamic_sidebar('Footer Widget '.$i)): 
endif; 
?> 
</article> 
<?php 
} 
else{ 
?> 
<article class="col-md-<?php echo $footer_col ?>" last> 
<?php 
if (!function_exists('dynamic_sidebar') || !dynamic_sidebar('Footer Widget '.$i)): 
endif; 
?> 
</article> 
<?php 
} 
} 
?> 
+0

它解决了我的问题.......... gr8 –

+0

请选择正确的答案来结束您的问题。 –