doukezi4606 2016-07-15 12:54
浏览 99
已采纳

循环结果为三列

I have following HTML code and I need make results from db, but I don't know how. There are three static divs. I don't know how to end static div and how to recognize columns. Can you help me please? Please see structure of html code with numbers of results. Thank for help.

 <div class="static-div">
     <div class="first-div">1</div>
     <div class="second-div">4</div>
     <div class="third-div">7</div>

     <div class="first-div">10</div>
     <div class="second-div">13</div>
     <div class="third-div">16</div>

     <div class="first-div">19</div>
     <div class="second-div">22</div>
     <div class="third-div">25</div>
  </div>

  <div class="static-div">
     <div class="first-div">2</div>
     <div class="second-div">5</div>
     <div class="third-div">8</div>

     <div class="first-div">11</div>
     <div class="second-div">14</div>
     <div class="third-div">17</div>

     <div class="first-div">20</div>
     <div class="second-div">23</div>
     <div class="third-div">26</div>
  </div>

  <div class="static-div">
     <div class="first-div">3</div>
     <div class="second-div">6</div>
     <div class="third-div">9</div>

     <div class="first-div">12</div>
     <div class="second-div">15</div>
     <div class="third-div">18</div>

     <div class="first-div">21</div>
     <div class="second-div">24</div>
     <div class="third-div">27</div>
  </div>
  • 写回答

1条回答 默认 最新

  • doushou9028 2016-07-15 13:18
    关注

    You can achieve this output by using Arrays.

    we will create three arrays for given three static divs.

    try the following code.

    The variable $i is not for loop iteration so if you are using $i for iteration please replace the following $i with other name.

    $i=1;
    $first_arr=array();
    $sec_arr=array();
    $third_arr=array();
    while(database loop condition)
    {
         if($i==1)
         {
            $val=' <div class="first-div">'.$row['column'].'</div>';//$val is a variable from database query result
            array_push($first_arr,$val);
            $i++;
         }
        elseif($i==2)
         {
            $val=' <div class="second-div">'.$row['column'].'</div>';//$val is a variable from database query result
            array_push($sec_arr,$val);
            $i++;
         }
         elseif($i==3)
         {
            $val=' <div class="second-div">'.$row['column'].'</div>';//$val is a variable from database query result
            array_push($sec_arr,$val);
            $i=1;
         }
    }
    $first_div=implode('',$first_arr);
    $sec_div=implode('',$sec_arr);
    $third_div=implode('',$third_arr);
    echo '<div class="static-div">'.$first_div.'</div>
    <div class="static-div">'.$sec_div.'</div>
    <div class="static-div">'.$third_div.'</div>';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?