make table using php data

127 views Asked by At

I have created a table using information from my database which all works fine. I have this all echo'd back in position how i want it.

My question is when i add a background to this, it adds a background to everything in every row. How can i add a background to each row with maybe 10px padding in between each background?

 echo "\n<table id=\"messageboard\" cellpadding=\"0\" cellspacing=\"0\">\n";
                echo "<tr><th width=\"150px\" style=\"text-align: center;\"></th>\n";
                echo "<th width=\"330px\" style=\"background-color: #c01718;\"></th>\n";
                echo "</tr>";;

                while($row = mysql_fetch_array($result)){

                echo "<tr><td>\n";
                echo $row ['username']."<br />".$row ['date_time'];
                                echo "<td>\n";
                echo $row ['message'];
                                echo "</td></tr>\n";                        
                                }
3

There are 3 answers

0
Bilal Abdul Kader On

You need to add the css class to the tr in order to give a style for every row

The starting row line should read

echo "<tr class=\"styledRow\"><td>\n";
0
Andrew On
0
Susheel On

your problem can be solved by to ways:

  • add style in tr tag ,and give padding to tr 10px or more

alternatively

$style = ['style1','style2']

$i = 0

while($row = mysql_fetch_array($result))

{

if {$i ==0{$i = 1}}


else {$i = 0}
                echo "<tr";
echo "class=$style[$i]";

echo "><td>\n";
                echo $row ['username']."<br />".$row ['date_time'];
                                echo "<td>\n";
                echo $row ['message'];
                                echo "</td></tr>\n";                        
                                }

now in css define two style sheet style1 and style2, your table row will have alternating style as style1 and style2,

please ignore syntax error