dongzhi7641 2013-10-30 04:34
浏览 30
已采纳

用于显示结果的简化php代码

Sorry if this have been asked before but I couldn't find what I wanted and I am not strong in PHP.

Right now I have this code, which is supposed to return result for different levels:

<div class="swiper-slide">
        <img src="img/B1.jpg" alt="" />
        <div class="content_container">
            <?php
                $result = mysqli_query($con,"SELECT * FROM floor_directory WHERE level='B1'");
                while($row = mysqli_fetch_array($result))
                {
            ?>
            <h1><?php echo $row['categories']; ?></h1>
            <ul class="shop_listing clearfix">
                <li class="float_left"><?php echo $row['name']; ?></li>
                <li class="float_right"><?php echo $row['unit_number']; ?></li>
            </ul>
            <?php
                }
            ?>
        </div>
      </div>
      <div class="swiper-slide">
        <img src="img/L1.jpg" alt="" />
        <div class="content_container">
            <?php
                $result = mysqli_query($con,"SELECT * FROM floor_directory WHERE level='L1'");
                while($row = mysqli_fetch_array($result))
                {
            ?>
            <h1><?php echo $row['categories']; ?></h1>
            <ul class="shop_listing clearfix">
                <li class="float_left"><?php echo $row['name']; ?></li>
                <li class="float_right"><?php echo $row['unit_number']; ?></li>
            </ul>
            <?php
                }
            ?>
        </div>
      </div> and so on...

Right now I can only duplicate it in order to fulfil the displaying of result for each individual levels. If let's say the building have 10 levels, is there a way to simplified the coding?

Hope you guys understand. Thanks in advance! =)

  • 写回答

1条回答 默认 最新

  • doubinduo3364 2013-10-30 05:50
    关注
    Try this:
    
        <?php
        $levelArray=array('L1','B1','L2','B2');
            foreach ($levelArray as $i=>$level) {
                 $data='';
                $img = "img/".$levelArray[$i];
               $result = mysqli_query($con,"SELECT * FROM floor_directory WHERE level='$levelArray[$i]'");
               while($row = mysqli_fetch_array($result)){
                $data .= '<h1>'.$row['categories'].'</h1>
                            <ul class="shop_listing clearfix">
                                <li class="float_left">'.$row['name'].'</li>
                                <li class="float_right">'.$row['unit_number'].'</li>
                            </ul>';
            }
            echo '<div class="swiper-slide">
                <img src="'.$img.'" alt="" />
                    <div class="content_container">'.$data.'</div>
              </div>'
    
            }
    
         ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)