2

Here is a code. This loads all the header part (i.e the header for the table) dynamically from the database.

The below code works fine. But the column is mismatched. i.e. the first row first column of the header is blank and there is a dislocation in the table.

Code

    <table border="1">
    <?php
    $book_query = mysql_query("select * from book_master");
    $i = 0;
    while($row = mysql_fetch_assoc($book_query))
    {

            $columns = array_keys($row);
            ?> 
            <th>
                <?php
                foreach($columns as $column)
                {
                ?>
                    <td><?php echo $column; ?> </td>

            </th>
            <?php 
        } 
        ?>
        <tr>
            <?php
            foreach($row as $key=>$value)
            {
                ?>
                <td><?php echo $value; ?></td>
                <?php
            }
            ?>
        </tr>
        <?php
        $i++;
    }
    ?>          
    </table>

EDIT:

Here is my print_r($columns) value:

Array ( [0] => Author Name [1] => Book Name [2] => Rating [3] => Location )

I know the problem is with the loop. Could someone help me out?

4
  • 1
    This is terrible code – you should separate data handling and output. And you are generating invalid HTML here – you are putting td elements into th elements; both belong into a tr instead and are not to be nested into each other. Commented Apr 1, 2014 at 12:55
  • CBroe - Now its working fine. I have changed the th to tr. now its okay.. yes, as you said - terrible code. ;) Commented Apr 1, 2014 at 12:58
  • The mysql extension is deprecated. New code should use mysqli or PDO, both of which have important advantages, such as support for prepared statements. Also, don't use SELECT * unless you're writing a DB administration program; select only the columns you need. Commented Apr 1, 2014 at 14:03
  • outis - Noted with thanks :) Commented Apr 2, 2014 at 5:11

3 Answers 3

2

You can try to change

         <th>
            <?php
            foreach($columns as $column)
            { ?>
                <td><?php echo $column; ?> </td>
            <?php 
            }   
            ?>
        </th>

to

        <tr>
            <?php
            foreach($columns as $column)
            { ?>
                <th><?php echo $column; ?> </th>
            <?php 
            }   
            ?>
        </tr>
Sign up to request clarification or add additional context in comments.

Comments

1

Just remove TH tag because its create one extra cell, so your layout messed up

<table border="1">
    <?php

    $book_query = mysql_query("select * from book_master");
    $i = 0;
    while($row = mysql_fetch_assoc($book_query))
    {
    if($i == 0){
    $columns = array_keys($row);
    ?> 
    <?php
    foreach($columns as $column){ ?>
    <td><?php echo $column; ?> </td>
    <?php } ?>
    <?php } ?>
    <tr>
    <?php
    foreach($row as $key=>$value){ ?>
    <td><?php echo $value; ?></td>
    <?php } ?>
    </tr>
    <?php
    $i++;
    }
    ?>          
    </table>

Comments

1

Hope this will help someone. Just I have replaced the TH tag with TR and the output is perfect.

    <table border="1">
    <?php
    $book_query = mysql_query("select * from book_master");

    while($row = mysql_fetch_assoc($book_query))
    {

    $columns = array_keys($row);
    ?> 
    <tr>
    <?php
    foreach($columns as $column){ ?>
    <td><?php echo $column; ?> </td>
    <?php } ?>
    </tr>

    <tr>
    <?php
    foreach($row as $key=>$value){ ?>
    <td><?php echo $value; ?></td>
    <?php } ?>
    </tr>


    </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.