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>'
    
            }
    
         ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料