简体   繁体   English

在While循环中中断If语句而不中断while循环PHP

[英]Break If Statement in While loop without breaking the while loop PHP

Ok, So Im creating a while loop for multiple accordions in php. 好的,所以我为php中的多个手风琴创建了一个while循环。 I marked up some php code that ALMOST works. 我标记了一些ALMOST可以工作的php代码。 The problem I am having is that I had to nest five different IF statements in a while loop for the five different accordions. 我遇到的问题是,我不得不在while循环中为五个不同的手风琴嵌套五个不同的IF语句。 The If statements contain the content to be in the accordion. If语句包含手风琴中的内容。 The content is a list of songs in a particular album. 内容是特定专辑中歌曲的列表。 What I need to happen is for the If statement containing the content for one accordion to break, but still continue with the original while statement and do this for each accordion. 我需要做的是,如果If语句包含一个手风琴的内容要中断,但仍然继续原始的while语句并为每个手风琴执行此操作。 Here is my PHP Code. 这是我的PHP代码。

if ($result)
        {
            while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
               {
                   echo '<div class="col-small-6 col-med-6 col-lg-4 albumContainer">';
                   echo '<img src=' . $row['Album_Art'] . 'alt="">';           
                   echo '<div class="panel-group" id="accordion">';
                   echo '<div class="panel panel-default">';
                   echo '<div class="panel-heading">';
                   echo '<h2 class="panel-title"><a data-toggle="collapse" data-parent="#accordion" href=' . $row['direction'] . '>' . $row['Title'] . '</a></h2></div><div id=' . $row['destination'] . ' class="panel-collapse collapse"><div class="panel-body"> <ol>';
                   if($result1)
                   {
                        while ($row1 = mysqli_fetch_array($result1, MYSQLI_ASSOC))
                        {                               
                            echo '<li>' . $row1['Name'] . '</li>';                              
                        }; 
                    }; 
                    if($result2)
                    {
                        while($row2 = mysqli_fetch_array($result2, MYSQLI_ASSOC))
                        {
                            echo '<li>' . $row2['Name'] . '</li>';
                        }
                    };
                    if($result3)
                    {
                        while($row3 = mysqli_fetch_array($result3, MYSQLI_ASSOC))
                        {
                            echo '<li>' . $row3['Name'] . '</li>';
                        }
                    };
                    if($result4)
                    {
                        while($row4 = mysqli_fetch_array($result4, MYSQLI_ASSOC))
                        {
                            echo '<li>' . $row4['Name'] . '</li>';
                        }
                    };
                    if($result5)
                    {
                        while($row5 = mysqli_fetch_array($result5, MYSQLI_ASSOC))
                        {
                            echo '<li>' . $row5['Name'] . '</li>';
                        }
                    };
                    if($result6)
                    {
                        while($row6 = mysqli_fetch_array($result6, MYSQLI_ASSOC))
                        {
                            echo '<li>' . $row6['Name'] . '</li>';
                        }
                    };
                    echo '</ol>';
                    echo '<h3>available at:</h3><a href=' . $row['Location'] . '>iTunes</a>
                    <a href=' . $row['Location2'] . '>Amazon</a>
                    <a href=' . $row['Location3'] . '>United Interests</a></div>';
                    echo '</div></div></div></div>';
                }
                    mysqli_free_result ($result); 
        }
            else 
            {
                echo '<p class="error">The current users could not be retrieved. We apologize for any inconvenience.</p>';
                echo '<p>' . mysqli_error($dbcon) . '<br><br />Query: ' . $q . '</p>';
            } 
            mysqli_close($dbcon); 
            ?>

Any insights on how to make this work for me would be greatly appreciated. 对于如何为我完成这项工作的任何见解将不胜感激。 I cant use Jquery for the accordion because of how the code was previously structured. 由于代码的先前结构,我无法将Jquery用于手风琴。

use continue http://php.net/manual/en/control-structures.continue.php 使用continue http://php.net/manual/en/control-structures.continue.php

continue is used within looping structures to skip the rest of the current loop iteration and continue execution at the condition evaluation and then the beginning of the next iteration. 在循环结构中使用continue可以跳过当前循环迭代的其余部分,并在条件评估和下一个迭代的开始处继续执行。

Note: Note that in PHP the switch statement is considered a looping structure for the purposes of continue. 注意:请注意,在PHP中,出于继续的目的,将switch语句视为循环结构。

also, you should probably use a switch instrad of multiple if conditions 另外, if条件允许,您应该使用多个的开关

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

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