Listing Results In Columns<

liunx

Guest
I would think that this would be a simple thing using the for loop but I can't figure it out. How can I display the following in 3 different table columns:


<?php
/*
Author: Pawel Kowalski
Filename: functions.php
Description: Functions for scripts.
*/
require 'config.php';

if ($params['include'] == "category_list") {
mysql_connect("$sql_host", "$sql_user", "$sql_pass");
mysql_select_db("$sql_db");
if (!$_GET['catid']) {$catid = 0;}
else { $catid = $_GET['catid']; }
$sql = "SELECT * FROM cms_category WHERE topcat=$catid";
$pointer = mysql_query( $sql );
while($row = mysql_fetch_assoc( $pointer )) {
$rows[] = $row;
$sub_id = $row['id'];
echo "<p><b><a href=http://www.htmlforums.com/archive/index.php/\"category.php?catid={$row['id']}\">{$row['name']}</a></b>";
$sql_prodsub = "SELECT * FROM cms_products WHERE catid=$sub_id";
$point_prodsub = mysql_query( $sql_prodsub );
$num_prod = mysql_num_rows($point_prodsub);
if ($num_prod) {
echo "<table border=\"1\" cellpadding=\"5\" cellspacing=\"0\" width=\"100%\">";
}


while($row_prod = mysql_fetch_assoc( $point_prodsub )) {

$rows_prod[] = $row_prod;
if ($num_prod) {


?>
<tr>
<td width="18%"><? if ($row_prod['main_picture']) { echo "<img src=http://www.htmlforums.com/archive/index.php/\"{$row_prod['main_picture']}\" border=\"0\">"; } ?></td>
<td width="82%"><a href=http://www.htmlforums.com/archive/index.php/"products.php?do=product&prodid=<? echo "{$row_prod['id']}"; ?>"><? echo "{$row_prod['title']}"; ?></a><br>
<? echo "{$row_prod['general_desc']}"; ?>
</td>
</tr>
<?


}

}


if ($num_prod) {
echo "</table>";
}


$sql_sub = "SELECT * FROM cms_category WHERE topcat=$sub_id";
$pointer_sub = mysql_query( $sql_sub );
while($row_sub = mysql_fetch_assoc( $pointer_sub )) {
$rows_sub[] = $row_sub;
$num_rows = mysql_num_rows($pointer_sub);
if ($num_rows) {
echo "<br>|-<a href=http://www.htmlforums.com/archive/index.php/\"category.php?catid={$row_sub['id']}\">{$row_sub['name']}</a>";
}



}

}

mysql_close();
}
?>


The above is the full script, all I need to display in the columns is the following part:

if ($num_prod) {
echo "<table border=\"1\" cellpadding=\"5\" cellspacing=\"0\" width=\"100%\">";
}


while($row_prod = mysql_fetch_assoc( $point_prodsub )) {

$rows_prod[] = $row_prod;
if ($num_prod) {


?>
<tr>
<td width="18%"><? if ($row_prod['main_picture']) { echo "<img src=http://www.htmlforums.com/archive/index.php/\"{$row_prod['main_picture']}\" border=\"0\">"; } ?></td>
<td width="82%"><a href=http://www.htmlforums.com/archive/index.php/"products.php?do=product&prodid=<? echo "{$row_prod['id']}"; ?>"><? echo "{$row_prod['title']}"; ?></a><br>
<? echo "{$row_prod['general_desc']}"; ?>
</td>
</tr>
<?


}

}


if ($num_prod) {
echo "</table>";
}


Thank you in advance for any help in this matter.you have to tell the loop when to add a </tr> if it reaches the 3rd column.

it is tricky but very possible. you can set a variable to count and if it reaches 3 then add a closing tr tag and start a new one for a new row.

make sense?

<tr>
<td>
<td>
<td>
</tr>

is tha twhat you are looking for as far as columns?Thanks scoutt for the reply. I am completely lost on this one. I was able to use the for loop do this without problems but that was on something that wasn't in another loop like this:

echo "<table width=\"100%\" border=\"1\"><tr>";
for($loop1 = 1; $loop1 < 5; $loop1++) {

if ($loop1 == 4) {
echo "<tr>";
}
echo "<td><b>Loop:</b>$loop1</td>";
if ($loop1 == 4) {
//echo "</tr><tr>";
}


}
echo "</tr></table>";

However, when I place it over my while loop which gets results from a database I can't get it to count which loop its on:

for($loop2 = 1; $loop2 < 5; $loop2++) {
while($row_prod = mysql_fetch_assoc( $point_prodsub )) {

$rows_prod[] = $row_prod;

if ($num_prod) {



if ($loop2 == 2) {
echo "<tr>";
}

?>
<td width="18%"><? if ($row_prod['main_picture']) { echo "<img src=http://www.htmlforums.com/archive/index.php/\"{$row_prod['main_picture']}\" border=\"0\">"; } ?></td>
<td width="82%"><a href=http://www.htmlforums.com/archive/index.php/"products.php?do=product&prodid=<? echo "{$row_prod['id']}"; ?>"><? echo "{$row_prod['title']}"; ?></a><br>
<? echo "{$row_prod['general_desc']}"; echo "<b>loop2:</b> $loop2"; ?>
</td>
<?
if ($loop1 == 2) {
//echo "</tr>";
}



//}

}
}


In the above $loop2 always stays at 1 so I can't use a boolean that will be used to display the <tr> tag. Any ideas?
Sorry about the messy code, I need to figure this out before I clean it up.no, you are in one loop already so you don't need another one.


$loop2 = 0;
while($row_prod = mysql_fetch_assoc( $point_prodsub )) {

$rows_prod[] = $row_prod;

if ($num_prod) {



if ($loop2 == 2) {
echo "<tr>";
}

$loop2++;
?>I already tried that, but I put $loop2 = 0; into the loop so that is why it didn't work :bash:, thanks scoutt this works perfectly. However, I will have lots of products listed and don't want to have to list a boolean for every 3 rows, is there a way to do this so it will print out <tr> every 3 rows? Currently I have to have it like this:

if ($loop2 == '3' or $loop2 == '6' or $loop2 == '9') { echo "<tr>"; }


This can cause a problem if you have a lot of rows.well you want to use the modulus term.

if ($loop2%3) { echo "<tr>"; }

that is saying, if the $loop2 is divisible by 3 it will be true. you may have to play with it as I always get it one off.Scoutt, have I ever told you that I love you? :cheers:

That should do what I need it to do, highly appreciated.not today anyway :P

glad it works.
 
Back
Top