English 中文(简体)
我不能使用PHP和SQL在循环之间传递变量?
原标题:I cant pass variables between loops using PHP and SQL?

在下面的循环中,PHP似乎不会将输出的字符串传递回包含它的循环以进行进一步的操作,然后使该循环的输出什么都没有。

请帮我理解为什么。。。

<?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;">&nbsp;&nbsp;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> ;
最佳回答

请注意,当您启动第二个循环时,您调用的SQL语句与之前的原始语句相同

  while ($row = mysql_fetch_assoc($result)) {   //1st while
while ($row_1 = mysql_fetch_assoc($result)) {    //2nd while

这反过来意味着这个if语句将永远是真的。。。。

 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> ;  

所以你真的应该删除你的第二个while/if语句。。。因为它目前没有取得任何成就

问题回答

因为js脚本在最后引导你离开的速度太快了,你没有机会看到发生了什么!删除它,直到你按照自己喜欢的方式修复它,并确保js正是按照你的要求做的。





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

难以执行 REGEXP_SUBSTR

I m 查询Oracle 10g。 我有两张表格(样本数据见下文)。 i m 试图提取一些领域

SQL Query Shortcuts

What are some cool SQL shorthands that you know of? For example, something I learned today is you can specify to group by an index: SELECT col1, col2 FROM table GROUP BY 2 This will group by col2

PHP array callback functions for cleaning output

I have an array of output from a database. I am wondering what the cleanest way to filter the values is example array Array ( [0] => Array ( [title] => title 1 ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...

Running numbers in SQL

I have a SQL-statement like this: SELECT name FROM users WHERE deleted = 0; How can i create a result set with a running number in the first row? So the result would look like this: 1 Name_1 2 ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签