2016-08-03 17 views
0

私はこのテーブルを自分のデータベースにhtmlの表として表示する必要があります。問題は、私がどのようにして自分のtdをインラインでインライン化し、余裕のない人にゼロを追加するのか分かりません。インラインtdと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

関連する問題