doukao8851 2018-09-07 21:49
浏览 48

如何在php查询中第二次停止相同的数据显示?

i try to development exam management system. I do not want same question or same id not showing second time or other any time exam a user. How can this condition be given?

my function is:

public function qustionShow($question, $limit=4){
    $show = $this->conn->query("select * from question where cat_id ='$question' ORDER BY RAND() LIMIT $limit");
    while ($row=$show->fetch_array(MYSQLI_ASSOC)){
        $this->qus[]=$row;
    }
    return $this->qus;  
}
  • 写回答

1条回答 默认 最新

  • dqkf36241 2018-09-07 22:34
    关注

    You would need to keep track of which questions have already been asked. you can save the question id's to the Session if it's you don't want the questions to be selected again only for the session.

    you could initialize an array to the session $_SESSION['questions_asked'] = array(); and then once a question is asked you would

    array_push([THE QUESTION ID], $_SESSION['questions_asked']);
    

    of course you need to replace [THE QUESTION ID] with the sql id for whatever question was asked

    keep in mind you would need to modify your query to account for anything saved in the session.

    If you don't want them to ever be shown again you would need to record which questions a user has seen and would need to store that persistently in the database probably.

    possibly you can have a table to store those in for each user

    user_question_asked

    with at least these 2 columns

    [user_id][question_id]

    so each time a question is asked you insert the current user id and question id

    then your query could be

    SELECT * FROM question where cat_id ='$question' AND [QUESTION_ID] NOT IN 
    (SELECT question_id from  user_question_asked where user_id [CURRENT USER's ID] 
    ) ORDER BY RAND() LIMIT $limit;`
    

    Hope that helps, I'm not sure what your table / column structure is, but those are a couple suggestions I have for addressing this problem.

    评论

报告相同问题?

悬赏问题

  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器