停止 PHP while 循环然后继续它?

发布于 2024-11-27 07:20:04 字数 2651 浏览 0 评论 0原文

我有一个 PHP MySQL 查询来从 MySQL 数据库中获取特色产品,然后该查询左连接另一个表以获取产品图像。

然而,我的网站设计布局方式使得查询需要停止然后继续。

我有一个指向左的箭头,然后有一个指向右的箭头(图像的两侧),以浏览特色产品。然而,图像本身是在两个按钮之前调用的。

我要么需要停止循环然后继续它,要么找到另一种方法?否则,对于每个被调用的产品,按钮都会得到回显。

这就是我的意思:

    echo '<div class="slides_container">';
        echo '<div class="slideItem" style="display: none"><a href="product.php?id='.$q['id'].'"><img src="images/dummy/pic_1.jpg" alt="" /></a></div>';
    echo '</div>';
    echo '<a class="s_button_prev" href="javascript:;"></a>';
    echo '<a class="s_button_next" href="javascript:;"></a>';

以下是所有内容:

if (!$query = @mysql_query("
                        SELECT *
                        FROM products
                        LEFT JOIN products_img_lookup ON products_img_lookup.nil_products_id = products.id
                        WHERE featured = 1
                        GROUP BY products.id
                        ORDER BY id DESC")) {
                        echo '<strong>Error:</strong> '.mysql_error().'';
                    } else {
                        $c = 0;
                        while ($q = mysql_fetch_array($query)) {
                            $detail = stripslashes($q['description']);
                            $detail = strip_tags($detail);

                            echo '<h2><a href="product.php?id='.$q['id'].'">'.$q['name'].'</a></h2>';

                                echo '<p class="s_desc">'.trim_text($detail,25).'</p>';
                                echo '<div class="s_price_holder">';
                                    echo '<p class="s_price"> <span class="s_currency s_before">&pound;</span>'.$q['price'].' </p>';
                                echo '</div>';
                            echo '</div>';
                        echo '<div id="product_intro_preview">';
                            echo '<div class="slides_container">';
                                echo '<div class="slideItem" style="display: none"><a href="product.php?id='.$q['id'].'"><img src="images/dummy/pic_1.jpg" alt="" /></a></div>';
                            echo '</div>';
                            echo '<a class="s_button_prev" href="javascript:;"></a>';
                            echo '<a class="s_button_next" href="javascript:;"></a>';
                        echo '</div>';
                        }
                    }

I've got a PHP MySQL query to grab featured products from a MySQL database, the query then left joins another table for the product image.

However the way I have my website design laid out the while query needs to stop and then continue.

I have an arrow that points left and then an arrow that points right, either side of the image, to navigate through the featured products. However the image itself is called before the two buttons.

I either need to stop the loop and then continue it, or find another way? Otherwise the buttons get echoed for each product that is called.

This is what I mean:

    echo '<div class="slides_container">';
        echo '<div class="slideItem" style="display: none"><a href="product.php?id='.$q['id'].'"><img src="images/dummy/pic_1.jpg" alt="" /></a></div>';
    echo '</div>';
    echo '<a class="s_button_prev" href="javascript:;"></a>';
    echo '<a class="s_button_next" href="javascript:;"></a>';

Here's all of it in one:

if (!$query = @mysql_query("
                        SELECT *
                        FROM products
                        LEFT JOIN products_img_lookup ON products_img_lookup.nil_products_id = products.id
                        WHERE featured = 1
                        GROUP BY products.id
                        ORDER BY id DESC")) {
                        echo '<strong>Error:</strong> '.mysql_error().'';
                    } else {
                        $c = 0;
                        while ($q = mysql_fetch_array($query)) {
                            $detail = stripslashes($q['description']);
                            $detail = strip_tags($detail);

                            echo '<h2><a href="product.php?id='.$q['id'].'">'.$q['name'].'</a></h2>';

                                echo '<p class="s_desc">'.trim_text($detail,25).'</p>';
                                echo '<div class="s_price_holder">';
                                    echo '<p class="s_price"> <span class="s_currency s_before">£</span>'.$q['price'].' </p>';
                                echo '</div>';
                            echo '</div>';
                        echo '<div id="product_intro_preview">';
                            echo '<div class="slides_container">';
                                echo '<div class="slideItem" style="display: none"><a href="product.php?id='.$q['id'].'"><img src="images/dummy/pic_1.jpg" alt="" /></a></div>';
                            echo '</div>';
                            echo '<a class="s_button_prev" href="javascript:;"></a>';
                            echo '<a class="s_button_next" href="javascript:;"></a>';
                        echo '</div>';
                        }
                    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

寄与心 2024-12-04 07:20:04

这是由于混合布局、数据库访问和业务逻辑而导致的常见问题。您应该使用 MVC(模型、视图、控制器)模式以干净的方式处理这些问题。

当您实现 MVC 时,您将把布局代码(在本例中为 HTML)与查询数据库并返回结果的代码分开。您将能够将数据库查询与复杂的界面布局分开。这将使您能够生成更易于维护的灵活代码。

This is a common problem caused by mixing layout, database access, and business logic. You should use the MVC (Model, View, Controller) pattern to deal with these problems in a clean manner.

When you implement MVC, you will keep your layout code (HTML, in this case) separate from the code that queries the database and returns results. You will be able separate the database queries from the intricacies of the interface layout. This will allow you to produce flexible code that is easier to maintain.

戒ㄋ 2024-12-04 07:20:04

正如其他人所说,困难的答案是采用 MVC。

简单的答案似乎是将这些按钮放在 while 循环之外。

或者,您可以简单地每隔一段时间输出一次,如下所示:

echo '<div id="product_intro_preview">';
    echo '<div class="slides_container">';
        echo '<div class="slideItem" style="display: none"><a href="product.php?id='.$q['id'].'"><img src="images/dummy/pic_1.jpg" alt="" /></a></div>';
    echo '</div>';

    // Every five rows (or however many you need in between output), print out the buttons
    if($c % 5 == 0) {                                    
        echo '<a class="s_button_prev" href="javascript:;"></a>';
        echo '<a class="s_button_next" href="javascript:;"></a>';
     }                       
     echo '</div>';
  $c++; 
} // end while loop

或者,您是否只想在第一次访问时将它们输出一次?那也很简单:

 if($c==0) {
     // output buttons
 }

The hard answer, like others are saying, is to go MVC.

The easy answer seems to be to take those buttons outside of the while loop.

Or, you could simply output it only every so often, as so:

echo '<div id="product_intro_preview">';
    echo '<div class="slides_container">';
        echo '<div class="slideItem" style="display: none"><a href="product.php?id='.$q['id'].'"><img src="images/dummy/pic_1.jpg" alt="" /></a></div>';
    echo '</div>';

    // Every five rows (or however many you need in between output), print out the buttons
    if($c % 5 == 0) {                                    
        echo '<a class="s_button_prev" href="javascript:;"></a>';
        echo '<a class="s_button_next" href="javascript:;"></a>';
     }                       
     echo '</div>';
  $c++; 
} // end while loop

Or, are you just wanting them put out once, on the first go through? That would be just as simple:

 if($c==0) {
     // output buttons
 }
恋你朝朝暮暮 2024-12-04 07:20:04

就在我的脑海中:

尝试从 while 中删除 product_intro_preview 并通过 ajax 添加该内容,并在每次单击侧面按钮时偏移调用。

Just off the top of my head:

try removing product_intro_preview from the while and adding that content via ajax and just offseting the call for each time the side button it clicked.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文