我希望将列ID添加到由foreach循环创建的表中。感谢帮助!:
echo "<h1>Table: {$table}</h1>";
echo "<table border='0'><tr>";
echo "</tr>\n";
// printing table rows
while($row = mysql_fetch_row($result))
{
echo "<tr>";
// $row is array... foreach( .. ) puts every element
// of $row to $cell variable
foreach($row as $cell)
echo "<td>$cell</td>";
echo "</tr>\n";
谢谢, 迈克
答案 0 :(得分:1)
我完全不明白你是否需要每个单元格或行或...列的ID?
对于行:
$i = 1;
while($row = mysql_fetch_row($result))
{
echo '<tr id="row-"'.$i.'">';
// $row is array... foreach( .. ) puts every element
// of $row to $cell variable
foreach($row as $cell)
echo "<td>$cell</td>";
echo "</tr>\n";
$i++;
}
对于单元格:
$i = 1;
while($row = mysql_fetch_row($result))
{
echo '<tr>';
// $row is array... foreach( .. ) puts every element
// of $row to $cell variable
foreach($row as $cell)
echo '<td id="cell-"'.$i.'">$cell</td>';
echo "</tr>\n";
$i++;
}
注意:
<强> 1 - 强>
你为什么要用这个:
echo "<table border='0'><tr>";
echo "</tr>\n";
代替:
echo "<table border='0'><tr></tr>\n";
甚至更好:
echo "<table border='0'><tr><td></td></tr>";
因为你知道你只使用一个单元格。
<强> 2 - 强>
由于你引入了一个int
作为id(好吧,通常就是这种情况......增量ID)并且html的id不能以数字开头,你必须把东西放在它前面。
答案 1 :(得分:0)
@ user547794:我不完全确定你的意思,但是如果你只想为每个单元分配一个id
,这应该可以解决问题 -
echo "<h1>Table: {$table}</h1>\n";
echo '<table cellspacing="0">' . "\n";
$i = 1;
// printing table rows
while($row = mysql_fetch_row($result))
{
echo "<tr>\n";
// $row is array... foreach( .. ) puts every element
// of $row to $cell variable
foreach($row as $cell)
{
echo '<td id="r' . $i . '">$cell</td>' . "\n";
}
echo "</tr>\n";
$i++;
}
echo "</table>\n";
答案 2 :(得分:0)
while($row = mysql_fetch_assoc($result)){
echo "<tr>";
// $row is array... foreach( .. ) puts every element
// of $row to $cell variable
foreach($row as $colName=>$colValue)
// do wathever you like here
echo "</tr>\n";
此致