如何强制if循环重置[PHP]

时间:2018-01-27 11:47:19

标签: php sql


我想知道如何强制if循环重置
当用户点击索引页面上的按钮时,将启动此代码。

  <?php
    include 'connexionBDD.php';
    session_start();
    $commentNewCount = $_POST['commentNewCount'];

    $sql = "SELECT * FROM sentencesen WHERE type = 'Fun' ORDER BY rand() LIMIT 1";
    $result = mysqli_query($conn, $sql);
    if (mysqli_num_rows($result) > 0) {
        while ($row = mysqli_fetch_assoc($result)) { //while 1
            $id = $row['id']; 

                    if(in_array($id, $_SESSION['stockID'])){ //Check if an id has been seen
                        break; //I would like this thing start the loop again
                    } else {
                        echo $row['type'];
                        echo "<br>";
                        echo $row['phrases'];
                        array_push($_SESSION['stockID'], $id);
                    }
        }
    } else {
        echo "There is a problem, could you refresh the page ?";
    }
?>

问题是当in_“array()= true”屏幕上没有任何内容时,所以我只想让if循环重新开始并搜索一个从未出现过的内容。
谢谢大家!

1 个答案:

答案 0 :(得分:0)

在这种情况下,您试图找出是否发生特定情况 - 只需检查该特定条件。在您的情况下,请检查:

    if(!in_array($id, $_SESSION['stockID'])){
                    echo $row['type'];
                    echo "<br>";
                    echo $row['phrases'];
                    array_push($_SESSION['stockID'], $id);
                }