SitePoint Sponsor |
|
User Tag List
Results 1 to 4 of 4
-
Apr 29, 2007, 04:14 #1
- Join Date
- Feb 2007
- Location
- Melbourne, Australia
- Posts
- 144
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Display Database Results in a HTML Table spanning multiple rows
Ok I have been searching the forums for a while and the net and I can't seem to come up with a solution to my problem. I want to display a bunch of results from a mysql db in tabular format like this:
link1 | link2 | link3 |
------------------
link4 | link5 | link6 |
OK thats a really bad way to draw a table but you get the idea. I know I need to use a nested loop to do this I'm just not sure what logic apply and what types of loops to use. At the moment I have the result set coming back from database as an object.
-
Apr 29, 2007, 04:30 #2
- Join Date
- Aug 2000
- Location
- Philadephia, PA
- Posts
- 20,578
- Mentioned
- 1 Post(s)
- Tagged
- 0 Thread(s)
PHP Code:$i = 0;
echo "<table><tr>";
while ($object = mysql_fetch_object($result)) {
if ($i % 3 == 0) {
echo "</tr><tr>";
}
echo "<td>" . $object->linkname . "</td>";
$i++;
}
echo "</tr></table>";
Try Improvely, your online marketing dashboard.
→ Conversion tracking, click fraud detection, A/B testing and more
-
Apr 29, 2007, 05:32 #3
- Join Date
- Feb 2007
- Location
- Melbourne, Australia
- Posts
- 144
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
The following is producing a infinite loop.
PHP Code:if($item = $db->get_results("SELECT tb1.products_id, tb1.products_image, tb1.products_price, tb2.products_name
FROM products AS tb1, products_description AS tb2
WHERE tb1.products_id = tb2.products_id")) {
echo"<div id='insCnt'>";
echo"<h1 id='Products'>Products</h1>";
$i = 0;
echo"<table cellpadding='0' cellspacing='3' border='0'>\n";
echo"<tr>\n";
while($items = $item) {
if ($i % 3 == 0){
echo"</tr>\n<tr>\n";
}
echo"<td>";
echo "<a href='/shop/product_info.php?products_id=$items->products_id'><img src='/shop/images/$items->products_image' border='0' height='80' width='100'><br>";
echo "$items->products_name</a><br>";
echo"</td>\n";
$i++;
}
echo"</tr>\n";
echo"</table>\n";
echo"</div>";
}
-
Apr 29, 2007, 05:54 #4
- Join Date
- Feb 2007
- Location
- Melbourne, Australia
- Posts
- 144
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Alright I figured it out by having a look at some articles here on sitepoint
Here is the final code I'm using
PHP Code:if($item = $db->get_results("SELECT tb1.products_id, tb1.products_image, tb1.products_price, tb2.products_name
FROM products AS tb1, products_description AS tb2
WHERE tb1.products_id = tb2.products_id")) {
echo"<div id='insCnt'>";
echo"<h1 id='Products'>Products</h1>";
$num_rows = 4;
echo"<table cellpadding='0' cellspacing='3' border='0'>\n";
echo"<tr>\n";
foreach($item as $items) {
if ($counter == $num_rows){
$counter = 1;
echo"</tr>\n<tr>\n";
}else $counter++;
echo"<td>";
echo "<a href='/shop/product_info.php?products_id=$items->products_id'><img src='/shop/images/$items->products_image' border='0' height='80' width='100'><br>";
echo "$items->products_name</a><br>";
echo"</td>\n";
}
echo"</tr>\n";
echo"</table>\n";
echo"</div>";
}
Bookmarks