2

Im a real beginner when it comes to queries and PDO, i need some assistance in a project i am busy with. What i want to accomplish is to display products from the database. However the template style i am using forces me to show 3 products per row, is there a way to show 3 products per row and loop the code once there is more than 3 (if there is 5 products, the first 3 will display in the first row, and the rest in the second).

Here is the template i am using, note the div class "top-box", this forces that only 3 products can be shown per row.

<div class="top-box">
        <?php
        $sql = "SELECT * FROM _products WHERE category = '$cat'";
$result = dbConnect()->query($sql);
// If the SQL query is succesfully performed ($result not false)
if($result !== false) {
$cols = $result->columnCount();           // Number of returned columns
// Generate ADS Feed for each ROW
foreach($result as $row) {
                echo '<div class="col_1_of_3 span_1_of_3">
             <a href="product.php?i=' . $row['model'] . '">
                <div class="inner_content clearfix">
                <div class="product_image">
                    <img src="images/' . $row['model'] . '.jpg" alt=""/>
                </div>
                <div class="price">
                   <div class="cart-left">
                        <p class="title">' . $row['name'] . '</p>
                    </div>
                    <div class="clear"></div>
                 </div>             
               </div>
               </a>
            </div>';
            }
        } else {
            echo "<p>No products available at this moment, contact us for more information!</p>";
        }

        ?>

            <div class="clear"></div>
        </div>
Marc Delisle
  • 8,879
  • 3
  • 29
  • 29

3 Answers3

2

You can solve it like this:

 <?php
    if ($counter % 3 == 0 && $counter!=$total_row_fetched) {
      echo '<div class="clear"></div>';
      echo '</div>';                    
      echo '<div class="top-box">';
     }

     if($counter==$total_row_fetched){
       echo '<div class="clear"></div>';
       echo '</div>';                    // it will close the last open div 
     }

     ++$counter;
    ?>
Kumari Manisha
  • 682
  • 2
  • 8
  • 22
1

You can use a count variable inside your products foreach-loop. Every three products you can close the top-box and open a new one (This is an assumption as I don't know exactly how your styles work).

At the end of the foreach-loop:

if ($count != 0 && $count % 3 == 0) {
   echo '<div class="clear"></div>';
   echo '</div>';                    // close last .top-box
   echo '<div class="top-box">';
}

++$count;

I don't see how your question is connected to PDO. Keep in mind that using unescaped variables inside a query is potentially dangerous. Have a look here for some help: https://stackoverflow.com/a/60496/2516377

Community
  • 1
  • 1
Marcel Pfeiffer
  • 1,018
  • 11
  • 12
  • Your code will not close the open div until the $count is multiple of 3 – Kumari Manisha May 22 '15 at 11:09
  • @KumariManisha this code is to be put at the end of the foreach-loop. In Codecommon's code the div is closed after the foreach-loop is complete. Some thought has to be put into not opening a .top-box if it will not contain anymore items. – Marcel Pfeiffer May 25 '15 at 07:58
0

Add a counter and use % arithmetic operator to calculate column number.

$counter=0;
foreach (...) {
  $column_number=$counter % 3;
  $counter++;
}
umka
  • 1,655
  • 1
  • 12
  • 18