Re: Need help to create HTML table with 2 columns.
| From: | Hugh Bothwell | Date: | Thu, 23 Aug 2001 19:04:31 +0000 |
| Subject: | Re: Need help to create HTML table with 2 columns. | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-64160@lists.php.net to get a copy of this message | ||
"John Bass" <php_mysql21@hotmail.com> wrote in message
news:F776KqINGuk0YemjjCA0000f589@hotmail.com...
> Hi All,
>
> I am trying to generate a table with two columns. Each column should
print
> 2 fields named image_link and web_url. Have spent over week looking
> documentations and trying different coding but nothing is working. Any
help
> will be greatly appreciated.
The other answers are generally correct but a bit hard to follow.
Try this:
<?php
// declare variables
$col = 0;
$numcols = 0;
// == fn startTable ===============
function startTable($columns) {
global $col, $numcols;
// reset variables
$col = 0;
$numcols = (int) $columns;
// start table
echo "\n<table>";
}
// === fn addColumn ==============
function addColumn($str) {
global $col, $numcols;
// first column? start row
if ($col == 0)
echo "\n\t<tr>";
// echo cell contents
echo "\n\t\t<td>$str</td>";
// last column? end row and reset
$col++;
if ($col == $numcols) {
echo "\n\t</tr>";
$col = 0;
}
}
// ==== fn endTable ===============
function endTable() {
global $col, $numcols;
// pad the table as necessary
for ($i = $col; $i < $numcols; $i++)
addColumn(' ');
// end table
echo "\n</table>";
}
// ======= MAIN =================
mysql_connect("host", "user", "pwd")
or die("Error connecting: ".mysql_error());
$query = "SELECT image_link,web_url FROM testdata";
$result = mysql_db_query("db", $query)
or die("Error querying: ".mysql_error());
startTable(2);
while ($row = mysql_fetch_array($result))
addColumn( $row["image_link"].'<br>'.$row["web_url"] );
endTable();
?>
How's that?