Hello to everyone, im having some trouble in setting a position on a table in php. I learn how to display a table in php on school and with some research through the internet, but not how to customize it. Is it possible to display a mysql table from a database on a html table? Is there another way to do this? Heres my code :

<?php
include"connect_database.php";
$result = mysql_query("SELECT * from despesas_casa");

echo "<table border='1'><tr><th>Payment</th><th>Date of payment</th><th>Payment</th><th>Date of payment</th><th>Payment</th><th>Date of payment</th><th>Payment</th><th>Date of payment</th></tr>";

while ($row = mysql_fetch_array($result)){
    echo "<tr>";
    for($i=1;$i<9;$i++){
        echo "<td>".$row[$i]."</td>";
    }
    echo '<td><a href="apagar.php?id_dados='.$row['id_despesa_casa'].'">DELETE</A>';
    echo '<td><a href="editar.php?id_dados='.$row['id_despesa_casa'].'">EDIT</A>';
    echo "</tr>";
}
echo "</table>";
?>

Your approach is completely OK, I think most of people do it this way. You start a html table, display a header row and then loop through the resultset from a mysql query and display each row, adding a delete and edit links. The only thing that could be done in a safer way is the for loop which would be better if it was done with a foreach loop:

while ($row = mysql_fetch_array($result)){
    echo "<tr>";
    foreach($row as $field){
        echo "<td>".$field."</td>";
    }
    echo '<td><a href="apagar.php?id_dados='.$row['id_despesa_casa'].'">DELETE</A>';
    echo '<td><a href="editar.php?id_dados='.$row['id_despesa_casa'].'">EDIT</A>';
    echo "</tr>";
}

Why is the foreach loop safer? Well, mainly because you do not have to worry about how many fields you have in one row. The foreach will loop through all of them. When you use a for loop you have to know the number of fields so you can set the iterration count (from 1 to 8 in your case) which can lead to errors.

Hello broj1 thanks for replying and helping. About the foreach loop your right, but for some reason my table gets unformatted :(

Unformated in what way? What is the result you would like to achieve?

I would like to set a position, put the table on the center of the page, thats what i cant do.

This has nothing to do with a type of for loop you use. The best thing to do is to use a CSS. To get table positioned in the midle just set left and right margins to auto:

echo "<table border='1' style='margin: 0 auto;' ><tr><th>Payment</th><th>Date of payment</th><th>Payment</th><th>Date of payment</th><th>Payment</th><th>Date of payment</th><th>Payment</th><th>Date of payment</th></tr>";

while ($row = mysql_fetch_array($result)){
    echo "<tr>";
    for($i=1;$i<9;$i++){
        echo "<td>".$row[$i]."</td>";
    }
    echo '<td><a href="apagar.php?id_dados='.$row['id_despesa_casa'].'">DELETE</A>';
    echo '<td><a href="editar.php?id_dados='.$row['id_despesa_casa'].'">EDIT</A>';
    echo "</tr>";
}
echo "</table>";

You could also do it in an external stylesheet which is usually recommended.

ok im going to investigate more about CSS and external stylesheet, thankyou for helping me with this

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.