...

View Full Version : Tables in php, rows/columns confusion



Figurine
08-15-2008, 12:14 PM
Hi everyone!

I am hoping someone can help me with a simple php table?
I am a total beginner with php and am a little lost in this...

I have a db table with the fields 'date' (primary key), 'heading' and 'text' and I need these to be displayed so that the date and the heading are in 2 columns in the same row and the 'text' in the same column as 'heading' but in a row beneath it. This is a news table so content will be added all the time and should be displayed by date.

I am using this now:


<?

$link=mysql_connect("", "", "")
or die('Could not connect: ' . mysql_error());
echo 'Connected successfully';
mysql_select_db('Mydatabase') or die('Could not select database');

$query = 'SELECT * FROM news';
$result = mysql_query($query) or die('Query failed: ' . mysql_error());


// Printing results in HTML
echo "<table>\n";
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "\t<tr>\n";
foreach ($line as $col_value) {
echo "\t\t<td>$col_value</td>\n";
}
echo "\t</tr>\n";
}
echo "</table>\n";

// Free resultset
mysql_free_result($result);

// Closing connection
mysql_close($link);

?>


But it shows everything in the same row. I realize I need to call the variables separately and put then into the tables but I am confused as to how I would do that.

sorry if this is a crazy simple question, but help would be appreciated :)

best!
Figurine

abduraooft
08-15-2008, 12:35 PM
Try something like

while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "<tr>
<td>{$line['date']}</td>
<td>{$line['heading']}</td>
</tr>
<tr>
<td colspan=\"2\">{$line['text']}</td>
</tr>";
}

Figurine
08-15-2008, 12:58 PM
thank you once again!

You're the best Abduraooft :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum