Eric
Eric

Reputation: 21

PHP - empty table appearing after first result from nested while loop

I'm getting a table appearing after the first result of the nested while loop.

What I'm trying to do is display the usernames as a list by selecting from the "users" table, then comparing that list with the project engineers from the "current projects" table.

This way there is a list of project engineers with their assigned projects underneath their names.

Unfortunately, I'm after the first nested while loop runs, it spits out a empty table and I'm not sure how to get rid of it.

I'm suspecting it has to do with the initial variable of $proj_engineer = "";

Does anyone know where this empty table is coming from and how to get rid of it?

Below is the code:

<?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>";
    }                           
?>

Upvotes: 2

Views: 219

Answers (1)

Rohit Jain
Rohit Jain

Reputation: 213243

This might be because, your query: -

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

fetched you an empty value for $proj_engineer. You can do a check before printing your table, whether your variable contains a value instead. Try printing the $proj_engineer before printing the table.

You can enclose your code echoing the table inside an if construct, which will print table only when your value is not empty.

Upvotes: 1

Related Questions