切换到IN?不确定这是怎么做的

时间:2017-02-27 10:16:11

标签: php while-loop

<?php 
$x = 1;

while($x <= 17) {

switch($x) {
    case 1: $sdg_text = "End poverty in all its forms everywhere";
    case 2: $sdg_text = 'End hunger, achieve food security and improved nutrition and promote sustainable agriculture';
    case 3: $sdg_text = "Ensure healthy lives and promote well-being for all at all ages";
    default: echo '';
} 
?>
    <div class='grid-20 mobile-grid-100'>
        <ul class="img-list">
            <li>
                <a>
                    <img src="<?php echo get_template_directory_uri(); ?>/img/sdg/sdg<?php echo $x?>.svg" />
                    <span class="text-content"><span><?php echo $sdg_text; ?></span></span>
                </a>
            </li>
        </ul>
    </div> 
<?php $x++ ?>
<?php } //End of While ?>

嘿大家,

我目前有一组图像列为SDG1,SDG2 ... SDG17,每张图像都有你悬停的文字。我正在复制sustainable development in the UN website.

我不是一遍又一遍地复制div,而是认为把它放在while循环中并且在while循环中更容易,有一个开关循环,它会根据案例回显文本。现在,它只会回显默认值。

提前谢谢。

2 个答案:

答案 0 :(得分:0)

你应该在每个案例后添加一个适当的休息时间

switch($x) {
case 1: 
   $sdg_text = "End poverty in all its forms everywhere";
   break;
case 2:
   $sdg_text = 'End hunger, achieve food security and improved nutrition and promote sustainable agriculture';
   break;
case 3: 
  $sdg_text = "Ensure healthy lives and promote well-being for all at all ages";
  break;
default: echo '';
 break;
} 

答案 1 :(得分:0)

实际上它只是一个简单的错误。问题是你不使用break;。在每个案例结束时(默认情况下),您必须添加break;

  

如果你不在案件陈述的末尾写一个break语句   列表,PHP将继续执行以下案例的陈述。