简体   繁体   English

使用switch语句从数据库调用:PHP

[英]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. 布局是块/模块化的,所以我需要将一个类应用于每个div。 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. 问题是,当我在浏览器中预览时,代码循环再次从第一种情况开始-跳过了数据库中的一项,并在情况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! 任何建议/替代方法将不胜感激!

You given $index as 1 您给$ index作为1

$index = 1;

So, it will go to case 1 every time. 因此,每次都会转到案例1。 Its not dynamic. 它不是动态的。

If I understand correctly you want 'grouping' of 10 products 如果我理解正确,则希望将10种产品“分组”

$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++;
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM