简体   繁体   English

循环时停止PHP然后继续吗?

[英]Stop PHP while loop then continue it?

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. 我有一个PHP MySQL查询从MySQL数据库中获取特色产品,然后查询离开加入产品图像的另一个表。

However the way I have my website design laid out the while query needs to stop and then continue. 然而,我的网站设计的方式奠定了while查询需要停止然后继续。

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">&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>';
                        }
                    }

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. 您应该使用MVC(模型,视图,控制器)模式以干净的方式处理这些问题。

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. 实现MVC时,您将使布局代码(在本例中为HTML)与查询数据库的代码分开并返回结果。 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. 这将允许您生成更易于维护的灵活代码。

The hard answer, like others are saying, is to go MVC. 像其他人一样,硬答案就是去MVC。

The easy answer seems to be to take those buttons outside of the while loop. 简单的答案似乎是将这些按钮放在while循环之外。

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
 }

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. 尝试从while删除product_intro_preview ,并通过ajax添加该内容,并在每次单击侧边按钮时取消调用。

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

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