【问题标题】:Stop PHP while loop then continue it?停止 PHP while 循环然后继续它?
【发布时间】:2011-10-16 06:03:02
【问题描述】:

我有一个 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>';
                        }
                    }

【问题讨论】:

  • 抱歉,它在我的本地 XAMPP 安装中,我不打算很快上传该站点。我知道这看起来很荒谬,因为我是寻求帮助的人,但我认为没有必要进行演示。

标签: php mysql loops while-loop


【解决方案1】:

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

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

【讨论】:

  • MVC 可能与这样的代码相比有很大的飞跃。将数据采集与数据表示分开应该是一个很好的开始练习。是的 - 至少需要两个 while llop。
  • 虽然我同意这条评论,但它评论而不是答案。
  • 也许是相当大的飞跃,但值得一试。它回答了 OP 的问题:“我要么需要停止循环然后继续它,要么找到另一种方式?
  • 我不同意这不是一个答案。这不是他们正在寻找的答案:D。我希望我可以多次投票。
  • 这个答案确实让我有点困惑,但我明天会回来(对我来说已经很晚了)看看。谢谢:)
【解决方案2】:

正如其他人所说,最难的答案是采用 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
 }

【讨论】:

  • 会的,但是代码的布局方式(基于网站的设计)我无法将其取出,但仍然有我想要的按钮。
【解决方案3】:

就在我的脑海中:

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

【讨论】:

  • 我对 Ajax 了解不多,所以我明天再看看吧:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-18
  • 1970-01-01
  • 2017-08-19
  • 2012-05-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多