Gregorios
Gregorios

Reputation: 1

Calling from a database using a switch statement: PHP

The layout is block/modular so I need to apply a class to each of the divs. Issue is that when I preview in browser, after the code loops to begin at case 1 again - it skips one of the items from the db and displays the next item as case 1.

     $result = mysqli_query($connection, "SELECT * FROM products");
        $index = 1;
        while($row = mysqli_fetch_array($result)) {

        $float = "";

            switch ($index) {
                case 1: $float = "large-left";
                        echo "<div class=\"$float\"><img src=\"" . $row['product_imagepath'] . "\"/></div>";
                    break;
                case 2: $float = "mini-left";
                        echo "<div class=\"$float\"><img src=\"" . $row['product_imagepath'] . "\"/></div>";
                    break;
                case 3: $float = "mini-right";
                        echo "<div class=\"$float\"><img src=\"" . $row['product_imagepath'] . "\"/></div>";
                    break;
                case 4: $float = "mini-left";
                        echo "<div class=\"$float\"><img src=\"" . $row['product_imagepath'] . "\"/></div>";
                    break;
                case 5: $float = "mini-right";
                        echo "<div class=\"$float\"><img src=\"" . $row['product_imagepath'] . "\"/></div>";
                    break;
                case 6: $float = "large-right";
                        echo "<div class=\"$float\"><img src=\"" . $row['product_imagepath'] . "\"/></div>";
                    break;
                case 7: $float = "mini-left";
                        echo "<div class=\"$float\"><img src=\"" . $row['product_imagepath'] . "\"/></div>";
                    break;
                case 8: $float = "mini-right";
                        echo "<div class=\"$float\"><img src=\"" . $row['product_imagepath'] . "\"/></div>";
                    break;
                case 9: $float = "mini-left";
                        echo "<div class=\"$float\"><img src=\"" . $row['product_imagepath'] . "\"/></div>";
                    break;
                case 10: $float = "mini-right";
                        echo "<div class=\"$float\"><img src=\"" . $row['product_imagepath'] . "\"/></div>";
                    break;
                default: $index = 0;
                    break;
            };          

        $index++;
        };

        mysqli_close($result);
    ?>

Any recommendations/alternative methods would be greatly appreciated!

Upvotes: 0

Views: 234

Answers (2)

Gervs
Gervs

Reputation: 1397

If I understand correctly you want 'grouping' of 10 products

$result = mysqli_query($connection, "SELECT * FROM products");
$index = 1;
while($row = mysqli_fetch_array($result)) {
     if ($index % 10 == 1) {
         $float = 'large-left';
     }
     else {
         if($index % 2 == 1) {
             $float = 'mini-right';
         }
         else {
             $float = 'mini-left';
         }
     }
     echo '<div class="' . $float . '"><img src="' . $row['product_imagepath'] . '"></div>';
     $index++;
} 

Upvotes: 0

Naincy
Naincy

Reputation: 2943

You given $index as 1

$index = 1;

So, it will go to case 1 every time. Its not dynamic.

Upvotes: 3

Related Questions