将数据格式化为HTML表格

时间:2011-02-20 05:38:23

标签: php mysql html

我有一个特别有趣的问题。也许我的“不记得我最后一次睡觉”逻辑失败了......

无论如何,我正在从数据库表中获取用户列表,并希望将其粘贴到HTML表中。这很完美:

$table->construct_header( $lang->username );

while ( $user = $db->fetch_array( $user_query ) )
{
    $link = '<a href="'.$settings[ 'url' ].'/'.get_profile_link( $user[ 'uid' ] ).'" target="_blank">'.$user[ 'username' ].'</a>';
    $table->construct_cell( $link );
    $table->construct_row();
}

但是,我希望有三列。当然,我试过这个:

$table->construct_header( $lang->username );
$table->construct_header( $lang->username );
$table->construct_header( $lang->username );

while ( $user = $db->fetch_array( $user_query ) )
{
    $static $i = 1;
    if ( $i <= 3 )
    {
        $link = '<a href="'.$settings[ 'url' ].'/'.get_profile_link( $user[ 'uid' ] ).'" target="_blank">'.$user[ 'username' ].'</a>';
        $table->construct_cell( $link );
    }
    else
    {
        $table->construct_row();
        $i = 1;
    }
}

除了现在没有输出。我有没有看到一些明显的问题?我基本上需要将一个用户名插入三个连续的列(直接),然后生成该行并重新开始。

2 个答案:

答案 0 :(得分:1)

$static $i = 1;

应该是

static $i = 1;

答案 1 :(得分:1)

以下是我通常使用的基本方法:

$i = 0;
echo '<table><tbody><tr>';
while ( $user = $db->fetch_array( $user_query ) ) {
    if ($i && $i%3 == 0) echo '</tr><tr>';
    $i++;
    echo '<td><a href="'.$settings[ 'url' ].'/'.get_profile_link( $user[ 'uid' ] ).'" target="_blank">'.$user[ 'username' ].'</a></td>';
}
echo '</tr></tbody></table>';

我不熟悉MyBB的表类,但这可能会有效:

$table->construct_header( $lang->username );
$table->construct_header( $lang->username );
$table->construct_header( $lang->username );

$i = 0;
while ( $user = $db->fetch_array( $user_query ) ) {
    if ($i && $i%3 == 0) $table->construct_row();
    $i++;
    $table->construct_cell( '<a href="'.$settings[ 'url' ].'/'.get_profile_link( $user[ 'uid' ] ).'" target="_blank">'.$user[ 'username' ].'</a>' );
}
$table->construct_row();