0

I retrieved a list of data from an SQL database and now I would like to display it in a neat table rather than in a list. I managed to find a way to do this (probably not very elegant, though), but the column headers seem to be offset and I have not idea how to fix this.

enter image description here

I'm completely new to PHP, so any hints on how to solve this will be much appreciated!

    echo '<table>';
    echo '<tr>';
    echo '<th>';
    echo '<td>Word</td>';
    echo '<td>Frequency</td>';
    echo '</th>';
    echo '</tr>';
    $response = $db->query("SELECT * FROM frequencies WHERE freq BETWEEN 900 AND 910 ORDER BY freq");

    while ($row = $response->fetch())
    {
        echo '<tr>';
        echo '<td>'.$row['word'].'</td>';
        echo '<td>'.$row['freq'].'</td>';
        echo '</tr>';
    }
    echo '</table>';
    $response->closeCursor();   

2 Answers 2

1

A <th> element is a table header element and should be used instead of <td> (table data) element in your header row - it should never be a wrapper around <td> elements.

echo '<table>';
echo '<tr>';
echo '<th>Word</th>';
echo '<th>Frequency</th>';
echo '</tr>';
Sign up to request clarification or add additional context in comments.

Comments

0

I prefer combining php and html

<table >
    <thead>
        <tr>
            <th >Word</th>
            <th >Frequency</th>   
        </tr>
    </thead>
        <?php
        $response = $db->query("SELECT * FROM frequencies WHERE freq 
        BETWEEN 900 AND 910 ORDER BY freq");

        ?>
    <tbody>
        <?php  
 while ( $row = $response->fetch()) { 
         ?>
           <tr>

            <td><?php echo $row['word']; ?></td>
            <td><?php echo $row['freq']; ?></td>

         </tr>
          <?php } 

            $response->closeCursor();

           ?>
    </tbody>
</table>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.