停止 PHP while 循环然后继续它?
我有一个 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">£</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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是由于混合布局、数据库访问和业务逻辑而导致的常见问题。您应该使用 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.
正如其他人所说,困难的答案是采用 MVC。
简单的答案似乎是将这些按钮放在 while 循环之外。
或者,您可以简单地每隔一段时间输出一次,如下所示:
或者,您是否只想在第一次访问时将它们输出一次?那也很简单:
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:
Or, are you just wanting them put out once, on the first go through? That would be just as simple:
就在我的脑海中:
尝试从
while
中删除product_intro_preview
并通过 ajax 添加该内容,并在每次单击侧面按钮时偏移调用。Just off the top of my head:
try removing
product_intro_preview
from thewhile
and adding that content via ajax and just offseting the call for each time the side button it clicked.