0

i want to create an html table from a mariadb database. I dont know how many columns each Database table will end up with.

So as i dont know how many columns i have, i came up with this:

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <link rel="stylesheet" type="text/css" href="styles/global.css">
</head>
<body>

    <?php

    $con=mysqli_connect("localhost","root","","test");
// Check connection
    if (mysqli_connect_errno()){
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
//query
    $sql="SELECT * FROM news ORDER BY author";
    $result=mysqli_query($con,$sql);



    $counter = 0;
    while($row=mysqli_fetch_array($result, MYSQLI_ASSOC)){

        echo $counter;
        echo nl2br("\n");
        ?>  

        <table>
            <thead>
                <?php       
                if($counter==0){ ?>
                    <tr>
                        <th><?php echo implode("</th><th>", array_keys($row)); ?>
                        </th>
                    </tr>
                    }
            </thead>
            <tbody>
                <?php       
                if($counter>=0){ ?>
                    <tr>
                    <td><?php echo implode("</td><td>", $row); ?>
                    </td>
                    </tr>
                }
            </tbody>
        </table>

        <?php
        $counter ++;
    }

mysqli_free_result($result);
mysqli_close($con);
?>  
</body>
</html>

The problem is that the table wont fill with more than the first row.

Additionally i have get the error

Parse error: syntax error, unexpected end of file in E:\xampp\htdocs\test.php on line 58 Blockquote

The Error disappears when i enter two curly braces "}}" after the while loop, it will present the following:

execute:

I dont know why there are the curly braces in the browser as seen in the picture.

EDIT: I edit the thread because it was marked as duplicate to parsing and syntax errors, though my main problem is the creation of the table with unknown number of columns.

Any idea how to solve this? Thank you guys!

Community
  • 1
  • 1

1 Answers1

0

To be honest, I'm not sure why it was giving that error, as all your braces were closed. However, try simplifying -- as RiggsFolly said, put only what you want repeated inside the loop. It makes your logic much simpler and neater. Try this:

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <link rel="stylesheet" type="text/css" href="styles/global.css">
</head>
<body>

    <?php

    $con=mysqli_connect("localhost","root","","test");
// Check connection
    if (mysqli_connect_errno()){
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
//query
    $sql="SELECT * FROM news ORDER BY author";
    $result=mysqli_query($con,$sql);
    $row=mysqli_fetch_array($result, MYSQLI_ASSOC)
    ?>

    <table>
        <thead>
                <tr>
                    <th><?php echo implode("</th><th>", array_keys($row)); ?>
                    </th>
                </tr>
        </thead>
        <tbody>
                <tr>
                <td><?php echo implode("</td><td>", $row); ?>
                </td>
                </tr>

    <?php
    $counter = 0;
    while($row=mysqli_fetch_array($result, MYSQLI_ASSOC)){
//        echo $counter;
//        echo nl2br("\n");
        ?>  

                <tr>
                <td><?php echo implode("</td><td>", $row); ?>
                </td>
                </tr>

        <?php
        $counter ++;
    }

mysqli_free_result($result);
mysqli_close($con);
?>  
            </tbody>
        </table>
</body>
</html>

And then, check out any of the free template processors available, so you're not mixing content and code.

alanlittle
  • 460
  • 2
  • 12
  • Thank you for your help, especially pointing out to the template engines (as i am a total beginner is very nice to know). I can see one big error came from not setting the fetched array free inside the table. – Schmause R Schlurimuri Feb 03 '17 at 21:07