2

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?

7
  • can you add complete table code?? Commented Nov 17, 2015 at 9:32
  • check now updated thread Commented Nov 17, 2015 at 9:38
  • Note that i just take out the Person Header from code Commented Nov 17, 2015 at 9:43
  • how many rows will this return $this->vacancies->get_cvstate($candidate_id); Commented Nov 17, 2015 at 9:50
  • As per your image, u shud have 12 columns.. But your code has only 10... Commented Nov 17, 2015 at 9:54

2 Answers 2

1

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>
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you , this works :) Now the same thing need for State, like 1st user Can be on Cv Stage but 2nd User Can be on Interview Stage so i have to display 2 Stages in 1 td
take out the td, and do it as above, it will work.
0

At first, your code is really hard to read - please use correct indention in the future..

Second, you shouldn't print the td inside the for loop or you will get a new column for each candidate which might be not, what you want..

<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">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>
                         </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 "<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>

  <td>  <!-- NEW -->
  <?php if ($row['candidates'] !=null && count($row['candidates'])): ?>
         <?php $i = 0; ?> <!-- NEW -->
         <?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'] ): ?>
          <?php if ($i++ > 0) echo "<br />"; ?>  <!-- NEW -->
          <?php echo $t['firstname'].  " ".$t['lastname']; ?> <!-- CHANGED -->
          <?php else: ?>
            <?php echo " "; ?> <!-- CHANGED -->
        <?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> <!-- NEW -->

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

As you can see, I added a counter var $i which increments for each relevant candidate and adds a line break (instead of a new column..)

3 Comments

I have already try this one its not not working and gives me a lot of errors like Undefined variable but they are all defined
I dont testet your code, but the changes I made, wouldn't result in undefined variable warnings.. maybe you should just add the lines I changed to your code..
Sorry i forget to remove num_rows() from latest comments, but counter is not slove the problem :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.