help displaying sql output in a table

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
phpstudent
Forum Newbie
Posts: 10
Joined: Wed Feb 04, 2009 11:54 pm

help displaying sql output in a table

Post by phpstudent »

Brand new to php and trying to get a page that will collect data from sql db and output in a table.

should be two columns, the first being date, second being news.... date and news should be the headers, and then all the information would display in one table. I am getting close, but for some reason, every row of data shows in its own individual table, and the titles keep repeating.

Any ideas what im doing wrong?

Code: Select all

$result_query  = mysql_query($query_ave);
while($row = mysql_fetch_assoc($result_query)){
   
echo "<table border='1'>";
echo "<tr> <th>Date</th> <th>News</th> </tr>";
    echo "<tr><td>"; 
    echo $row['date'];
    echo "</td><td>"; 
    echo $row['news'];
    echo "</td></tr>"; 
} 
echo "</table>";
 
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: help displaying sql output in a table

Post by Benjamin »

This should not be inside the while loop:

Code: Select all

 
echo "<table border='1'>";
echo "<tr> <th>Date</th> <th>News</th> </tr>";
 
phpstudent
Forum Newbie
Posts: 10
Joined: Wed Feb 04, 2009 11:54 pm

Re: help displaying sql output in a table

Post by phpstudent »

thanks astions... that worked perfectly

wish i had asked that here about an hour ago :crazy:

do you know how i could limit the news column to a certain width and have text inside wrapping?

anyways, thanks and look forward to learning here, seems like good place for it
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: help displaying sql output in a table

Post by Benjamin »

Code: Select all

 
<td style="width: 400px;">
<!-- OR -->
<td style="width: 80%;">
 
phpstudent
Forum Newbie
Posts: 10
Joined: Wed Feb 04, 2009 11:54 pm

Re: help displaying sql output in a table

Post by phpstudent »

awesome, thanks again
Post Reply