2016-08-03 17 views
0

So habe ich diese Tabellen in meiner Datenbank, die ich als Tabelle in HTML anzeigen muss. Das Problem ist, ich weiß nicht, wie ich meine td mit meinem th inline und addiere Null zu der Person, die keine Erlaubnis hat.Inline-Td mit th Verwendung von Schleife in PHP

Dies ist das Bild meiner Toleranztabelle

allowance table.

Bisher das ist, was ich habe:

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

Und aus diesem Code habe ich dieses Ergebnis.

output image

+0

In der Tabelle zum Beispiel, die einzige "Kleidung" Wert "500", während das Ergebnis der "Kleidung" Spalte 250 und 150 ist ebenfalls enthalten! Sehr gemischt sind die Werte! –

Antwort

0

ich Ihre while-Schleife geändert hat, versuchen, es zu kopieren und wenn Werke zu sehen.

Die $tdarray setzt alle Berechtigungen auf Null, wenn einige von ihnen einen höheren Wert haben, wird dies in der Mitarbeiterzuordnungsschleife festgelegt. Auf diese Weise wird, wenn kein Mitarbeiter für einen Mitarbeiter zugelassen ist, Null und ordnungsgemäß inline angezeigt, da die Standardwerte (Nullen) voreingestellt sind.

Lassen Sie mich wissen, ob funktioniert.

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

ja danke, ich brauche eine Ausgabe wie diese, aber in Ihrem $ tdarray definieren Sie Kleidung, Essen und Transport. Ich brauche es dynamisch und sollte davon abhängig sein, was ich in meiner Datenbank habe. Und inline mit der Menge. Tut mir leid, ich bin ein kompletter Anfänger. – saitama

+0

@saitama Ich habe den Code bearbeitet, versuche es jetzt. Lassen Sie mich wissen, ob es funktioniert oder wenn Sie eine Erklärung brauchen. – Pixel1002

+0

Vielen Dank, das löst mein Problem. – saitama

0

versuchen Sie else mit Ihrem Code:

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

versucht dies bereits, aber es funktioniert nicht. Tnx sowieso. – saitama