【问题标题】:Generating a table from a PHP array从 PHP 数组生成表
【发布时间】:2016-10-13 16:23:28
【问题描述】:

我不确定这有多难,但我有一个数组,想把它放到一个 html 表中。 我需要每行有两个数组字符串,所以如果这是数组:

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

我需要 html 表看起来像这样:

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

这是我的代码:

$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>';  

我想把它放在两列。

【问题讨论】:

  • 如果我打印 ou $record 我得到:资源 id #5 如果我打印 $vidaidi 我得到 Array ([ID] => 61 [movieno] => VpWnUkUdUA ) hmmm.. 我只收到 1数组中的行!如何提取 1 个数组中的所有数据?

标签: php arrays html-table


【解决方案1】:

试试这个代码...

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

?>

【讨论】:

    【解决方案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>';
    

    【讨论】:

      【解决方案3】:

      你可以用这样的多维数组来做到这一点: http://www.terrawebdesign.com/multidimensional.php

      除了编写自己的代码来创建 , 和标签虽然我不认为有内置的方式。

      您可以使用 print_r() 来打印数组的内容,作为查看数组的内置方式。

      【讨论】:

        【解决方案4】:

        你可以这样做:

        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>";
        

        【讨论】:

          【解决方案5】:
          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>"
          

          【讨论】:

            猜你喜欢
            • 2011-04-21
            • 2014-06-29
            • 1970-01-01
            • 1970-01-01
            • 2012-06-05
            • 2012-02-24
            • 2022-01-17
            • 2013-07-04
            • 1970-01-01
            相关资源
            最近更新 更多