我不能使用PHP和SQL在循环之间传递变量?
16lz
2021-01-22
In the loops below, PHP doesn't seem to pass the outputted string back out to the loop that contains it for further manipulation, then making the output from this loop nothing at all.
在下面的循环中,PHP似乎没有将输出的字符串传递回包含它的循环以进行进一步操作,然后从这个循环输出什么都没有。
Please help me understand why this is...
请帮我理解为什么这是......
<?php
$finalTablePrint = '<html><body style="font-family:Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size:40px;">
LTU Activity Report<br/>
<span style="font-size:18px;"> Date Created: ' . date("l jS \of F Y") . '<br/><br/>
<table width=100% style="background-color:white; font-size:12px; border-collapse:collapse;" >
<tr style="font-size:12px; font-weight:600; background-color:#d2dbdf; height:35px;">
<td>Activity</td><td>Department</td><td>Hours Spent</td><td>Month</td>
</tr>';
while ($row = mysql_fetch_assoc($result)) {
$finalTablePrint .= '<tr><td colspan=4>' . $row['activity'] . '</td></tr>';
while ($row_1 = mysql_fetch_assoc($result)) {
if ($row_1['activity'] == $row['activity'] && $row_1['department'] == $row['department']) {
$finalTablePrint .= '<tr style="height:30px;"><td colspan=3>' . $row['department'] . '</td>' . '<td>' . $row['hours'] . '</td><td>' . $row['month'] . '</td></tr>';
}
}
}
echo $finalTablePrint .='</table><script type="text/javascript">javascript:window.print(); setTimeout(\'window.location="../../admin/index.php"\',"1000");</script></body></html>';
2 个解决方案
#1
1
Note that when you set off your second loop, you are calling the same SQL statement as the original one before it
请注意,当您启动第二个循环时,您将调用与之前的原始SQL语句相同的SQL语句
while ($row = mysql_fetch_assoc($result)) { //1st while
while ($row_1 = mysql_fetch_assoc($result)) { //2nd while
which in turn means that this if statement will allways be true....
这反过来意味着这个if语句总是真实的......
if ($row_1['activity'] == $row['activity'] && $row_1['department'] == $row['department']) {
and this will allways echo:
这将总是回应:
$finalTablePrint .= '<tr style="height:30px;"><td colspan=3>'.$row['department'].'</td>'.'<td>'.$row['hours'].'</td><td>'.$row['month'].'</td></tr>';
so you should really remove your 2nd while/if statement... since it achieves nothing at the moment
所以你应该删除你的第二个/ if语句...因为它目前没有任何结果
更多相关文章
- 如何生成字符串'a'..'zzz'然后将它们保存到表中?
- 使用navicat for mysql 将查询语句过滤的结果导出成insert语句时
- oracle新接触的sql语句
- SQL当执行插入操作时,字符串含有''的时候的处理
- 求查询成绩表中两门科成绩90分以上的学生学号的SQL语句?
- mysql常用命令/语句学习三
- StringBuilder 拼接sql语句比较快
- 求一SQL语句(如何按某列的值分组且取出每组前几行的数据)
- 1)如何用语句来查看一个表内是否建了索引2)或用plsql查看一个表