PHP - 在嵌套while循环的第一个结果之后出现的空表

时间:2012-10-20 08:07:26

标签: php mysql nested-loops

我在嵌套while循环的第一个结果后出现了一个表。

我要做的是通过从“users”表中选择,然后将该列表与“当前项目”表中的项目工程师进行比较,将用户名显示为列表。

这样就有一个项目工程师列表,他们的名字下面有他们指定的项目。

不幸的是,我在第一次嵌套的while循环运行之后,它会吐出一个空表,我不知道如何摆脱它。

我怀疑它与$ proj_engineer =“”的初始变量有关;

有谁知道这个空表的来源以及如何摆脱它?

以下是代码:

<?php
    $query = mysql_query("SELECT * FROM `users` ORDER BY `username` ASC") or die(mysql_error());
    while ($row = mysql_fetch_assoc($query)) {
        $user_id = $row['user_id']; 
        $username = $row['username'];                               

        $proj_engineer = "";

        $query1 = mysql_query("SELECT * FROM `current_projects` WHERE `proj_engineer`='$username' ORDER BY `proj_engineer` ASC") or die(mysql_error());
        while ($row1 = mysql_fetch_assoc($query1)) {
            $proj_id = $row1['proj_id']; 
            $proj_engineer = $row1['proj_engineer'];
        }

        echo "<table border=1><tr><td colspan=12><p class='bold18'>" . $proj_engineer . "</p></td></tr>";                           


        $query3 = mysql_query("SELECT * FROM `current_projects` WHERE `proj_engineer`='$username' ORDER BY `proj_id` DESC") or die(mysql_error());
        while ($row3 = mysql_fetch_assoc($query3)) {
            $proj_id = $row3['proj_id'];
            $proj_number = $row3['proj_number']; 
            $proj_name = $row3['proj_name'];
            $proj_sort = $row3['proj_sort'];
            $proj_start = $row3['proj_start'];
            $proj_finish = $row3['proj_finish'];
            $proj_overstat = $row3['proj_overstat'];
            $proj_dwgstat = $row3['proj_dwgstat']; 
            $proj_soostat = $row3['proj_soostat']; 
            $proj_substat = $row3['proj_substat']; 
            $proj_engineer = $row3['proj_engineer'];
            $proj_drafter = $row3['proj_drafter'];
            $proj_rating = $row3['proj_rating'];
            $proj_pending = $row3['proj_pending'];
            $proj_notes = $row3['proj_notes'];

            $start_time = date("m/d/y", $proj_start);
            $finish_time = date("m/d/y", $proj_finish);

            echo "

                <tr>
                    <td width=40>$proj_number</td>
                    <td width=100>$proj_engineer</td>
                    <td width=100><a href='./project-page.php?proj_id=$proj_id'>$proj_name</a></td>
                    <td width=40>$start_time</td>
                    <td width=40>$finish_time</td>
                    <td width=110>
                        <div style='position:relative; background:url(images/bar01.gif); width:$proj_overstat; height:20;'>
                            <div style='position:absolute; bottom:0; left:0; width:$proj_overstat; font-weight:bold; color:#000000; vertical-align:middle; height:20; text-align:center;'>$proj_overstat%</div>
                        </div>
                    </td>
                    <td width=110>
                        <div style='position:relative; background:url(images/bar02.gif); width:$proj_dwgstat; height:20;'>
                            <div style='position:absolute; bottom:0; left:0; width:$proj_dwgstat; font-weight:bold; color:#000000; vertical-align:middle; height:20; text-align:center;'>$proj_dwgstat%</div>
                        </div>
                    </td>
                    <td width=110>
                        <div style='position:relative; background:url(images/bar03.gif); width:$proj_soostat; height:20;'>
                            <div style='position:absolute; bottom:0; left:0; width:$proj_soostat; font-weight:bold; color:#000000; vertical-align:middle; height:20; text-align:center;'>$proj_soostat%</div>
                        </div>
                    </td>
                    <td width=110>
                        <div style='position:relative; background:url(images/bar04.gif); width:$proj_substat; height:20;'>
                            <div style='position:absolute; bottom:0; left:0; width:$proj_substat; font-weight:bold; color:#000000; vertical-align:middle; height:20; text-align:center;'>$proj_substat%</div>
                        </div>
                    </td>
                    <td width=40 align='center'><a href='project-notes.php?proj_id=$proj_id'><img src='images/note.png' border=0></a></td>
                    <td width=40 align='center'><a href='./project-edit.php?proj_id=$proj_id'>EDIT</a></td>
                    <td width=40 align='center'><a href='./project-delete.php?proj_id=$proj_id'>DELETE</a></td>
                </tr>

            ";
        }
        echo "</table><br>";
    }                           
?>

1 个答案:

答案 0 :(得分:1)

这可能是因为您的查询: -

$query1 = mysql_query("SELECT * FROM `current_projects` WHERE 
                      `proj_engineer`='$username' ORDER BY `proj_engineer` ASC") 
                       or die(mysql_error());

$proj_engineer提取了一个空值。您可以在打印表格之前进行检查,无论您的变量是否包含值。在打印$proj_engineer之前尝试打印table

您可以将代码echoing括在if构造中,只有当您的值不为空时才会打印表格。