php - 在 PHP 中定义 HTML 表

标签 php mysql html-table

在从 MySQL 数据库获取信息后,我试图通过 PHP 将 HTML 表输出转换为特定格式。

在 HTML 中,我可以这样做:

<table cellpadding="0" cellspacing="0" class="list">
    <tr>
        <td style="cursor:hand" onclick="window.location.href = 'page.php?presenter=Name1'">Name1</td>
        <td style="cursor:hand" onclick="window.location.href = 'page.php?presenter=Name2'">Name2</td>
        <td style="cursor:hand" onclick="window.location.href = 'page.php?presenter=Name3'">Name3</td>
    </tr>
    <tr>
        <td style="cursor:hand" onclick="window.location.href = 'page.php?presenter=Name4'">Name4</td>
        <td style="cursor:hand" onclick="window.location.href = 'page.php?presenter=Name5'">Name5</td>
        <td style="cursor:hand" onclick="window.location.href = 'page.php?presenter=Name6'">Name6</td>
</table>

这很好用,看起来也不错,我已经将它进行了 DIV 和格式化。

但是,我需要通过从 MySQL 中提取名称,然后在需要时创建一个新单元格,然后在 3 个单元格之后创建一个新行来完成此操作。

到目前为止,我有这个:

<?php
//PRESENTERS HERE
/* connect to the db */
$connection = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db($db, $connection);

/* show tables */
$result = mysql_query('SHOW TABLES', $connection) or die('cannot show tables');
echo '<h1>Presenters:</h1>';
while ($tableName = mysql_fetch_row($result))
{
    $table = 'presenters';

    /* Get the presenters*/
    $result2 = mysql_query('SELECT presenter FROM ' . $table) or die('cannot show data from ' . $table);
}

if (mysql_num_rows($result2))
{
    echo '<table cellpadding="0" cellspacing="0" class="list">';

    while ($row1 = mysql_fetch_row($result2))
    {
        echo '<tr>';
        foreach ($row1 as $key => $value)
        {
            echo '<td style="cursor:hand" onclick=\"window.location.href = \'page.php?presenter=' . $value . '">', $value, '</td>';
        }
        echo '</tr>';
    }
    echo '</table><br />';
}
?>

但我无法弄清楚如何让它表现得像上面的 HTML 那样。我也无法让点击事件起作用。

PHP 表单最终将所有条目各占一行,而不是一行三个。

最佳答案

啊啊,我发现您的问题是 3 行而不是一行的条目。

while($row1 = mysql_fetch_row($result2)) {
    echo '<tr>';
    foreach($row1 as $key=>$value) {
        echo '<td style="cursor:hand" onclick=\"window.location.href = \'page.php?presenter='.$value.'">',$value,'</td>';
    }
    echo '</tr>';
}
echo '</table><br />';
}

因为每次从数据库中检索某些内容(while 循环语句)时,您都会打开和关闭表中的行。

您需要在 while 循环之外启动一个计数器,用于计算您拥有的行数,当您达到所需的列数时,重置计数器并关闭表格标签。

类似这样的事情:

$columncount = 0;
while($row1 = mysql_fetch_row($result2)){
    if($columncount == 0){
        echo "<tr>";
    }

    foreach($row1 as $key=>$value) {
        echo '<td style="cursor:hand" onclick=\"window.location.href = \'page.php?presenter='.$value.'">',$value,'</td>';
    }

    $coloumncount ++;
    //assuming you want 3 columns in your table
    if($columncount == 3) {
        echo "</tr>";
        $coloumncount = 0;
    }                
}

不会解决您的 onclick 事件,但会格式化表格。

祝你好运!

关于php - 在 PHP 中定义 HTML 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20388613/

相关文章:

php - 简单的ajax POST不执行php函数

php - move_uploaded_file 未上传图像

mysql - bash 脚本中的未知选项 '--print-defaults'

php - 在 PHP 中动态生成具有特定行跨度的表?

php - 新表列,用于每个 echo'd 复选框(php、mysql)

php - 用于替换 html 代码中的 css 类的正则表达式

php - password_verify 哈希值与密码不匹配

mysql - 文本和自动递增数字一起出现在mysql的一个字段中

Mysql 陌生人语法错误 #1064

android - 单击 Samsung Galaxy Tab 9.0 版 WebView 中的 SELECT 标记会使应用程序崩溃