设计优秀的 PHP 测验脚本

发布于 2024-09-08 09:57:20 字数 1536 浏览 1 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

维持三分热 2024-09-15 09:57:20

从我个人的角度来看,如果我被要求设计类似的东西,我会执行以下操作:

  • 如果我不想使用数据库资源,我会有一个“开始测验之前”步骤,我会在其中获取我的问题来自数据库
  • 每个用户(即使没有注册)都有一个独特的会话;从数据库获取的数据将位于会话中(并且因为会话存储在服务器端,所以我不关心对其进行加密)
  • 会话虽然有点问题,但是使用cookie并加密数据似乎更有效比进行更长的会话(也许学生需要一些时间来解决测验,而 5 分钟的会话会有点短,但这不是一个大问题,您可以根据需要进行设置。
  • 答案也存储在会话中,位于最后,您只需将会话中的结果与会话中的答案进行比较,这是一项非常基本的任务:如果结果存储在单独的数组中,您只需调用 in_array()每个答案(数组是正确的结果)并增加一个变量以了解有多少问题被正确回答(如果每个问题都具有相同的“值”,则有效)

享受吧!

From my personal point of view, if I'd be asked to design something similar, I would do the following:

  • If I don't want to use database resources I'd have a "before starting quizz" step where I'd fetch my questions from the database
  • Each user (even if not registered) has a unique session; the data fetched from the database would sit right there on the session (and because session is stored server side I wouldn't care for encrypting it)
  • Session it's a bit problematic, though, but using the cookie and encrypting the data seems more work rather than making a longer session (maybe the students need some time to solve the quizz and a 5minute session would be a little low, but that's not a big problem, you can set it as you wish
  • The answers are also stored on the session, at the end you just need to compare the results in the session with the answers in the session which is a pretty basic task: if the results are stored in a separate array, you could just call in_array() on each answer (the array being the correct results) and increment a variable to know how many questions are answered right (that works if every question has the same "value")

Enjoy!

冷弦 2024-09-15 09:57:20

您显然需要一个快速的缓存层。为此,我建议使用 memcached 或 APC。会话数据也可能起作用。

也就是说,您可能最好使用全栈 PHP 框架。好的缓存会为你管理。我建议研究 symfony 或 CakePHP。

然而,真正的问题是:在确定确实存在问题之前,您是否试图预先解决这个问题?这是一个糟糕的做法。在尝试解决问题之前,请确保您确实遇到了问题。

You clearly want a fast cache layer. I suggest either memcached or APC for that purpose. Session data may also work.

That said, you'd probably be better off using a full-stack PHP framework for this. A good one will manage caches for you. I suggest looking into symfony or CakePHP.

Here's the real question, though: are you trying to address this problem up-front before you have established that there actually is a problem? This is a bad approach. Make sure you have a problem before you try to solve it.

圈圈圆圆圈圈 2024-09-15 09:57:20

如果你必须一一显示它们,你需要将结果存储在某个地方,并且由于 http 是无状态的,你有 2 个选择:db 或 cookie。现在,我认为最好将答案存储在像这样的 var 中的 cookie 中

"a:1,b:2,question:answer,.."

,然后在最后分解它们并检查结果。

If you have to show them one by one, you need to store the results somewhere and since http is stateless you have 2 choices: db or cookie. Now, in my opinion is better to store the answers in a cookie in a var like this

"a:1,b:2,question:answer,.."

and then explode them at the end and check the result.

夜还是长夜 2024-09-15 09:57:20

你不应该想要这样。 Web 应用程序将中间结果存储在数据库中是完全正常的。大多数应用程序每页执行多次数据库查询。

您可以使用会话。这允许您存储与用户耦合的数据。但是,会话数据也存储在某个地方,因此效率并不是将其存储在会话而不是数据库中的原因。

You should not want that. It is perfectly normal for a webapp to store intermediate results in a database. Most apps do several database queries per page.

You could use sessions. This allows you to store data which is coupled to a user. However, session data is also stored somewhere, so efficiency is not a reason to store it in a session instead of a database.

桃扇骨 2024-09-15 09:57:20

您想要做的是一种向导式流程,每个页面上都有一个“下一步”按钮。对于此类事情,将结果存储在会话中是很常见的。

如果您存储在会话中随机提取的问题,如下所示:

$_SESSION['questions'] = array(
    array(
        'question'    => 'What is the response to life ?', 
        'answer'      => '42', 
        'user_answer' => null,   // Will contain the user answer
        'answered'    => false), // Will be set to true once the question is answered
    array(
        'question'    => 'What is the color of the sky ?',
        'answer'      => 'blue',
        'user_answer' => null, 
        'answered'    => false)
);

然后您可以迭代问题,跟踪已回答的问题。

What you want to do is a sort of wizard-style flow, with a Next button on each page. This is common to store the results in session for such things.

If you store the questions extracted randomly in session, like this :

$_SESSION['questions'] = array(
    array(
        'question'    => 'What is the response to life ?', 
        'answer'      => '42', 
        'user_answer' => null,   // Will contain the user answer
        'answered'    => false), // Will be set to true once the question is answered
    array(
        'question'    => 'What is the color of the sky ?',
        'answer'      => 'blue',
        'user_answer' => null, 
        'answered'    => false)
);

Then you could iterate over the questions, keeping trace of the answered questions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文