2011-05-19 52 views
0

我砍死在一起,这个代码与各如果和ELSEIF语句的,只是想知道,如果它可以被收拾(我的语法知识是垃圾!):收拾一下,如果和ELSEIF声明

而不是显示所有的HTML代码再次(因为它是相同的)有没有一种方法可以将所有elseif和if组合成一个?

if(in_array("Branding", get_field('categories')) && $grid_title == "Branding"){ 
    echo " 
     <div class=\"grid-box\" onclick=\"location.href='" . get_page_link($post->ID) ."';\" style=\"cursor: pointer;\"> 
     <div class=\"phase-1\"> 
      <img class=\"grid-image\" src=\"" . $fields->thumb_image . "\" alt=\"" . $fields->company_name ."\" height=\"152\" width=\"210\" /> 
      <div class=\"grid-heading\"> 
       <h2>". $fields->company_name ."</h2> 
       <h3>" . implode(', ',get_field('categories')) ."</h3> 
      </div> 
     </div> 
     <div class=\"phase-2\"> 
      <div class=\"grid-info\"> 
       <h4>". $fields->project_name ."</h4> 
       <p>". $fields->description ."</p> 
      </div> 
      <div class=\"grid-heading-hover\"> 
       <h2>". $fields->company_name ."</h2> 
       <h3>". implode(', ',get_field('categories')) ."</h3> 
      </div> 
     </div> 
    </div> 
    "; 
} 
elseif(in_array("Web", get_field('categories')) && $grid_title == "Web"){ 
    echo " 
     <div class=\"grid-box\" onclick=\"location.href='" . get_page_link($post->ID) ."';\" style=\"cursor: pointer;\"> 
     <div class=\"phase-1\"> 
      <img class=\"grid-image\" src=\"" . $fields->thumb_image . "\" alt=\"" . $fields->company_name ."\" height=\"152\" width=\"210\" /> 
      <div class=\"grid-heading\"> 
       <h2>". $fields->company_name ."</h2> 
       <h3>" . implode(', ',get_field('categories')) ."</h3> 
      </div> 
     </div> 
     <div class=\"phase-2\"> 
      <div class=\"grid-info\"> 
       <h4>". $fields->project_name ."</h4> 
       <p>". $fields->description ."</p> 
      </div> 
      <div class=\"grid-heading-hover\"> 
       <h2>". $fields->company_name ."</h2> 
       <h3>". implode(', ',get_field('categories')) ."</h3> 
      </div> 
     </div> 
    </div> 
    "; 

} 
else { 
    echo "hello"; 
} 
+4

这应该在代码审查。 – Bobby 2011-05-19 11:21:54

+0

两个第一选择之间的不同(不一致)是什么? – bungdito 2011-05-19 11:25:23

+0

两者的内容都是一样的,但是if略有不同。 – Rob 2011-05-19 12:09:47

回答

1

elseif与第一个if相同。所以移动状态到第一个使用OR和删除ELSEIF:

if((in_array("Branding", get_field('categories')) && $grid_title == "Branding") || (in_array("Web", get_field('categories')) && $grid_title == "Web")){ 
    echo " 
     <div class=\"grid-box\" onclick=\"location.href='" . get_page_link($post->ID) ."';\" style=\"cursor: pointer;\"> 
     <div class=\"phase-1\"> 
      <img class=\"grid-image\" src=\"" . $fields->thumb_image . "\" alt=\"" . $fields->company_name ."\" height=\"152\" width=\"210\" /> 
      <div class=\"grid-heading\"> 
       <h2>". $fields->company_name ."</h2> 
       <h3>" . implode(', ',get_field('categories')) ."</h3> 
      </div> 
     </div> 
     <div class=\"phase-2\"> 
      <div class=\"grid-info\"> 
       <h4>". $fields->project_name ."</h4> 
       <p>". $fields->description ."</p> 
      </div> 
      <div class=\"grid-heading-hover\"> 
       <h2>". $fields->company_name ."</h2> 
       <h3>". implode(', ',get_field('categories')) ."</h3> 
      </div> 
     </div> 
    </div> 
    "; 
} 
else { 
    echo "hello"; 
} 
+0

是的,那是我想到的那种东西,谢谢。 – Rob 2011-05-19 12:10:58

4

您应该考虑使用PHP的Heredoc分隔字符串。这将有助于摆脱回声和所有逃逸'\'字符。

使用PHP的if/else/elseif/endif短语句法。这使得它更容易阅读:

if(condition) : 
    //statments 
elseif(condition) : 
    //statments 
endif; 
+0

+1对于heredocs,-1对于块语法 – Alnitak 2011-05-19 11:41:01

+0

使用短语法有什么问题?我个人觉得更容易阅读 – 2011-05-19 11:43:37

1

如果我是你,我会保持HTML为纯文本,而不是一个PHP字符串:

<?php if(condition) : ?> 
    // html 
<?php elseif(condition) : ?> 
    // html 
<?php endif; ?> 

这使得它的方式更容易阅读IMO。