Order data retrieved in database in HTML table using foreach

I am using codeigniter. I pass the data retrieved from the database to my view. The table should display 5 lists, each column should contain a list of names generated by the foreach loop in the view. Below is my view code.

 <table class="table table-hover" >
    <thead>
    <tr>
      <th scope="col" width="20%">Cameramen</th>
      <th scope="col" width="30%">Camera Assistants</th>
      <th scope="col" width="12%">Technical Assistants</th>
      <th scope="col" width="20%">Setup Engineer</th>
      <th scope="col" width="30%">Audio Operator</th>
      <th scope="col" width="12%">Vision Operator</th>
    </tr>
    </thead>


    <tbody>
      <tr>
       <?php
        $index = 0;
        foreach($c_list as $n_key){?>

           <td><?php echo $index+1; ?>
            <?php echo $n_key->name; ?><br/><br/>
             <?php 
              $index++;
              }?>  
            </td>

        <?php
          $index = 0;
          foreach($ca_list as $n_key){?>
            <td><?php echo $index+1; ?>
              <?php echo $n_key->name; ?><br/><br/>
              <?php 
              $index++;
              }?>  
            </td>

          <?php
            $index = 0;
              foreach($ta_list as $n_key){?>
               <td><?php echo $index+1; ?>
                <?php echo $n_key->name; ?><br/><br/>
                 <?php 
                  $index++;
                  }?>  
                </td>


            <?php
              $index = 0;
              foreach($se_list as $n_key){?>
                <td><?php echo $index+1; ?>
                <?php echo $n_key->name; ?><br/><br/>
                 <?php 
                  $index++;
                  }?>  
                </td>

            <?php
              $index = 0;
              foreach($ao_list as $n_key){?>
                <td><?php echo $index+1; ?>
                  <?php echo $n_key->name; ?><br/><br/>
                  <?php 
                  $index++;
                  }?>  
                </td>

              <?php
                $index = 0;
                  foreach($vo_list as $n_key){?>
                    <td><?php echo $index+1; ?>
                    <?php echo $n_key->name; ?><br/><br/>
                     <?php 
                      $index++;
                      }?>  
                    </td>

            </tr>
          </tbody>
        </table>

But this gives the following result.

enter image description here

I want to display names row by row in each column. Can someone show me the error?

To get data from the database, I wrote 6 functions in my model. They are almost the same. Here I add one model function.

public function get_c_names($c)
{
    $cdata = array();
    foreach($c as $row) {
        $cdata[] = $row->employee_id;
    }

    $this->db->select('employee.name');
    $this->db->from('employee');
    $this->db->where('employee.id IN ('.implode(", ",$cdata).')');
    $query=$this->db->get();
    return $query->result();
}

In the controller, I call this function with the following code, and then passed for viewing as follows. All 6 functions have the same template. Therefore, I publish only 1 of them.

 $name['c_list']  = $this->employee_model->get_c_names($c);
 $this->load->view('team_view',$name);
+4
2
<table class="table table-hover" >
<thead>
<tr>
   <th scope="col" width="20%">Cameramen</th>
   <th scope="col" width="30%">Camera Assistants</th>
   <th scope="col" width="12%">Technical Assistants</th>
   <th scope="col" width="20%">Setup Engineer</th>
   <th scope="col" width="30%">Audio Operator</th>
   <th scope="col" width="12%">Vision Operator</th>
</tr>
</thead>


<tbody>


<?php

// find the longest array
$max = max(count($c_list), count($ca_list), count($ta_list), count($se_list), count($ao_list), count($vo_list));

for($x = 0 ; $x < $max ; $x++){
    echo '<tr>';
      echo '<td>'. (isset($c_list[$x])?$c_list[$x]:'') .'</td>';
      echo '<td>'. (isset($ca_list[$x])?$ca_list[$x]:'') .'</td>';
      echo '<td>'. (isset($ta_list[$x])?$ta_list[$x]:'') .'</td>';
      echo '<td>'. (isset($se_list[$x])?$se_list[$x]:'') .'</td>';
      echo '<td>'. (isset($ao_list[$x])?$ao_list[$x]:'') .'</td>';
      echo '<td>'. (isset($vo_list[$x])?$vo_list[$x]:'') .'</td>';
    echo '</tr>';
}

?>

  </tbody>
</table>

.

+1
<table class="table table-hover" >
<thead>
<tr>
  <th scope="col" width="20%">Cameramen</th>
  <th scope="col" width="30%">Camera Assistants</th>
  <th scope="col" width="12%">Technical Assistants</th>
  <th scope="col" width="20%">Setup Engineer</th>
  <th scope="col" width="30%">Audio Operator</th>
  <th scope="col" width="12%">Vision Operator</th>
</tr>
</thead>


<tbody>


  <?php
    for($x = 0 ; $x < count($c_list) ; $x++){
        echo '<tr>';
          echo '<td>'. $c_list[$x] .'</td>';
          echo '<td>'. $ca_list[$x] .'</td>';
          echo '<td>'. $ta_list[$x] .'</td>';
          echo '<td>'. $se_list[$x] .'</td>';
          echo '<td>'. $ao_list[$x] .'</td>';
          echo '<td>'. $vo_list[$x] .'</td>';
        echo '</tr>';
    }

  ?>

      </tbody>
    </table>

, .

0

Source: https://habr.com/ru/post/1660421/


All Articles