Hi all,i'm trying to display products by limiting it to 3 columns in a row..How should i achieve it?Thanks..
Displaying products
Started by yonghan, Oct 05 2009 07:05 PM
8 replies to this topic
#1
Posted 05 October 2009 - 07:05 PM
|
|
|
#2
Posted 05 October 2009 - 09:20 PM
I'm guessing you have your list of products in an array?
<?php
$products = array("A", "B", "C", "D", "E", "F", "G");
$numColumns = 3;
echo("<table>");
while($k<count($products)){
echo("<tr>\n");
for($j=0; $j<$numColumns; $j++){
$k = $j + $i*$numColumns;
if(isset($products[$k])){
echo("<td>".$products[$k]."</td>\n");
}else{
echo("<td></td>\n");
}
}
echo("</tr>\n");
$i++;
}
echo("</table>");
?>
#3
Posted 05 October 2009 - 09:40 PM
Thanks Pa28....By the way,is it possible to achieve it by using css?
#4
Posted 05 October 2009 - 09:59 PM
Make a div with the width of what you want, then make the div's inside float left, specify one third width. No need for breaks etc it does so automatically:
<div style="border:2px solid black;width:650px;display:table;"> <div style="float:left;border:2px solid green;width:200px;">ONE</div> <div style="float:left;border:2px solid red;width:200px;">TWO</div> <div style="float:left;border:2px solid blue;width:200px;">THREE</div> <div style="float:left;border:2px solid purple;width:200px;">THREE</div> </div>
#5
Posted 05 October 2009 - 10:10 PM
@BlaineSch It can be used inside the loop,does it?Thanks..
#6
Posted 05 October 2009 - 10:14 PM
#7
Posted 05 October 2009 - 10:46 PM
So i should change table to div,isn't it?Is it the same with <tr> and <td>? Thanks
#8
Posted 06 October 2009 - 06:34 AM
<?php
$products = array("A", "B", "C", "D", "E", "F", "G");
$numColumns = 3;
echo '<div style="width:650px;display:table">';
while($k<count($products)){
for($j=0; $j<$numColumns; $j++){
$k = $j + $i*$numColumns;
if(isset($products[$k])){
echo '<div style="float:left;border:2px solid green;width:200px;">'.$products[$k].'</div>\n';
}
}
$i++;
}
echo '</div>';
?>
#9
Posted 06 October 2009 - 06:47 AM
Ok,thanks a lot guys..I'll try it out.. ^_^


Sign In
Create Account


Back to top










