我有这样的循环:
Value A Value B Value C Total X - X Total X X - X Total X X - X Total X X - X Total X X - X Total X
我想要总计X,这是我的代码:
<table border="1">
<tr>
<th>Value A</th>
<th>Value B</th>
<th>Value C</th>
<th>Total</th>
</tr>
<?php
for($i = 1; $i <= 5; $i++) {
?>
<tr>
<?php
for($ii = 1; $ii <= 3; $ii++) {
$a = ($ii % 2 == 0) ? "-" : "X";
echo "<td>".$a."</td>";
}
?>
<td>Total X</td>
</tr>
<?php
}
?>
</table>
有什么花招让它与PHP结合使用?
非常感谢!
答案 0 :(得分:0)
从新代码更新:
<table border="1">
<tr>
<th>Value A</th>
<th>Value B</th>
<th>Value C</th>
<th>Total</th>
</tr>
<?php for($i = 1; $i <= 5; $i++) { ?>
<?php $total = 0; ?>
<tr>
<?php
for($ii = 1; $ii <= 3; $ii++) {
$a = ($ii % 2 == 0) ? "-" : "X";
if($a == "X") $total++;
echo "<td>".$a."</td>";
}
?>
<td><?php echo $total ?></td>
</tr>
<?php } ?>
答案 1 :(得分:0)
所有您需要做的就是将其转换为int
然后求和:
<table border="1">
<tr>
<th>Value A</th>
<th>Value B</th>
<th>Value C</th>
<th>Total</th>
</tr>
<?php
for($i = 1; $i <= 10; $i++) {
$a = ($i % 2 == 0) ? "10" : "-";
$b = ($i % 2 == 0) ? "-" : "20";
$c = ($i % 2 == 0) ? "15" : "-";
$total = (int)$a + (int)$b + (int)$c;
$res = "<tr>";
$res .= " <td>".$a."</td>";
$res .= " <td>".$b."</td>";
$res .= " <td>".$c."</td>";
$res .= " <td>Total ".$total."</td>";
$res .= "</tr>";
echo $res;
}
?>
</table>
答案 2 :(得分:0)
对不起,我的代码是这样的:
<table border="1">
<tr>
<th>Value A</th>
<th>Value B</th>
<th>Value C</th>
<th>Total</th>
</tr>
<?php
for($i = 1; $i <= 5; $i++) {
?>
<tr>
<?php
for($ii = 1; $ii <= 3; $ii++) {
$a = ($ii % 2 == 0) ? "-" : "X";
echo "<td>".$a."</td>";
}
?>
<td>Total X</td>
</tr>
<?php
}
?>
</table>
我想算X的总数 使它在PHP中发挥作用的任何技巧