gpt4 book ai didi

php - 如何根据条件在 php while 循环中填充两个不同的 html 表?

转载 作者:太空宇宙 更新时间:2023-11-03 12:20:50 24 4
gpt4 key购买 nike

我想在 php while 循环中填充两个不同的 html 表。我当前的代码在名称不为空时填充表 1($row[name])。但是,我如何声明 table2 以便它只在名称为空时填充 table2?谁能告诉我如何声明 2 个不同的表并根据 db 中的名称值是否为空来填充它们?

//table declration
echo "<table border='1'>
<tr>
<th>ID</th>
<th>name</th>
<th>page URL</th>
<th>img URL</th>
</tr>";


foreach ($lines as $line) {

//print_r( $line );

$line = rtrim($line);

$result = mysqli_query($con,"SELECT ID,name,imgUrl,imgPURL FROM testdb WHERE imgUrl like '%$line'");


if (!$result) {
die('Invalid query: ' . mysql_error());
}
//echo $result;


if(mysqli_num_rows($result)>0)
{
if(mysqli_num_rows($result)>1)
{
echo "<br>Duplicate in DB:".$line."<br>";
};

while($row = mysqli_fetch_array($result))
{
$totalRows++;



if (!empty($row[name]))
{


echo "<tr>";
echo "<td>" . $row['ID'] ."(".$totalRows. ")</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['imgPURL'] . "</td>";
echo "<td>" . $row['imgUrl'] . "</td>"; echo "</tr>";


}
else
{
$emptyNameCounter++;


//fill table2 if name is empty



};



}; // end of while loop


}
else
{
echo "<br>Not Found:".$line."<br>";

};

};// end of for each line loop

echo "</table>";

最佳答案

另一种方法是将数据附加到表一和表二的变量中,并在循环后将数据放入表中

这是一个演示,您可以如何做到这一点

$table1='';
$table2='';
while($row = mysqli_fetch_array($result))
{
$totalRows++;
if (!empty($row[name]))
{
$table1.="<tr>";
$table1.="<td>" . $row['ID'] ."(".$totalRows. ")</td>";
$table1.="<td>" . $row['name'] . "</td>";
$table1.="<td>" . $row['imgPURL'] . "</td>";
$table1.="<td>" . $row['imgUrl'] . "</td>"; echo "</tr>";

}
else
{
$emptyNameCounter++;
$table2.="data to show in table 2";
//fill table2 if name is empty
}
} // end of while loop

if(!empty($table1)){
echo "<table border='1'>
<tr>
<th>ID</th>
<th>name</th>
<th>page URL</th>
<th>img URL</th>
</tr>";
echo $table1;
echo "</table>";
}

if(!empty($table2)){
echo "<table border='1'>
<tr>
<th>ID</th>
<th>name</th>
<th>page URL</th>
<th>img URL</th>
</tr>";
echo $table2;
echo "</table>";
}

关于php - 如何根据条件在 php while 循环中填充两个不同的 html 表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20154415/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com