明智地获取并显示记录部分

时间:2015-07-07 06:45:10

标签: php mysql

3个表quiz_sectionsquiz_attemptsquestion_attempts表格的列如下图所示。

$USER->id是当前登录的用户ID变量。

这是我的代码

<?php
echo "Current User ID : ".$USER->id."<br />";
$quizsections = mysql_query("SELECT * FROM quiz_sections");
while($quizsectionsrslt = mysql_fetch_array($quizsections)){
    echo $quizsectionsheading = $quizsectionsrslt['heading']."<br />";
    $quizsectionsquizid = $quizsectionsrslt['quizid'];
    $quizsectionsfirstslot = $quizsectionsrslt['firstslot'];
    $quizattempts = mysql_query("SELECT * FROM quiz_attempts WHERE `quiz`=$quizsectionsquizid AND `userid`=$USER->id");
    while($quizattemptsrslt = mysql_fetch_array($quizattempts)){
        $quizattemptsids = $quizattemptsrslt['id'];
        $quizattemptsattempt = $quizattemptsrslt['attempt'];
        $quizattemptsquiz = $quizattemptsrslt['quiz'];
        $questionattempts = mysql_query("SELECT * FROM question_attempts WHERE `questionusageid`=$quizattemptsids");
        while($questionattemptsrslt = mysql_fetch_array($questionattempts)){
            echo $questionattemptsid = $questionattemptsrslt['id'];
        }
    }
}

我得到的输出

enter image description here

enter image description here

我想分割userid=3

的结果

性向

  

1 2 3 4 ... 10

算术

  

11 12 13 ... 20

推理

  

21 22 23 ... 30

计算机

  

31 32 33 ... 40

userid=4的结果为41到80。

这些是表结构。

enter image description here

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:1)

您需要在thr $quizsectionsfirstslot查询中使用question_attempts

$questionattempts = mysql_query("SELECT * FROM question_attempts 
                                WHERE `questionusageid`=$quizattemptsids 
                                AND slot BETWEEN $quizsectionsfirstslot AND $quizsectionsfirstslot + 9");