Search code examples
phphtmldatabasehtml-tablerow

HTML table is adding additional row when calling information from database table


I seem to be having an issue with my HTML formatting. I am calling the database to display all information within the table on a webpage. I have got the information coming down into the table. Unfortunately the table is adding an additional row to the bottom of the table making 4 rows when in fact there is only 3 rows displaying in the database.

Any idea as to why?

<?php
//connect to the server
$link = mysql_connect('*****', '*****', '****'); 
if (!$link) { 
    die('Could not connect: ' . mysql_error()); 
}

mysql_select_db('******'); 

$query = mysql_query("SELECT * FROM tablename");

        echo "<table border='1'>
            <tr>
            <td>Date</td>
            <td>Region</td>
            <td>Cameraman</td>
            <td>Livestream?</td>
            <td>Event Title</td>
            <td>Lecturer</td>
            <td>Time</td>
            <td>Speaker</td>
            <td>ICE Contact</td>
            <td>Venue Address</td>
            <td>Venue Contact</td>
            <td>Additional Comments</td>
            <td>On App?</td>
            </tr>";

        WHILE($rows = mysql_fetch_array($query)):

            echo "<tr>";
            echo "<td>" . $rows['date'] . "</td>";
            echo "<td>" . $rows['region'] . "</td>"; 
            echo "<td>" . $rows['cameraman'] . "</td>";
            echo "<td>" . $rows['livestream'] . "</td>"; 
            echo "<td>" . $rows['eventitle'] . "</td>";
            echo "<td>" . $rows['lecturer'] . "</td>"; 
            echo "<td>" . $rows['time'] . "</td>";
            echo "<td>" . $rows['speaker'] . "</td>";
            echo "<td>" . $rows['icecontact'] . "</td>";
            echo "<td>" . $rows['venueaddress'] . "</td>"; 
            echo "<td>" . $rows['venuecontact'] . "</td>";
            echo "<td>" . $rows['additioncomments'] . "</td>";  
            echo "<td>" . $rows['onapp'] . "</td>";     
            echo "</tr>";


        endwhile;
            echo "</table>";

?>

I have added the link to the page below.

http://cpdonline.tv/spreadsheet/spreadsheet.php


Solution

  • Update your while loop with the following:

    WHILE($rows = mysql_fetch_array($query)):
                $rows = array_filter($rows);
                if (!empty($rows)) {
                    echo "<tr>";
                    echo "<td>" . $rows['date'] . "</td>";
                    echo "<td>" . $rows['region'] . "</td>"; 
                    echo "<td>" . $rows['cameraman'] . "</td>";
                    echo "<td>" . $rows['livestream'] . "</td>"; 
                    echo "<td>" . $rows['eventitle'] . "</td>";
                    echo "<td>" . $rows['lecturer'] . "</td>"; 
                    echo "<td>" . $rows['time'] . "</td>";
                    echo "<td>" . $rows['speaker'] . "</td>";
                    echo "<td>" . $rows['icecontact'] . "</td>";
                    echo "<td>" . $rows['venueaddress'] . "</td>"; 
                    echo "<td>" . $rows['venuecontact'] . "</td>";
                    echo "<td>" . $rows['additioncomments'] . "</td>";  
                    echo "<td>" . $rows['onapp'] . "</td>";     
                    echo "</tr>";
                }
                endwhile;
                echo "</table>";
    

    array_filter() function's default behavior will remove all values from array which are equal to null, 0, '' or false.