Jump to content

Displaying products

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
8 replies to this topic

#1
yonghan

yonghan

    Learning Programmer

  • Members
  • PipPipPip
  • 99 posts
Hi all,i'm trying to display products by limiting it to 3 columns in a row..How should i achieve it?Thanks..

#2
Pa28

Pa28

    Newbie

  • Members
  • Pip
  • 4 posts
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
yonghan

yonghan

    Learning Programmer

  • Members
  • PipPipPip
  • 99 posts
Thanks Pa28....By the way,is it possible to achieve it by using css?

#4
BlaineSch

BlaineSch

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,448 posts
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
yonghan

yonghan

    Learning Programmer

  • Members
  • PipPipPip
  • 99 posts
@BlaineSch It can be used inside the loop,does it?Thanks..

#6
BlaineSch

BlaineSch

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,448 posts
Yes, exactly!

#7
yonghan

yonghan

    Learning Programmer

  • Members
  • PipPipPip
  • 99 posts
So i should change table to div,isn't it?Is it the same with <tr> and <td>? Thanks

#8
BlaineSch

BlaineSch

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,448 posts
<?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
yonghan

yonghan

    Learning Programmer

  • Members
  • PipPipPip
  • 99 posts
Ok,thanks a lot guys..I'll try it out.. ^_^