FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Most efficient way to randomize a quiz from a database
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
Most efficient way to randomize a quiz from a database [message #170563] Mon, 08 November 2010 16:08 Go to next message
Mason Barge is currently offline  Mason Barge
Messages: 5
Registered: November 2010
Karma: 0
Junior Member
I'm creating a quiz using PHP 5.3 and MySQL (mysqli). The database has
about 1000 questions and answers, in categories, and I want to ask 12
randomly selected questions, one at a time.

Which would be more efficient:

1) Make one call to the database and build an array, stored in session
variables, of 12 randomly chosen rows; or

2) Make 12 requests to the database?

I'm open to other general approaches if anyone has a suggestion I haven't
considered.

I'd like to avoid reloading the page after every answer and I don't see
how I could use Ajax with #1. The quiz has to be in PHP because I need to
generate the final score as a PHP variable, not a javascript variable.
Re: Most efficient way to randomize a quiz from a database [message #170565 is a reply to message #170563] Mon, 08 November 2010 16:26 Go to previous messageGo to next message
Captain Paralytic is currently offline  Captain Paralytic
Messages: 204
Registered: September 2010
Karma: 0
Senior Member
On Nov 8, 4:08 pm, Mason Barge <masonba...@gmail.com> wrote:
> I'm creating a quiz using PHP 5.3 and MySQL (mysqli).  The database has
> about 1000 questions and answers, in categories, and I want to ask 12
> randomly selected questions, one at a time.
>
> Which would be more efficient:
>
> 1) Make one call to the database and build an array, stored in session
> variables, of 12 randomly chosen rows;  or
>
> 2) Make 12 requests to the database?
>
> I'm open to other general approaches if anyone has a suggestion I haven't
> considered.
>
> I'd like to avoid reloading the page after every answer and I don't see
> how I could use Ajax with #1.  The quiz has to be in PHP because I need to
> generate the final score as a PHP variable, not a javascript variable.

In #1 you can have the query return 12 randomly selected rows.

Regardless of which method you choose, you would store the state
information in a session variable and thus whether you use AJAX or not
is irrelevant.
Re: Most efficient way to randomize a quiz from a database [message #170568 is a reply to message #170563] Mon, 08 November 2010 16:45 Go to previous messageGo to next message
Matthew Leonhardt is currently offline  Matthew Leonhardt
Messages: 9
Registered: November 2010
Karma: 0
Junior Member
"Mason Barge" <masonbarge(at)gmail(dot)com> wrote in message
news:s87gd61ldtnkeuuc817uiej2qmv0s4vc88(at)4ax(dot)com...
> I'm creating a quiz using PHP 5.3 and MySQL (mysqli). The database has
> about 1000 questions and answers, in categories, and I want to ask 12
> randomly selected questions, one at a time.
>
> Which would be more efficient:
>
> 1) Make one call to the database and build an array, stored in session
> variables, of 12 randomly chosen rows; or

Most likely this method. You're talking about a fairly small amount of data
though, so I highly doubt you need to be worried about efficiency...

> 2) Make 12 requests to the database?
>
> I'm open to other general approaches if anyone has a suggestion I haven't
> considered.

You could also opt for a hybrid solution...generate a session array
containing 12 primary keys and then with each page load, just pop the next
question, so to speak :)

> I'd like to avoid reloading the page after every answer and I don't see
> how I could use Ajax with #1. The quiz has to be in PHP because I need to
> generate the final score as a PHP variable, not a javascript variable.

I don't see how #1 prevents an AJAX solution. What's your cause for worry
about that? Regarding the language in which the final score is calculated,
look at json_encode() and json_decode() for convenient data-passing between
the two languages.
Re: Most efficient way to randomize a quiz from a database [message #170570 is a reply to message #170568] Mon, 08 November 2010 18:52 Go to previous messageGo to next message
Mason Barge is currently offline  Mason Barge
Messages: 5
Registered: November 2010
Karma: 0
Junior Member
On Mon, 8 Nov 2010 11:45:16 -0500, "Matthew Leonhardt"
<matt(at)mattleonhardtmusic(dot)com> wrote:

> "Mason Barge" <masonbarge(at)gmail(dot)com> wrote in message
> news:s87gd61ldtnkeuuc817uiej2qmv0s4vc88(at)4ax(dot)com...
>> I'm creating a quiz using PHP 5.3 and MySQL (mysqli). The database has
>> about 1000 questions and answers, in categories, and I want to ask 12
>> randomly selected questions, one at a time.
>>
>> Which would be more efficient:
>>
>> 1) Make one call to the database and build an array, stored in session
>> variables, of 12 randomly chosen rows; or
>
> Most likely this method. You're talking about a fairly small amount of data
> though, so I highly doubt you need to be worried about efficiency...
>
>> 2) Make 12 requests to the database?
>>
>> I'm open to other general approaches if anyone has a suggestion I haven't
>> considered.
>
> You could also opt for a hybrid solution...generate a session array
> containing 12 primary keys and then with each page load, just pop the next
> question, so to speak :)

Yeah that's what I'd do.

>> I'd like to avoid reloading the page after every answer and I don't see
>> how I could use Ajax with #1. The quiz has to be in PHP because I need to
>> generate the final score as a PHP variable, not a javascript variable.
>
> I don't see how #1 prevents an AJAX solution. What's your cause for worry
> about that? Regarding the language in which the final score is calculated,
> look at json_encode() and json_decode() for convenient data-passing between
> the two languages.
>

Thanks so much, I've been out of touch with PHP for a long time and didn't
know about JSON. It solves so many problems with this kind of thing.
Re: Most efficient way to randomize a quiz from a database [message #170589 is a reply to message #170570] Tue, 09 November 2010 09:48 Go to previous messageGo to next message
Erwin Moller is currently offline  Erwin Moller
Messages: 228
Registered: September 2010
Karma: 0
Senior Member
On 11/8/2010 7:52 PM, Mason Barge wrote:
> On Mon, 8 Nov 2010 11:45:16 -0500, "Matthew Leonhardt"
> <matt(at)mattleonhardtmusic(dot)com> wrote:
>
>> "Mason Barge"<masonbarge(at)gmail(dot)com> wrote in message
>> news:s87gd61ldtnkeuuc817uiej2qmv0s4vc88(at)4ax(dot)com...
>>> I'm creating a quiz using PHP 5.3 and MySQL (mysqli). The database has
>>> about 1000 questions and answers, in categories, and I want to ask 12
>>> randomly selected questions, one at a time.
>>>
>>> Which would be more efficient:
>>>
>>> 1) Make one call to the database and build an array, stored in session
>>> variables, of 12 randomly chosen rows; or
>>
>> Most likely this method. You're talking about a fairly small amount of data
>> though, so I highly doubt you need to be worried about efficiency...
>>
>>> 2) Make 12 requests to the database?
>>>
>>> I'm open to other general approaches if anyone has a suggestion I haven't
>>> considered.
>>
>> You could also opt for a hybrid solution...generate a session array
>> containing 12 primary keys and then with each page load, just pop the next
>> question, so to speak :)
>
> Yeah that's what I'd do.
>
>>> I'd like to avoid reloading the page after every answer and I don't see
>>> how I could use Ajax with #1. The quiz has to be in PHP because I need to
>>> generate the final score as a PHP variable, not a javascript variable.
>>
>> I don't see how #1 prevents an AJAX solution. What's your cause for worry
>> about that? Regarding the language in which the final score is calculated,
>> look at json_encode() and json_decode() for convenient data-passing between
>> the two languages.
>>
>
> Thanks so much, I've been out of touch with PHP for a long time and didn't
> know about JSON. It solves so many problems with this kind of thing.

Hi Mason Barge,

I don't want to be too negative, but you exaggerate things.
JSon is just a dataformat definition to exchange data.

You have *your* serverside PHP scripts, and *your* JavaScript in the
browser.
So you might as well make up your own format and use that. ;-)
I don't see how JSON is solving so many problems for you.

Regards,
Erwin Moller


--
"There are two ways of constructing a software design: One way is to
make it so simple that there are obviously no deficiencies, and the
other way is to make it so complicated that there are no obvious
deficiencies. The first method is far more difficult."
-- C.A.R. Hoare
Re: Most efficient way to randomize a quiz from a database [message #170597 is a reply to message #170589] Tue, 09 November 2010 14:55 Go to previous messageGo to next message
Matthew Leonhardt is currently offline  Matthew Leonhardt
Messages: 9
Registered: November 2010
Karma: 0
Junior Member
"Erwin Moller"
<Since_humans_read_this_I_am_spammed_too_much(at)spamyourself(dot)com> wrote in
message news:4cd91905$0$41103$e4fe514c(at)news(dot)xs4all(dot)nl...
> On 11/8/2010 7:52 PM, Mason Barge wrote:
>> On Mon, 8 Nov 2010 11:45:16 -0500, "Matthew Leonhardt"
>> <matt(at)mattleonhardtmusic(dot)com> wrote:
>>
>>> "Mason Barge"<masonbarge(at)gmail(dot)com> wrote in message
>>> news:s87gd61ldtnkeuuc817uiej2qmv0s4vc88(at)4ax(dot)com...
>>>> I'm creating a quiz using PHP 5.3 and MySQL (mysqli). The database has
>>>> about 1000 questions and answers, in categories, and I want to ask 12
>>>> randomly selected questions, one at a time.
>>>>
>>>> Which would be more efficient:
>>>>
>>>> 1) Make one call to the database and build an array, stored in session
>>>> variables, of 12 randomly chosen rows; or
>>>
>>> Most likely this method. You're talking about a fairly small amount of
>>> data
>>> though, so I highly doubt you need to be worried about efficiency...
>>>
>>>> 2) Make 12 requests to the database?
>>>>
>>>> I'm open to other general approaches if anyone has a suggestion I
>>>> haven't
>>>> considered.
>>>
>>> You could also opt for a hybrid solution...generate a session array
>>> containing 12 primary keys and then with each page load, just pop the
>>> next
>>> question, so to speak :)
>>
>> Yeah that's what I'd do.
>>
>>>> I'd like to avoid reloading the page after every answer and I don't see
>>>> how I could use Ajax with #1. The quiz has to be in PHP because I need
>>>> to
>>>> generate the final score as a PHP variable, not a javascript variable.
>>>
>>> I don't see how #1 prevents an AJAX solution. What's your cause for
>>> worry
>>> about that? Regarding the language in which the final score is
>>> calculated,
>>> look at json_encode() and json_decode() for convenient data-passing
>>> between
>>> the two languages.
>>>
>>
>> Thanks so much, I've been out of touch with PHP for a long time and
>> didn't
>> know about JSON. It solves so many problems with this kind of thing.
>
> Hi Mason Barge,
>
> I don't want to be too negative, but you exaggerate things.
> JSon is just a dataformat definition to exchange data.

Yes...

> You have *your* serverside PHP scripts, and *your* JavaScript in the
> browser.
> So you might as well make up your own format and use that. ;-)
> I don't see how JSON is solving so many problems for you.

....how does it just being a format definition (with provided functions for
performing data conversion) NOT solve problems? Why was it functionalized
into the language if it *didn't* solve problems?

Passing a complex JS object to a PHP AJAX script--problem. Using JSON as an
encoding method--solution.
Re: Most efficient way to randomize a quiz from a database [message #170598 is a reply to message #170597] Tue, 09 November 2010 16:09 Go to previous messageGo to next message
Erwin Moller is currently offline  Erwin Moller
Messages: 228
Registered: September 2010
Karma: 0
Senior Member
On 11/9/2010 3:55 PM, Matthew Leonhardt wrote:
> "Erwin Moller"
> <Since_humans_read_this_I_am_spammed_too_much(at)spamyourself(dot)com> wrote in
> message news:4cd91905$0$41103$e4fe514c(at)news(dot)xs4all(dot)nl...
>> On 11/8/2010 7:52 PM, Mason Barge wrote:
>>> On Mon, 8 Nov 2010 11:45:16 -0500, "Matthew Leonhardt"
>>> <matt(at)mattleonhardtmusic(dot)com> wrote:
>>>
>>>> "Mason Barge"<masonbarge(at)gmail(dot)com> wrote in message
>>>> news:s87gd61ldtnkeuuc817uiej2qmv0s4vc88(at)4ax(dot)com...
>>>> > I'm creating a quiz using PHP 5.3 and MySQL (mysqli). The database has
>>>> > about 1000 questions and answers, in categories, and I want to ask 12
>>>> > randomly selected questions, one at a time.
>>>> >
>>>> > Which would be more efficient:
>>>> >
>>>> > 1) Make one call to the database and build an array, stored in session
>>>> > variables, of 12 randomly chosen rows; or
>>>>
>>>> Most likely this method. You're talking about a fairly small amount of
>>>> data
>>>> though, so I highly doubt you need to be worried about efficiency...
>>>>
>>>> > 2) Make 12 requests to the database?
>>>> >
>>>> > I'm open to other general approaches if anyone has a suggestion I
>>>> > haven't
>>>> > considered.
>>>>
>>>> You could also opt for a hybrid solution...generate a session array
>>>> containing 12 primary keys and then with each page load, just pop the
>>>> next
>>>> question, so to speak :)
>>>
>>> Yeah that's what I'd do.
>>>
>>>> > I'd like to avoid reloading the page after every answer and I don't see
>>>> > how I could use Ajax with #1. The quiz has to be in PHP because I need
>>>> > to
>>>> > generate the final score as a PHP variable, not a javascript variable.
>>>>
>>>> I don't see how #1 prevents an AJAX solution. What's your cause for
>>>> worry
>>>> about that? Regarding the language in which the final score is
>>>> calculated,
>>>> look at json_encode() and json_decode() for convenient data-passing
>>>> between
>>>> the two languages.
>>>>
>>>
>>> Thanks so much, I've been out of touch with PHP for a long time and
>>> didn't
>>> know about JSON. It solves so many problems with this kind of thing.
>>
>> Hi Mason Barge,
>>
>> I don't want to be too negative, but you exaggerate things.
>> JSon is just a dataformat definition to exchange data.
>
> Yes...
>
>> You have *your* serverside PHP scripts, and *your* JavaScript in the
>> browser.
>> So you might as well make up your own format and use that. ;-)
>> I don't see how JSON is solving so many problems for you.

Hi Matthew,

It seems I have irritated you, which was not my intention.
Read on.


>
> ...how does it just being a format definition (with provided functions for
> performing data conversion) NOT solve problems? Why was it functionalized
> into the language if it *didn't* solve problems?

Strawman fallacy: You are not attacking my statements or position (or
intention!), but a ridiculed version of it.

I never said JSON doesn't solve problems.
I have no problems with JSON at all: JSON is a nice way to transport
data, and thus a time-saver in some situations.


>
> Passing a complex JS object to a PHP AJAX script--problem. Using JSON as an
> encoding method--solution.
>

Yes, I know that, but.... A complex JavaScript object? For a quiz?
I am sure you can think up a few ways to pack questions and answers and
unpack them in JavaScript.
(I programmed those in hidden/refresh frames long before XMLHttpRequest
was there. That really isn't complex at all.)

The reason I replied (to Mason Barge) was only because I got the
impression he was missing something: Like he didn't know/understand
that he could send *any* data that suited him with a XMLHttpRequest
alone, with or without JSON.
He also seems to think JSON is something of PHP ("Thanks so much, I've
been out of touch with PHP for a long time and didn't know about JSON.").
If JSON is anything, it is JavaScript. Other languages simply offer the
JSON-packing.


So I told Mason he can send *any* data since it is his PHP output and
his JavaScript. I think that is important for him to understand.

Call me olf-fashioned, but I think a programmer should first understand
the nature of a XMLHttpRequest, exchange some data, and then, when
understanding the process, use JSON.
In that order.

Regards,
Erwin Moller

--
"There are two ways of constructing a software design: One way is to
make it so simple that there are obviously no deficiencies, and the
other way is to make it so complicated that there are no obvious
deficiencies. The first method is far more difficult."
-- C.A.R. Hoare
Re: Most efficient way to randomize a quiz from a database [message #170599 is a reply to message #170598] Tue, 09 November 2010 18:46 Go to previous message
Matthew Leonhardt is currently offline  Matthew Leonhardt
Messages: 9
Registered: November 2010
Karma: 0
Junior Member
"Erwin Moller"
<Since_humans_read_this_I_am_spammed_too_much(at)spamyourself(dot)com> wrote in
message news:4cd9724a$0$81484$e4fe514c(at)news(dot)xs4all(dot)nl...
> On 11/9/2010 3:55 PM, Matthew Leonhardt wrote:
>> "Erwin Moller"
>> <Since_humans_read_this_I_am_spammed_too_much(at)spamyourself(dot)com> wrote in
>> message news:4cd91905$0$41103$e4fe514c(at)news(dot)xs4all(dot)nl...
>>> On 11/8/2010 7:52 PM, Mason Barge wrote:
>>>> On Mon, 8 Nov 2010 11:45:16 -0500, "Matthew Leonhardt"
>>>> <matt(at)mattleonhardtmusic(dot)com> wrote:
>>>>
>>>> > "Mason Barge"<masonbarge(at)gmail(dot)com> wrote in message
>>>> > news:s87gd61ldtnkeuuc817uiej2qmv0s4vc88(at)4ax(dot)com...
>>>> >> I'm creating a quiz using PHP 5.3 and MySQL (mysqli). The database
>>>> >> has
>>>> >> about 1000 questions and answers, in categories, and I want to ask 12
>>>> >> randomly selected questions, one at a time.
>>>> >>
>>>> >> Which would be more efficient:
>>>> >>
>>>> >> 1) Make one call to the database and build an array, stored in
>>>> >> session
>>>> >> variables, of 12 randomly chosen rows; or
>>>> >
>>>> > Most likely this method. You're talking about a fairly small amount
>>>> > of
>>>> > data
>>>> > though, so I highly doubt you need to be worried about efficiency...
>>>> >
>>>> >> 2) Make 12 requests to the database?
>>>> >>
>>>> >> I'm open to other general approaches if anyone has a suggestion I
>>>> >> haven't
>>>> >> considered.
>>>> >
>>>> > You could also opt for a hybrid solution...generate a session array
>>>> > containing 12 primary keys and then with each page load, just pop the
>>>> > next
>>>> > question, so to speak :)
>>>>
>>>> Yeah that's what I'd do.
>>>>
>>>> >> I'd like to avoid reloading the page after every answer and I don't
>>>> >> see
>>>> >> how I could use Ajax with #1. The quiz has to be in PHP because I
>>>> >> need
>>>> >> to
>>>> >> generate the final score as a PHP variable, not a javascript
>>>> >> variable.
>>>> >
>>>> > I don't see how #1 prevents an AJAX solution. What's your cause for
>>>> > worry
>>>> > about that? Regarding the language in which the final score is
>>>> > calculated,
>>>> > look at json_encode() and json_decode() for convenient data-passing
>>>> > between
>>>> > the two languages.
>>>> >
>>>>
>>>> Thanks so much, I've been out of touch with PHP for a long time and
>>>> didn't
>>>> know about JSON. It solves so many problems with this kind of thing.
>>>
>>> Hi Mason Barge,
>>>
>>> I don't want to be too negative, but you exaggerate things.
>>> JSon is just a dataformat definition to exchange data.
>>
>> Yes...
>>
>>> You have *your* serverside PHP scripts, and *your* JavaScript in the
>>> browser.
>>> So you might as well make up your own format and use that. ;-)
>>> I don't see how JSON is solving so many problems for you.
>
> Hi Matthew,
>
> It seems I have irritated you, which was not my intention.
> Read on.

Not in the least. My days of allowing usenet to irritate me are long in my
past :)

I simply thought that your reaction to his excitement over the discovery of
json_ functions was undue and the "too negative" that you meant to avoid.

>
>>
>> ...how does it just being a format definition (with provided functions
>> for
>> performing data conversion) NOT solve problems? Why was it
>> functionalized
>> into the language if it *didn't* solve problems?
>
> Strawman fallacy: You are not attacking my statements or position (or
> intention!), but a ridiculed version of it.

Your position, if I understood it correctly, was that JSON is nothing to get
excited about because he still has to write the PHP/JS. You also implied
that he could just as easily create and implement his own format.

The OP expressly issued a concern about the language in which the quiz was
processed because he was unsure of how to pass data between the two
technologies. He hadn't expressed concerns over the implentation of
processing the form or initiating the AJAX XML request...

Hardly a strawman.

> I never said JSON doesn't solve problems.
> I have no problems with JSON at all: JSON is a nice way to transport data,
> and thus a time-saver in some situations.

Which, as I understood the OP, is exactly what he was after...

>
>>
>> Passing a complex JS object to a PHP AJAX script--problem. Using JSON as
>> an
>> encoding method--solution.
>>
>
> Yes, I know that, but.... A complex JavaScript object? For a quiz?

Complex in the sense of something beyond simple (scalar) or compound
(array). For example, it could be an array containing variables and other
arrays. It doesn't have to be mind-warpingly complicated.

> I am sure you can think up a few ways to pack questions and answers and
> unpack them in JavaScript.
> (I programmed those in hidden/refresh frames long before XMLHttpRequest
> was there. That really isn't complex at all.)
>
> The reason I replied (to Mason Barge) was only because I got the
> impression he was missing something: Like he didn't know/understand that
> he could send *any* data that suited him with a XMLHttpRequest alone, with
> or without JSON.

I didn't get that impression.

> He also seems to think JSON is something of PHP ("Thanks so much, I've
> been out of touch with PHP for a long time and didn't know about JSON.").
> If JSON is anything, it is JavaScript. Other languages simply offer the
> JSON-packing.

I read that as "I didn't know about the JSON functions."

>
> So I told Mason he can send *any* data since it is his PHP output and his
> JavaScript. I think that is important for him to understand.

Agreed. Again, my read of the OP was that he did understand. But, if he's
followed the thread down this rabbit hole, I'm *sure* he does now.

> Call me olf-fashioned, but I think a programmer should first understand
> the nature of a XMLHttpRequest, exchange some data, and then, when
> understanding the process, use JSON.
> In that order.

No disagreements there. I didn't evaluate the OP's question in a manner
that indicated to me a need for him to brush up on AJAX methodology.
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: how to retrieve xml data from three tables of mysql
Next Topic: Cronjob Apache module v CGI
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Wed Nov 27 23:54:03 GMT 2024

Total time taken to generate the page: 0.02246 seconds