在PHP中定义HTML表

时间:2013-12-04 23:51:25

标签: php mysql html-table

我试图通过PHP从MySQL数据库中获取信息后,将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表单最终都是每行的所有条目,而不是三行到一行。

2 个答案:

答案 0 :(得分:0)

onclick属性的第一个引号是否意外地在此行中被转义?

echo '<td style="cursor:hand" onclick=\"window.location.href = \'page.php?presenter='.$value.'">',$value,'</td>';

您还在示例静态HTML和PHP示例中生成的HTML(class="list"class="db-table")之间使用了不同的CSS类。

答案 1 :(得分:0)

啊,我看到你的问题是3行而不是1行。

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事件,但会格式化表格。

祝你好运!