weixin_33698823 2018-01-12 15:53 采纳率: 0%
浏览 30

仅返回一个数据AJAX

Here's what i'm doing. Im trying to return a data through my database and i want the data to display in an organized manner. I need to distribute the data in different html tables based on what year or semester the data is. If you know about curriculum it's like that.

here's my algorithm. first i initialize a variable outside the while loop which is $a, if semester = 1st and year = 1 then it will go through another if which is checking if $a = 1 if it's equals it will create a div that contains part of the html tables then it will print out the data fetched from the database in td after that it will check again if $a = 1 then it will create the last part of the html tables.

To summarize. I want to distribute the data based on what semester and year the data is. But the problem is only one data is shown i have two data which equals to 1st semester and year 1.

Data enter image description here

    $a = 1;
    while($row = mysqli_fetch_array($result))
    {
        if($row['semester'] == '1st' && $row['year'] == 1){

           /*This if condition below is set to initiliaze the table headers only once*/

            if($a == 1){
                $output .='<div class="table-responsive">
                           <table class="table table-striped">
                            <thead>
                              <tr>
                                  <th class="text-center">Subject Code</th>
                                  <th class="text-center">Subject Title</th>
                                  <th class="text-center">Units</th>
                                  <th class="text-center">Pre-req</th>
                                  <th class="text-center">Co-req</th>
                              </tr>
                          </thead>
                      <tbody>';
              }

                $output .= '<tr>
                         <td class="text-center">'.$row['code'].'</td>
                         <td class="text-center">'.$row['title'].'</td>
                         <td class="text-center">'.$row['unit'].'</td>
                         <td class="text-center">'.$row['prereq'].'</td>
                         <td class="text-center">'.$row['corequisite'].'</td>
                      </tr>';


   /*This if condition below is set to initiliaze the table headers only once*/

              if($a == 1){
                $output .= '</tbody>
                            </table>
                          </div';
                $a = 2;
              }

        }

    }
   echo $output;     
  • 写回答

3条回答 默认 最新

  • weixin_33725515 2018-01-12 16:18
    关注

    the reason you get only one result is because you didn't increment the value of a $a = 1; instead of using $a=2; increment it because it skips a result and also place ending tag outside the loop

    while($row = mysqli_fetch_array($result))
        {
            if($row['semester'] == '1st' && $row['year'] == 1){
    
               /*This if condition below is set to initiliaze the table headers only once*/
    
                if($a == 1){
                    $output .='<div class="table-responsive">
                               <table class="table table-striped">
                                <thead>
                                  <tr>
                                      <th class="text-center">Subject Code</th>
                                      <th class="text-center">Subject Title</th>
                                      <th class="text-center">Units</th>
                                      <th class="text-center">Pre-req</th>
                                      <th class="text-center">Co-req</th>
                                  </tr>
                              </thead>
                          <tbody>';
                  }
    
                    $output .= '<tr>
                             <td class="text-center">'.$row['code'].'</td>
                             <td class="text-center">'.$row['title'].'</td>
                             <td class="text-center">'.$row['unit'].'</td>
                             <td class="text-center">'.$row['prereq'].'</td>
                             <td class="text-center">'.$row['corequisite'].'</td>
                          </tr>';
    
    
       /*This if condition below is set to initiliaze the table headers only once*/
    
    
    
            }
          $a++;
        }
      $output .= '</tbody></table></div';
    
       echo $output;     
    
    评论

报告相同问题?

悬赏问题

  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R