Forum Moderators: coopster

Message Too Old, No Replies

PHP Output into Table, 5 Items Per Row?

         

thelight

10:43 am on Apr 11, 2009 (gmt 0)

10+ Year Member



Hi there, hope you can help here.

I basically want to output my list of products from my database into a table, but display 5 products per table row.

e.g.

"Product1, Product2, Product3, Product4, Product5"
"Product6, Product7, Product8, Product9, Product10"

At the momement I am outputting 1 product per row.

e.g.

"Product1"
"Product2"
"Product3" etc

Here is the PHP code I have got (I didn't write it so I need to customise it to do this).

Anyone have an idea?:

-------------------------------------------------------
require_once "$ds_path/lib/database.inc";

$products = get_products($db, stripslashes($ds_query), '' , 1, get_post_meta($post->ID, 'ds_title', true), get_option('ds_clickref'));

$permalink = get_permalink();
echo get_post_meta($post->ID, 'ds_title', true);

echo('<div style="clear:both"><table class="pricebox">');

foreach ($products as $product) {

if (str_startswith($product['deep_link'], 'http')) {
$deep_link = $product['deep_link'];
} else {

if (! $ds_redirect_url) {
$ds_redirect_url = $ds_url;
}

$deep_link = $ds_redirect_url . '/' . $product['deep_link'];
}

echo <<<EOHTML
<td>
<a href="$deep_link"><img src="{$product['image_url']}" border="0" alt="" width="100" height="100" /></a>
<p><strong>{$product['name']}</strong><br /><a target="_{$product['mykey']}" href="$deep_link"><strong>$CURRENCY_PREFIX{$product['display_price']}</strong></a><br /><a href="$deep_link"><img src="http://www.myurl.com/images/more.gif" border="0"/></a>

</td>

EOHTML;

}

echo('</table></div>');

}
------------------------------------------------------

Many Thanks

andrewsmd

7:51 pm on Apr 11, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I simplified your code but it is something like this

//initialize a counter
$count = 0;

//start the table
echo("<table><tr>");
foreach ($products as $product) {

//if it's divisible by 5 then we echo a new row
if(($count % 5) == 0){

echo("</tr><tr>\n");
$count++;

}//if
else{
$count++;
}

echo("<td>$product<td>");

}//foreach

//close the table
echo("</tr></table>");

rocknbil

3:20 pm on Apr 12, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That works, but what if the last row is not a full five? You get goofy table endings like this:

<tr><td>1</td><td>2</td><td>3</td><td>4</td><td>5</td></tr>
<tr><td>1</td><td>2</td><td>3</td></tr>

Try

EDITED: removed typos and posted working solution below

[edited by: rocknbil at 3:56 pm (utc) on April 12, 2009]

andrewsmd

3:27 pm on Apr 12, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I ran mine with two extra and got a table that was closed properly?

thelight

3:38 pm on Apr 12, 2009 (gmt 0)

10+ Year Member



Hi, thanks both for replies, Ill give them both a go.

Many thanks

rocknbil

3:51 pm on Apr 12, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Closed "properly", but not five columns in the last row. Put a border on it . . fugly! :-)

$products = Array ('one','two','three','four','five','six','seven');
.....
echo("<table border=\"1\"><tr>");
.....

<table border="1"><tr></tr><tr>
<td>one<td><td>two<td><td>three<td><td>four<td><td>five<td></tr><tr>
<td>six<td><td>seven<td></tr>
</table>

I did have a typo, should be
$php_script . '?show='

But the output from the working code below (fixed, and added newlines to pretty-fy output) is:

<table align="center" border="1">
<tr><td><a href="yourscript.php?show=1">one</a></td>
<td><a href="yourscript.php?show=2">two</a></td>
<td><a href="yourscript.php?show=3">three</a></td>
<td><a href="yourscript.php?show=4">four</a></td>
<td><a href="yourscript.php?show=5">five</a></td>
</tr>

<tr><td><a href="yourscript.php?show=6">six</a></td>
<td><a href="yourscript.php?show=7">seven</a></td>

<td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr></table>

Unfilled table cells can wreak all sorts of visual havok for column headers with colspans, tabled layouts, and invalid doctypes. A side note: You can use this same method with paragraphs, divs, or forms instead of table cells to output a table-less layout, if this is not tabular data (which in this scenario, products, it may not be.)

Working/fixed code:


$product_list = Array (
'1'=>'one',
'2'=>'two',
'3'=>'three',
'4'=>'four',
'5'=>'five',
'6'=>'six',
'7'=>'seven'
);
$cols = $i = 0;
$totalcols = 5; // I want five columns
$phpscript = 'yourscript.php';
$products=$output='';
// $product_list is your associative array of products
foreach ($product_list as $product_id=>$title) {
if ($cols == 0) { $products .= "\n" . '<tr>'; }
$products .= '<td><a href="' . $phpscript . '?show=' . $product_id . '">' . $title . '</a></td>'."\n";
$cols++; // Add a column, count it
if ($cols >= $totalcols) { $products .= '</tr>' . "\n"; $cols = 0; } // Reset, columns full
}
// We've reached the end, fill out bottom column.
// If cols == 0, it was divisible by five, if not . . .
if (($cols>0) and ($cols < $totalcols)) {
for ($i=$cols;$i<=($totalcols-1);$i++) {
$products .= '<td>&nbsp;</td>'; // IE hates empty cells
}
$products .= '</tr>';
}
if ($products == '') {
$products = '<tr><td colspan="' . $totalcols . '">There are no products to display.</td></tr>';
}
$output = '<table align="center" border="1">' . $products . '</table>';
header("content-type:text/html\n\n");
echo $output;
// or print, I don't see the diff . . . :-)