我有使用“ foreach”循环和“ if”条件以及来自SQL的数据的php代码。我想根据结果将结果分为两个不同的列。这是代码:
foreach($data as $row) {
if($row['Code'] == 235 && $row['PR'] == 592735 && $row['Status'] == "todo" ) {
echo "<tr><td>" . $row['Column'] . "</td>";
}
if($row['Code'] == 235 && $row['PR'] == 592705 && $row['Status'] == "todo") {
echo "<td>" . $row['Column'] . "</td>";
}
echo "</tr>";
}
我的问题是,现在所有的行都像列表一样打印在一列中,如下所示:
柱 |
---|
行(条件1) |
行(条件1) |
行(condition2) |
行(condition2) |
我想将这两个条件分为两列:
条件1 | 条件2 |
---|---|
行 | 行 |
行 | 行 |
我尝试使用html玩,但不起作用。有什么办法可以使用php来达到目的吗?顺便说一句,我是PHP入门者。
只需将您的数组分成两个数组,然后使用结果数组生成html
$condition1 = $condition2 = [];
foreach($data as $row) {
if($row['Code'] == 235 && $row['PR'] == 592735 && $row['Status'] == "todo" ) {
$condition1[] = $row['Column'];
}
if($row['Code'] == 235 && $row['PR'] == 592705 && $row['Status'] == "todo") {
$condition2[] = $row['Column'];
}
}
for($k=0; $k < max(count($condition1),count($condition2));$k++) {
echo '<tr><td>'.($condition1[$k] ?? '').'</td><td>'. ($condition2[$k] ?? '').'</td></tr>';
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句