tags where approriate when posting code. Read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
I am trying to build a simple table to display users and the number of posts to my blog. The problem is that they are on top of each other, rather than side by side. It is creating a new <tr> instead of a new <td>.
you're asking php to create a table for every field.. although only closing the last table. Move the table and table row set up echos outside the for loop.
<?php
if (! $dbh = mysql_connect('localhost','xxxx','xxxx')) {
die("Can't connect: ".mysql_error());
}
mysql_select_db('forums');
if (! $result = mysql_query("SELECT username, posts FROM user WHERE posts >= '30' ORDER by posts DESC")) {
die("Can't execute query: ".mysql_error());
}
$rowsFound = @ mysql_num_rows($result);
echo "\n<table border=1>\n<tr>";
while ($row=mysql_fetch_array($result))
{
for ($i=0; $i<mysql_num_fields($result); $i++)
echo "\n<td>$row[$i]" . "</td>";
}
echo "\n</tr></table>\n";
mysql_close($dbh);
?>
This changes the result to bieng displayed on one line. What I want, though, is for the data pairs to be on thier own rows, but listed one after the other.
I was using that only to show you what I want to happen. When I use the $row[$i] in my code to loop over the results of the query it doesn't put them into different TD's. I'm wondering how to get the data out of the query and into separate TD's?
while ($row=mysql_fetch_array($result))
{
for ($i=0; $i<mysql_num_fields($result); $i++)
echo "\n<table border=1>\n<tr>" .
"\n<td>$row[$i]" . "</td>" .
"\n</tr>";
}
But this is not the way to put individual entries into <td>
I changed the code so that it no longer looped in the same maner and instaed of puttin the variable $i I used the column name WITHOUT double quotes in this case...