Forum Moderators: coopster

Message Too Old, No Replies

Display randomized questions from a database?

Questionnaire items to be displayed at random and responses stored

         

ed_edin

10:41 am on Jun 2, 2004 (gmt 0)

10+ Year Member



Hi

I would like to store 300 questions in a MySQL database, and configure a webpage to display them in groups of 50 at random, then store the responses. Could anyone recommend a script or strategy to do this please?

Many thanks,
Ed

hughie

10:50 am on Jun 2, 2004 (gmt 0)

10+ Year Member



You can create a random selection out of MySQL using something like this..

$result = mysql_query("SELECT *,MD5( RAND( ) ) AS RandQues FROM questions ORDER BY RandQues DESC LIMIT 50 ");

The "MD5( RAND( ) ) AS RandQues" creates a random number which is assigned to each entry, you then sort by this random number.

ta,
Hughie

ed_edin

11:08 am on Jun 2, 2004 (gmt 0)

10+ Year Member



Thanks.

How do I echo (print) variable names, rather than their values?

I want to print a list of questions which are stored in the database, but

echo stripslashes($row['question1']);

would print the stored data rather than that question name itself.

(sorry, my knowledge is beginner level)

hughie

11:32 am on Jun 2, 2004 (gmt 0)

10+ Year Member



errr, i think what your saying is that your creating a new column for every question? thats not needed.

set your table 'questions' up like:

ID,Question_Contents,Question_Answer

then in your php:
<?php
$result = mysql_query("SELECT *,MD5( RAND( ) ) AS RandQues FROM questions ORDER BY RandQues DESC LIMIT 50 ");

$count=0;
while($row=mysql_fetch_array($result))
{
$count+=1;

$text.='Question '.$count.'<br>
'.$row['Question_Contents'].'<br><br>';

}
echo $text;
?>

That should output

question 1
This is question one

Question 2
This is Question 2

etc....
no matter what order they come out as they will always be question 1, 2, 3 etc.

Is that what your after?

ta,
hughie

ed_edin

12:51 pm on Jun 2, 2004 (gmt 0)

10+ Year Member



I'd like it to look something like this. The question comes from the MySQL database, and answer is stored:

{QUESTION ONE INSERTED HERE AUTOMATICALLY}
<label>
<input type="radio" name="q1" value="1">
Strongly Agree</label>
<br>
<label>
<input type="radio" name="q1" value="2">
Agree</label>
<br>
<label>
<input type="radio" name="q1" value="3">
Unsure</label>
<br>
<label>
<input type="radio" name="q1" value="4">
Disagree</label>
<br>
<label>
<input type="radio" name="q1" value="5">
Strongly Disagree</label>
<input type="submit" value="submit">

hughie

2:49 pm on Jun 2, 2004 (gmt 0)

10+ Year Member



well then my solution will work like so...

$count=0;
while($row=mysql_fetch_array($result))
{
$count+=1;

$text.='<b>Question '.$count.'</b><br>
'.$row['Question_Contents'].'<br>
<label>
<input type="radio" name="q'.$count.'" value="1">
Strongly Agree</label>
<br>
<label>
<input type="radio" name="q'.$count.'" value="2">
Agree</label>
<br>
<label>
<input type="radio" name="q'.$count.'" value="3">
Unsure</label>
<br>
<label>
<input type="radio" name="q'.$count.'" value="4">
Disagree</label>
<br>
<label>
<input type="radio" name="q'.$count.'" value="5">
Strongly Disagree</label>
<br><br>';

}
echo $text;
?>

looping through the while loop for 50 questions

ed_edin

4:15 pm on Jun 2, 2004 (gmt 0)

10+ Year Member



I'll give that a go, many thanks.

ed_edin

10:11 am on Jun 3, 2004 (gmt 0)

10+ Year Member



So I need to create a line in the table called 'Question_Contents', and fill it with the questions I want?

hughie

9:55 pm on Jun 3, 2004 (gmt 0)

10+ Year Member



yeah, thats the way, no need to create a field for each question.

If your new to php and mysql, have a look on hotscripts.com in the php section, there are lots of scripts available to download. pick one of those apart and see how they do it, its a good way of getting to grips with things.

There are probably some nice tutorials out there as well.

ta,
Hugie

ed_edin

1:21 pm on Jun 7, 2004 (gmt 0)

10+ Year Member



That works! Many thanks - it shows randomized questions with five radio buttons.

How can I create short variables names for each response, to carry forward onto my next page?

If I was writing this without the randomization, I would use something like these:

// create short variable names
$q1=$HTTP_POST_VARS['q1'];
$q2=$HTTP_POST_VARS['q2'];
$q3=$HTTP_POST_VARS['q3'];
$q4=$HTTP_POST_VARS['q4'];
$q5=$HTTP_POST_VARS['q5'];
// etc.

$query = "insert into items values
('".$q1."', '".$q2."', '".$q3."', '".$q4."', '".$q5."')";
// to enter questions 1 to 5 into the table

But in the code above, there is only:

name="q'.$count.'

Many thanks