使用php将mysql数据添加为td属性
我需要这段代码给我一点不同的输出使用php将mysql数据添加为td属性,php,mysqli,Php,Mysqli,我需要这段代码给我一点不同的输出 <?php $conn=mysqli_connect("localhost","root","",""); if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } function print_tableA ($conn, $id) { $sql = "SELECT Value1, Value2,
<?php
$conn=mysqli_connect("localhost","root","","");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
function print_tableA ($conn, $id) {
$sql = "SELECT Value1, Value2, Value3, Value4, Value5, Value6, Value7, Value8 FROM mytable1";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table id='$id'>";
while($row = $result->fetch_assoc()) {
echo '<tr><td>' . join('</td><td>', $row) . "</td></tr>\n" ;
}
echo "</table>";
}
}
$result = $conn->query("SELECT Date from mytable2");
while ($row = $result->fetch_assoc()) {
print_tableA ($conn, $row['Date']);
}
?>
目前,它以以下格式打印输出:`
<tr>
<td>2</td>
....
</tr>`
2.
....
`
我需要以以下格式打印:
<tr>
<td val="2">2</td>
....
</tr>
2.
....
因此,基本上mysql中的数据也应该作为td属性重复。我希望这很简单,如果有人能告诉我需要做什么改变才能得到这个结果,我将不胜感激。谢谢 您需要遍历行值并根据需要回显td:
while($row = $result->fetch_assoc()) {
echo '<tr>';
// Let's iterate through all the columns
foreach ($row as $value) {
echo "<td val='$value'>$value</td>";
}
echo "</tr>\n";
}
while($row=$result->fetch_assoc()){
回声';
//让我们遍历所有列
foreach(行作为$value){
回显“$value”;
}
回音“\n”;
}
需要注意的是:
val
不是td
-元素的有效属性。您需要遍历行值并根据需要回显td:
while($row = $result->fetch_assoc()) {
echo '<tr>';
// Let's iterate through all the columns
foreach ($row as $value) {
echo "<td val='$value'>$value</td>";
}
echo "</tr>\n";
}
while($row=$result->fetch_assoc()){
回声';
//让我们遍历所有列
foreach(行作为$value){
回显“$value”;
}
回音“\n”;
}
需要注意的是:
val
不是td
-元素的有效属性。替换,而循环使用此元素:
while($row = $result->fetch_assoc()) {
$data = array_reduce($row, function($carry, $value) {
$carry[] = "<td val='{$value}'>{$value}</td>";
return $carry;
}, []);
echo '<tr>' . implode('', $data) . "</tr>\n" ;
}
while($row=$result->fetch_assoc()){
$data=array\u reduce($row,function($carry,$value){
$carry[]=“{$value}”;
返回$carry;
}, []);
回显“”。内爆(“”,$data)。“\n”;
}
将输出:
<tr><td val='10'>10</td><td val='11'>11</td></tr>
1011
可以找到sendbox示例
PHP函数手册。替换,而循环使用此函数:
while($row = $result->fetch_assoc()) {
$data = array_reduce($row, function($carry, $value) {
$carry[] = "<td val='{$value}'>{$value}</td>";
return $carry;
}, []);
echo '<tr>' . implode('', $data) . "</tr>\n" ;
}
while($row=$result->fetch_assoc()){
$data=array\u reduce($row,function($carry,$value){
$carry[]=“{$value}”;
返回$carry;
}, []);
回显“”。内爆(“”,$data)。“\n”;
}
将输出:
<tr><td val='10'>10</td><td val='11'>11</td></tr>
1011
可以找到sendbox示例
PHP函数手册