只返回一个数据AJAX

时间:2018-01-12 15:53:52

标签: php jquery html ajax

这就是我正在做的事情。我试图通过我的数据库返回data,我希望data以有组织的方式显示。我需要根据数据的html tablesyear以不同的semester分发数据。如果你了解课程就像那样。

这是我的算法。首先,如果variable = while loop$a =,则semester在<{>> 1st 之外初始化year 1然后它将通过另一个检查是否 $a = 1如果它等于它会创建一个div,其中包含部分{{1}然后它将打印出html tables中从data获取的td,之后它会再次检查 $a = 1然后它会创建最后一个html tables的一部分。

总结一下。我想根据数据的学期和年份分发数据。但问题是只显示了一个数据,我有两个数据等于1st semester and year 1

数据 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 个答案:

答案 0 :(得分:2)

您的问题是,您已经在第一个运行循环中关闭了表,但它只应在循环结束时关闭,并且只有在您打开表时才会关闭。
如果你使用布尔变量并给它们起名称意思,那么更容易获得正确的逻辑。

$hasFoundAtLeastOneRow = false;
$firstRow = true;
$output = ''; // if you haven't above, you need to initialize that string before you can do a '.='
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($firstRow){
              $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>';
        $hasFoundAtLeastOneRow = true;   // we have at least one entry in our table
        $firstRow = false;  // set this to false, since we just worked the first row
    }

}

if($hasFoundAtLeastOneRow) {
    $output .= '</tbody>
              </table>
            </div';
} else {
    // optional
    $output = "nothing found";
}
echo $output;   

答案 1 :(得分:0)

您只获得一个结果的原因是因为您没有增加$a = 1;的值而不是使用$a=2;增加它,因为它会跳过结果并将结束标记放在环

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;     

答案 2 :(得分:-1)

<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>
<?php   
    while($row = mysqli_fetch_array($result))
    {
        if($row['semester'] == '1st' && $row['year'] == 1)
        {
?>      
            <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>
<?php
        }
    }
?>  
        </tbody>
    </table>
</div>