1

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">&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>';
                        }
                    }
1
  • Sorry, it's on my local XAMPP installation, and I don't plan on uploading the site any time soon. I know this seems ridiculous as I'm the one asking for help, but I don't think a demo is necessary. Commented Jul 30, 2011 at 1:01

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

5 Comments

MVC might quite a leap from code like this. Just separating data acquisition from data presentation should be a good excersise for start. Yes - at least two while llops will be needed.
while I agree with this comment, it is a comment and not an answer.
Quite a leap, perhaps, but a worthwhile one. And it answers the OP's question: "I either need to stop the loop and then continue it, or find another way? "
I disagree with this not being an answer. It is just not the answer they are looking for :D. I wish I could upvote more than once.
This answer did confuse me a little bit, but I'll come back tomorrow (it's pretty late for me) to take a look. Thanks :)
1

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
 }

1 Comment

It would be, but the way the code is laid out (based on the design of the site) I can't take it out and still have the buttons where I want them.
0

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.

1 Comment

I don't know an awful lot about Ajax, so I'll take a look at it tomorrow anyway :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.