Php 如何用多维数组填充表

Php 如何用多维数组填充表,php,mysql,arrays,codeigniter,Php,Mysql,Arrays,Codeigniter,我有一个多维数组,它看起来像这个after循环 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"]=

我有一个多维数组,它看起来像这个after循环

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


看看ID 157,Ian Philips和Greath Thorne应该在1
中,但它的回音是2次
td
,因为在foreach循环中,我如何才能使回音与值一起出现一次?

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

第二,您不应该在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
,它为每个相关的候选项递增,并添加了一个换行符(而不是一个新列)

问题仅从第一行开始。 候选人部分的一个小错误。当您有多个名称时会发生这种情况(如果您有2个名称,它将回显2)。因此,对于外部候选人,请使用
。再取一个变量
$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>


你能添加完整的表格代码吗?检查现在更新的线程注意,我刚刚从代码中取出了Person标题这将返回多少行
$this->emptance->get\u cvstate($candidate\u id)根据您的图像,u应该有12列。。但是你的代码只有10个…我已经试过了这一个它不起作用,并且给我很多错误,比如未定义的变量,但是它们都已定义我不测试你的代码,但是我所做的更改不会导致未定义的变量警告。。也许你应该把我修改过的行添加到你的代码中..对不起,我忘了从最新的注释中删除num_rows(),但counter并没有解决这个问题:(谢谢你,这很有效:)现在同样的事情需要State,像第一个用户可以在简历阶段,但第二个用户可以在面试阶段,所以我必须在1个TD2显示阶段拿出
td
,如上所述,它将工作。