Home Returns only one data AJAX
Reply: 3

Returns only one data AJAX

nethkennnnn
1#
nethkennnnn Published in 2018-01-12 15:53:52Z

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

    $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;     
Jeff
2#
Jeff Reply to 2018-01-12 17:01:16Z

Your problem is, that you close the table already in your first run loop, but it only should be closed at the end of the loop, and only if you opened the table at all.
It's easier to get the logic right if you use boolean variables and give them names that mean something.

$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;   
jasinth premkumar
3#
jasinth premkumar Reply to 2018-01-12 16:18:34Z

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;     
Monzur
4#
Monzur Reply to 2018-01-12 16:57:26Z
<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>
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.426839 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO