Re: Table formatting
| From: | Tech Support | Date: | Fri, 26 Jul 2002 22:23:08 +0000 |
| Subject: | Re: Table formatting | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-109894@lists.php.net to get a copy of this message | ||
Here is some working sample code that you can build from.
<?
$columns = 3;
$pretend_row = array(
"test",
"test2",
"testing",
"test4",
"testagain",
"hello"
);
$table = "<table border=\"1\">\n<tr>\n";
$count = 1;
foreach ($pretend_row as $row)
{
$table .= "<td>$row</td>\n";
if ($count % $columns === 0)
$table .= "</tr><tr>\n";
$count++;
}
$table = ereg_replace("<tr>\n$", "\n", $table);
$table .= "</table>\n";
print $table;
?>
Jim Grill
Support
Web-1 Hosting
http://www.web-1hosting.net
----- Original Message -----
From: "César Aracena" <icaam@icaam.com.ar>
To: "PHP General List" <php-general@lists.php.net>
Sent: Friday, July 26, 2002 5:05 PM
Subject: [PHP] Table formatting
Hi all.
Last nite I've came across a problem I wasn't able to figure out by my
self. It's not difficult to make a loop that will make new *TABLE ROWS*
(<tr>) to show several DB objects in a nice way. what I need to do, is
to display 2 or maybe even 3 of this objects stored in a DB per table
row, separated in different *TABLE COLUMS* (<td>). how can I achieve
this? What I usually do is:
------------------------------
// DB QUERY
$query = "SELECT * FROM table_name";
$result = mysql_query($query) or die(mysql_error());
$num_rows = mysql_num_rows($result);
// NOW THE LOOP
for ($i=0; $i<$num_rows; $i++)
{
$row = mysql_fetch_array($result);
echo "<tr>";
echo "<td>";
echo $row[whatever];
echo "</td>";
echo "</tr>";
}
------------------------------
but how can I get 2 or 3 columns displaying different db objects? A loop
inside a loop?
Thanks in advance,
<mailto:webmaster@icaam.com.ar> Cesar Aracena
CE / MCSE+I
Neuquen, Argentina
+54.299.6356688
+54.299.4466621