2016-08-03 51 views
0

所以我有我的數據庫,我必須在HTML中顯示爲表。問題是我不知道我將如何與我的th聯繫起來,並且爲沒有補貼的人添加零。inline td th使用PHP中的循環

這是我的津貼表

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

+0

例如,在表中,僅包括「服裝」值「500」,而「服裝」欄250和150的結果也包括在內!非常混合在一起的價值! –

回答

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>'; 
} 
+0

是的,謝謝,我需要這樣的輸出,但是在你的$ tdarray中你定義了服裝,餐點和運輸。我需要它是動態的,應該依賴我在數據庫中的任何東西。並與數額一起內聯。對不起,我是個完整的初學者。 – saitama

+0

@saitama我編輯了代碼,現在試試。讓我知道是否作品或如果你需要解釋。 – Pixel1002

+0

非常感謝你,這解決了我的問題。 – saitama

0

嘗試添加else與您的代碼:

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

已經試過,但它不起作用。無論如何,tnx。 – saitama