2016-03-26 10 views
0

Ich brauche Schleife td innerhalb während holen.Looping <td> hängt von rowCount innerhalb ab, während

Die td-Schleife hängt von der rowCount-Variablen ab der ersten Abfrage ab.

<?php include'../db/dbConnect.php'; 
$_GET['tb']; 
$t=$_GET['tb']; 
$q=$con->prepare("desc $t"); 
$q->execute(); 
$h=$q->rowCount(); ?> 
<table> 
<tr> 
    <?php while($r=$q->fetch(PDO::FETCH_NUM)){ ?> 

<th><?php echo $r[0];?></th> 

    <?php } ?> 
</tr> 

<?php 
$q=$con->prepare("select*from $t"); 
$q->execute(); 

      while($r=$q->fetch(PDO::FETCH_NUM)){ 
$d=$r[0]; 
?> 
<tr> 
<td><?php echo $d;?></td> 
</tr> 
    <?php } ?> 
</table> 

Above Code geben Ergebnis:

col1 col2 col3 
12  
13  
14 

Aber ich brauche das Ergebnis mit:

col1  col2 col3 
12  vala  x 
13  valb  y 
14  valc  z 

muss es geben td auf rowCount Variable abhängt.

Ich habe versucht, vorher td zu loopen, indem ich while, aber das bin ich nicht die Logik bekommen.

Ich schätze einige Logik oder Rat der Frage.

Antwort

0

Sie das Abrufen mehrerer Zeilen:

$r=$q->fetch(PDO::FETCH_NUM) 

Aber dann nur die erste Spalte angezeigt wird:

$d=$r[0]; 
<tr> 
    <td><?php echo $d;?></td> 
</tr> 

Probieren Sie etwas wie folgt aus:

while($r=$q->fetch(PDO::FETCH_NUM)) 
{ 
    ?> 
    <tr> 
    <?php 
     foreach($r as $colIdx => $column) 
     { 
      ?> 
      <td><?php echo $column;?></td> 
      <?php 
     } 
    <? 
    </tr> 
    <?php 
} 

NB. Nicht getestet; aber du hast die Idee.

Verwandte Themen