开发者

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.

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 s开发者_StackOverflow社区top 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.

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.


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
 }


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜