0

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>";
?>

Edited by guilherme.carvalho.9250

2
Contributors
6
Replies
17
Views
4 Years
Discussion Span
Last Post by guilherme.carvalho.9250
0

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.

Edited by broj1

0

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

0

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.

Edited by broj1: Corrected code

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.