如何使用多维数组填充表

时间:2015-11-17 09:27:36

标签: php mysql arrays codeigniter

我有多维数组,在循环

之后看起来像这样
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 
}

这是我用来回应

的代码
    <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; ?>

这就是如何照顾foreach中的echo td

enter image description here

看看ID 157,Ian飞利浦和Greath Thorne应该在1 <td>,但它的回声是td 2倍因为在foreach循环中,我怎么能用值回显一次?

2 个答案:

答案 0 :(得分:1)

问题仅来自第一行。 候选人中的一个小错误。如果您有多个名称,则会发生这种情况(如果您有2个名称,则会回显2 <td>)。因此,请<td>以外的候选人。再取一个变量$name。将候选人名称附加到其中。循环结束后,回显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>

答案 1 :(得分:0)

首先,您的代码真的难以阅读 - 请在将来使用正确的缩进..

其次,您不应该在for循环中打印td,否则您将为每个候选人获得一个新列,可能不是,您想要的......

<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; ?>

正如您所看到的,我添加了一个计数器var $i,它为每个相关候选者递增并添加换行符(而不是新列...)