0

I have product list ( 9 products in multidimensional array ), i want to achieve that all 9 product will be ordered in 3 div columns. for example

 product1       product4       product7
 product2       product5       product8
 product3       product6       product9

And my array structure is

array(
  array(product_name1),
  array(product_name2),
  array(product_name3),
  array(produ‌​ct_name4),
  array(product_name5)
)

i have html code how this must look like

<div class="container">
<div class="prod-column">
    <div class="product-slot">
        product1
    </div>
    <div class="product-slot">
        product2
    </div>
    <div class="product-slot">
        product3
    </div>
</div> <!-- close column -->
<div class="prod-column">
    <div class="product-slot">
        product4
    </div>
    <div class="product-slot">
        product5
    </div>
    <div class="product-slot">
        product6
    </div>
</div> <!-- close column -->
<div class="prod-column">
    <div class="product-slot">
        product7
    </div>
    <div class="product-slot">
        product8
    </div>
    <div class="product-slot">
        product9
    </div>
</div> <!-- close column -->
</div> <!-- close container -->

Also i find code on Answered here

but in my case i've got blank row at end, i can't find a reason why i've got this empty prod-column at end

Thanks for help in advance

3
  • array(array(product_name1),array(product_name2),array(product_name3),array(product_name4),array(product_name5)) Commented Oct 27, 2011 at 11:53
  • also i check linked code with 8 products and then this code work, it also work if i put 10 products Commented Oct 27, 2011 at 11:54
  • duplicate of How to create php 2 column table with values from the database? and many many many more Commented Oct 27, 2011 at 12:05

2 Answers 2

0

Ms solution would be like this:

<div class="container">
<?php
for ($i = 0,$c=count($array); $i<$c; $i++) {
    if ($i % 3 == 0) {
?>
<div class="prod-column">
<?php
    }
?>
<div class="product-slot"><?php echo $array[$i][product];?></div>
<?php
    if ($i % 3 == 2) {
?>
</div>
<?php
    }
}
?>
</div>
Sign up to request clarification or add additional context in comments.

Comments

-1

Try this. Assumes your product name is the first element of an indexed array - if it isn't, modify $array[$i][0] appropriately. Also assumes you have a number of products divisible by three (which presumably wont always be the case) so it doesn't generate empty filler fields, but hopefully it will give you the idea.

echo "<div class=\"container\">\n";

for ($i = 0, $j = 0; isset($array[$i]); $i++) {
  if ($j == 0) {
    echo "<div class=\"prod-column\">\n";
  }
  echo "    <div class=\"product-slot\">\n        {$array[$i][0]}\n    </div>\n";
  if ($j == 2) {
    echo "</div>\n";
    $j = 0;
  } else $j++;
}

echo "</div>";

3 Comments

Seriously, I don't mind being wrong/being disagreed with, but downvoting with no explanation is just rude, and does not promote people learning from their mistakes...
i didn't give any vote for you since i still checking your first version of code
No worries, not having a go at you, just the floating-downvoters out there. Like I say, this may not produce the result you expect if you have, say, 8 results instead of 9. You should try this code with a number of results not divisible by 3 to make sure you are still happy with the way it displays.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.