使用PHP中的循环内联td

时间:2016-08-03 07:09:18

标签: php html web

所以我在我的数据库上有这些表,我必须在html中显示为表。问题是我不知道我将如何用我的th来内联我的td并且给那些没有津贴的人加零。

这是我的津贴表的图像

allowance table

到目前为止,这就是我所拥有的:

<?php 
  error_reporting(0);
  include "./includes/connectdb.php";

  $emp = "select * from employee";
  $q = $dbhandle->query($emp);
  if($q->num_rows>0){   

  $all = "select distinct allowance from emp_allowances order by allowance     asc";
  $a = $dbhandle->query($all);
   if($a->num_rows>0) {
    while($b = $a->fetch_assoc()){
     $thallowance .= '<th>'.$b['allowance'].'</th>';
     $empallowance = $b['allowance'];

    }

  }
    while($r = $q->fetch_assoc()){

     $id= $r['id'];
     $name= $r['name'];

     $all2 = "select * from emp_allowances order by allowance asc";
     $c = $dbhandle->query($all2);
      if($c->num_rows>0){
        $tdallow='';
        while($d = $c->fetch_assoc()){
        $empid = $d['emp_id'];
        $empallowance = $d['allowance'];
        $amount = $d['amount'];

        if($empid==$id){
            $tdallow.='<td>'.$amount.'</td>';
        }
      }
     }  
        $tbody .= '<tr>
           <td>'.$name.'</td>
              '.$tdallow.'
           </tr>';
    }
  }
     $thead = '
        <thead>
        <tr>
                <th>Name</th>
                '.$thallowance.'
            </tr>
        </thead>    
            ';
 ?>

   <table border=1> 
      <?php echo $thead; ?> 
   <tbody>
     <?php echo $tbody; ?>
  </tbody>

从这段代码中我得到了这个结果。

output image

2 个答案:

答案 0 :(得分:0)

尝试使用您的代码添加else

if($empid==$id){
     $tdallow.='<td>'.$amount.'</td>';
}else{ // add else block for print zero
     $tdallow.='<td>0</td>';
}

答案 1 :(得分:0)

我改变了你的while循环,尝试复制它,看看是否有效。

$tdarray将所有配额设置为零,如果其中一些配额具有更大的值,则在员工配额循环中设置。这样,如果没有员工津贴,则会有零和正确内联,因为默认值(零)是预设的。

如果有效,请告诉我。

<?php 
  error_reporting(0);
  include "./includes/connectdb.php";

  $emp = "select * from employee";
  $q = $dbhandle->query($emp);
  if($q->num_rows>0){   


  // array that will hold key value pairs for allowances
  $tdarray = array();
  $all = "select distinct allowance from emp_allowances order by allowance     asc";
  $a = $dbhandle->query($all);
   if($a->num_rows>0) {
    while($b = $a->fetch_assoc()){
     $thallowance .= '<th>'.$b['allowance'].'</th>';
     $empallowance = $b['allowance'];
     // fill the array with keys (allowances) and default, zero values
     $tdarray[$b['allowance']] = 0;
    }

  }

// Looping through employees
while($r = $q->fetch_assoc()){

 $id= $r['id'];
 $name= $r['name'];

 $all2 = "select * from emp_allowances order by allowance asc";
 $c = $dbhandle->query($all2);
  if($c->num_rows>0){
    $tdallow='';

    // Looping through employee allowances
    while($d = $c->fetch_assoc()){
      $empid = $d['emp_id'];
      $amount = $d['amount'];
      $empallowance = $d['allowance'];

      if($empid==$id){
        $tdarray[$empallowance] = $amount;
      }
  }
 }  
    $tbody .= '<tr>';
    $tbody .= '<td>'.$name.'</td>';
    foreach ($tdarray as $allowance => $amount) {
      $tbody .= '<td>'.$amount.'</td>';
      // reset to zero 
      $tdarray[$allowance] = 0;
    }
    $tbody .= '</tr>';
}