简体   繁体   中英

Break while loop and get the count

I'm unable to break the while loop.

<?php $quizsections = mysql_query("SELECT * FROM quiz_sections");
while($quizsectionsrslt = mysql_fetch_array($quizsections)){
    $quizsectionsid        = $quizsectionsrslt['id'];
    $quizsectionsheading   = $quizsectionsrslt['heading'];
    $quizsectionsquizid    = $quizsectionsrslt['quizid'];
    $quizsectionsfirstslot = $quizsectionsrslt['firstslot'];
    echo $quizsectionsheading."<br />";

    $quizslots = mysql_query("SELECT * FROM quiz_slots WHERE `quizid`=$quizsectionsquizid");
    while($quizslotsrslt = mysql_fetch_array($quizslots)){
        $quizslotids = $quizslotsrslt['questionid'];
        $questions = mysql_query("SELECT * FROM question WHERE `id`=$quizslotids");

        while($questionsrslt = mysql_fetch_array($questions)){
            echo $questiontext = $questionsrslt['id']."<br />";
        }
    }
}?>

It is displaying the output like

Aptitude

1 2 3 .... 40

Arithmetic

1 2 3 ... 40

Reasoning

1 2 3 ... 40

Computers

1 2 3 ... 40

But the output i want

Aptitude

1 2 3 ... 10

Arithmetic

11 12 13 ... 25

Reasoning

26 27 28 ... 30

Computers

31 32 33 ... 40

Tables structure

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

You will want to use LIMIT on the MySQL query to change the output based on the first slot

<?php $quizsections = mysql_query("SELECT * FROM quiz_sections");
while($quizsectionsrslt = mysql_fetch_array($quizsections)){
    $quizsectionsid        = $quizsectionsrslt['id'];
    $quizsectionsheading   = $quizsectionsrslt['heading'];
    $quizsectionsquizid    = $quizsectionsrslt['quizid'];
    $quizsectionsfirstslot = $quizsectionsrslt['firstslot'];
    echo $quizsectionsheading."<br />";

    $quizslots = mysql_query("SELECT * FROM quiz_slots WHERE `quizid`=$quizsectionsquizid LIMIT ".($quizsectionsfirstslot-1).", 10");
    while($quizslotsrslt = mysql_fetch_array($quizslots)){
        $quizslotids = $quizslotsrslt['questionid'];
        $questions = mysql_query("SELECT * FROM question WHERE `id`=$quizslotids");

        while($questionsrslt = mysql_fetch_array($questions)){
            echo $questiontext = $questionsrslt['id']."<br />";
        }
    }
}?>

You can stop executing a loop by adding:

break;

On the line that you want to stop

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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