phpmysqlarrayscodeigniterhtml-table

How to populate an HTML table from a 2d array and conditionally write multiple values in one cell


I have multidimensional array which is looks like this after loop

array(10) {     
    ["id"]=> string(3) "482" 
    ["firstname"]=> string(3) "Ian" ["lastname"]=> string(8) "Phillips" 
    ["candidate_id"]=> string(3) "482" 
    ["vacancy_id"]=> string(3) "157" 
    ["date"]=> string(19) "2015-09-08 10:12:04" 
    ["rejected"]=> string(1) "N" 
    ["rejected_step"]=> NULL 
    ["rejected_reason"]=> NULL 
    ["rejected_date"]=> NULL 
} 

array(10) {     
    ["id"]=> string(3) "692" 
    ["firstname"]=> string(7) "Gareth " 
    ["lastname"]=> string(7) "Thorne " 
    ["candidate_id"]=> string(3) "692" 
    ["vacancy_id"]=> string(3) "157" 
    ["date"]=> string(19) "2015-10-27 16:05:10" 
    ["rejected"]=> string(1) "N" 
    ["rejected_step"]=> NULL 
    ["rejected_reason"]=> NULL 
    ["rejected_date"]=> NULL 
}

That's the code where i'm using to echo

    <div class="row">  
    <div class="col-lg-12">
        <div class="box">
            <div class="box-content">
<?php
        echo '   <table class="table table-striped table-bordered table-hover">
                     <thead>
                         <tr> 
                             <th class="text-center">ID</th> 
                             <th class="text-center">Person</th> 
                             <th class="text-center">Client</th> 
                             <th class="text-center">Role</th>
                             <th class="text-center">Qualified</th>
                             <th class="text-center">Type</th>
                             <th class="text-center">Priority</th>
                             <th class="text-center">Contact</th>
                             <th class="text-center">Candidate</th>
                             <th class="text-center">Stage</th>
                             <th class="text-center">£</th>
                             <th class="text-center">Cv Send Date</th>

                         </tr>
                     </thead> 
                 <tbody>';
?>                            


<?php if($all_vacancies != null && count($all_vacancies)): ?>
 <?php 
 foreach ($all_vacancies as  $row): ?>
    <tr>
        <td><?php echo $row['id'];?></td>
        <td><?php echo $user['name'];?></td>
        <td><?php echo "<b>".$row['client_name']."</b>";?></td>
        <td><?php echo $row['title'];?></td>
        <td><?php echo $row['qualified'];?></td>
        <td><?php echo $row['type'];?></td>
        <td><?php echo $row['priority'];?></td>
        <td><?php echo $row['contact_name'];?></td>

  <?php if ($row['candidates'] !=null && count($row['candidates'])): ?>
         <?php  foreach ($row['candidates'] as $cand): ?>
        <?php             $candidate_id = $cand['candidate_id'];
                          $this->load->model("vacancies");
                          $test= $this->vacancies->get_cvstate($candidate_id);
            ?>
        <?php   $test1=isset($test) ? $test :'';
                foreach ($test1 as $t): 
            ?>
        <?php endforeach; ?>

   <?php if ($t !=null && count($t) && $t['vacancy_id'] ===$row['id'] ): ?>
          <td><?php echo $t['firstname'].  " ".$t['lastname']; ?></td>
          <?php else: ?>
            <td><?php echo " "; ?></td>
        <?php endif; ?>
        <?php endforeach; ?>
        <?php endif; ?>
              <?php if ($t['vacancy_id'] === $row['id']): ?>
              <td><?php echo "Cv Stage"?></td>
              <?php elseif ($t['candidate_id'] === $cand['candidate_id']): ?>
                <td><?php echo $row['stage'];?></td>
            <?php elseif (is_null($t['vacancy_id'])):?>
            <td><?php echo "Send Cv"; ?></td>

        <?php endif; ?>
        ?>        

        <td><?php echo '£'.$row['value'];?></td>
        <td><?php echo $row['cv_date'];?></td>

    </tr>
<?php endforeach; ?>
<?php endif; ?>

and this is how looks after echo td in foreach

enter image description here

look at ID 157 , Ian Philips and Greath Thorne should be in 1 <td> but its echo 2 times td because is in foreach loop, how i can make to echo once with values?


Solution

  • Issue is there from first line only. A small mistake in candidates part. It happens when you have more than 1 name(if you have 2, it will echo 2 <td>). So take the <td> for candidates outside. Take one more variable $name. Append the candidate names to it. Once the loop is finished, echo the td

    <?php
    $name = "";  <---- new  var
    if ($row['candidates'] != null && count($row['candidates'])):
        foreach ($row['candidates'] as $cand):
            $candidate_id = $cand['candidate_id'];
            $this->load->model("vacancies");
            $test = $this->vacancies->get_cvstate($candidate_id);
    
            $test1 = isset($test) ? $test : '';
            foreach ($test1 as $t):
    
            endforeach;
            if ($t != null && count($t) && $t['vacancy_id'] === $row['id']):
                 $name .= $t['firstname'] . " " . $t['lastname'] ." , "; 
             ?>
            <?php endif; ?>
        <?php endforeach; ?>
    <?php endif; ?>
    <td><?= $name ?></td>