dongzhang0418 2010-03-18 06:29
浏览 10
已采纳

从PHP数组生成表

I'm not sure how difficult this but I have an array and would like to put it into an html table. I need to have two array strings per row, so if this were the array:

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

And I need the html table to look like this:

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

This is my 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>';  

I want to put it on two columns.

  • 写回答

5条回答 默认 最新

  • dongmouhao7438 2010-03-18 06:47
    关注

    Try this 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 "
    ";
      $val = next ( $array1 );
    }
    
    print "</table>";
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据