2010-03-18 3 views
1

Je ne suis pas sûr de savoir à quel point cela est difficile, mais j'ai un tableau et je voudrais le mettre dans une table html. je dois avoir deux chaînes de tableau par ligne, donc si tel était le tableau:Générer une table à partir d'un tableau PHP

$array1 = array(
    1 => 'one', 
    2 => 'two', 
    3 => 'three', 
    4 => 'four', 
    5 => "five", 
    6 => 'six', 
    ); 

Et je besoin du tableau html pour ressembler à ceci:

| one | two | 
|three| four | 
|five | six | 

Voici mon code:

$db = new Database(DB_SERVER, DB_USER, DB_PASS, DB_DATABASE); 
$db->connect(); 

    $sql = "SELECT ID, movieno 
      FROM movies 
      ORDER BY ID DESC 
      LIMIT 6 "; 

    $rows = $db->query($sql); 

    print '<table width="307" border="0" cellspacing="5" cellpadding="4">'; 
    while ($record = $db->fetch_array($rows)) { 
     $vidaidi = $record['movieno']; 
     print <<<END 
     <tr> 
      <td> 
       <a href="http://www.youtube.com/watch?v=$vidaidi" target="_blank"> 

       <img src="http://img.youtube.com/vi/$vidaidi/1.jpg" width="123" height="80"></a> 
      </td> 
     </tr> 
    END; 
    } 
    print '</table>'; 

Je veux le mettre sur deux colonnes.

+0

si j'imprimer OU $ record i get: Resource id # 5 si je print $ vidaidi je reçois Array ([ID] => 61 [movieno] => VpWnUkUdUA) hmmm .. je recive seulement 1 rangée dans le tableau! comment extraire toutes les données dans 1 tableau? – robertdd

Répondre

2

Essayez ce code ...

<?php 

$array1 = array(
       1 => 'one', 
       2 => 'two', 
       3 => 'three', 
       4 => 'four', 
       5 => "five", 
       6 => 'six', 
       ); 

$val = current ($array1) ; 
print "<table border=1>"; 
while ($val) 
{ 
    print "<tr> <td> $val </td> "; 
    $val = next ($array1) ; 
    print "<td> $val </td> </tr> "; 
    print "\n"; 
    $val = next ($array1); 
} 

print "</table>"; 

?> 
0

Vous pouvez le faire avec un tableau multidimensionnel comme celui-ci: http://www.terrawebdesign.com/multidimensional.php

En plus d'écrire votre propre code pour créer et balises bien que je ne pense pas qu'il y ait construit dans le chemin. La fonction print_r() permet d'imprimer le contenu du tableau en tant que manière intégrée de visualiser le tableau.

2
$array1 = array(
    1 => 'one', 
    2 => 'two', 
    3 => 'three', 
    4 => 'four', 
    5 => "five", 
    6 => 'six', 
    ); 

echo '<table>'; 
for ($i = 1; $i <= 7; $i++) { 
    if ($i % 2 == 1) echo '<tr>'; 
    echo "<td>{$array1[$i]}</td>"; 
    if ($i % 2 == 2) echo '</tr>'; 
} 
echo '</table>'; 
0
echo "<table>"; 
for($i=0;$i+=2;$i<count($array1)) 
{ 
    echo "<tr><td>".$array1[$i]."</td><td>".isset($array1[$i+1])?$array1[$i+1]:'no value'."</td></tr>"; 
} 
echo "</table>" 
0

Vous pouvez faire quelque chose comme:

print "<table>"; 
for($i=1;$i<=count($arr);$i++) { 
     if($i%2) 
       print"<tr><td>$arr[$i]</td>"; 
     else 
       print"<td>$arr[$i]</td></tr>\n"; 
} 
print "</table>"; 
Questions connexes