...

View Full Version : Need some help!



litoburrito
01-07-2010, 02:04 AM
Alright im new to PHP and MySQL but i heard from a friend that in order to do what im thinking of i'll have to learn it. But i was just wondering if any of you guys could help me even further. I read w3schools.net info and have a basic understanding

This is what i want to do:

I want to be able to create a Question and Answer Page like a Quiz.

I want something like this:
Question: What's my name?
Answer:______________

I don't want multiple choice but instead i want it to where the user has to write it.

Could you guys help?


I want them to be able to answer one at a time.
If they get it right they move on.
If they don't they stay there.
But then i also want them to not be able to do the quiz more than once
so if they complete it all they can't do it again.

mlseim
01-07-2010, 02:07 AM
Do they answer one question at a time, and get a correct or not,
or do they answer a whole page of them and then submit it?

It sort of makes a big difference.

litoburrito
01-07-2010, 02:12 AM
I want them to be able to answer one at a time.
If they get it right they move on.
If they don't they stay there.
But then i also want them to not be able to do the quiz more than once
so if they complete it all they can't do it again.

ole90
01-07-2010, 02:45 AM
You'd probably need a register each user who takes your Quiz, give them a username and password.

Then for each question, you'd want to either store them in a Database or text file and use PHP to grab the results. Use a html form to allow the user to submit their answer and check it against the right one, if correct, move them on, if not re-show the question.

mlseim
01-07-2010, 02:51 AM
You should use AJAX to put the PHP together with Javascripting so
the answers that are wrong, stay on the same page without refreshing.
A little bit more interactive and a cleaner user interface.

Best yet would be to use Flash with an XML file for the questions/answers.

There are so many different approaches to this one ... not sure what
direction you want to go, and how nice you want it to look and operate.
You can make it simple and clunky, or smooth and a good user experience.

I guess you decide how important the site will be, and the effort you want to use.

litoburrito
01-07-2010, 02:56 AM
I want to be able to customize it to look real nice but i guess i could just add an image behind it and stuff but right now i just want to be able to make it happen.

I have a vbulletin so that pretty much has the user info.

But i don't have flash or nothing like that. I'm pretty much gonna try and do it by using Notepad haha.

Could you tell me what i would possible need to learn and what i would have to use.

mlseim
01-07-2010, 03:17 AM
You don't really need MySQL for your questions/answers, make it simple by using a plain text file.

Make a file called "quiz.txt" that has a number|question|answer on each line separated by pipes |

1|What is the capital of Minnesota?|St. Paul
2|What is the capital of Wisconsin?|Madison

etc.

But you can see there's a problem already ...
What if I answer #1 like this: saint paul

How accurate do the answers need to be?
Do they have to be case-sensitive?
Might there be more than one correct answer?

You once again have a couple directions you could go ...

Add a bunch of alternate answers ...
1|What is the capital of Minnesota?|St. Paul|saint paul|sant paul|st. pal|saint pal|

Or, you could use a PHP command that "weighs" how close the answer is...
http://php.net/manual/en/function.similar-text.php

So, figure out what to do now ....

litoburrito
01-07-2010, 03:24 AM
Well i would want it to figure out the relative
like i think that php thing is right so if i used that it would make
st. acceptable for saint?
Their would be one right answer.
Now how would i connect that to show correctly?

mlseim
01-07-2010, 03:36 AM
Well, first make a text file with all of the questions ... like this:

1|What is the capital of Minnesota?|St. Paul
2|What is the capital of Wisconsin?|Madison

Then, attach that .txt file to a post so we can see it.

The first step is to display the questions and check the answer.

Once it all works, you'll be dealing with the "user login" issue,
but that is something for later on.

litoburrito
01-07-2010, 03:46 AM
Alrite so here is just some questions i thought up of right now haha

fail
01-07-2010, 03:52 AM
Maybe like this.....:




<form>
Whos the owner of Disneyland?
<input type="text" name="answer" />
</form>

<?php

$answer = $_POST['answer'];

if ($answer == 'Walt Disney') {

echo "That was correct!";
echo "<p><a href=\"question2.php\">Go to the next question...</a>"; // sorry, forgot the last ' " '

}
ELSE {

echo "wrong! Try again";

}

?>

litoburrito
01-07-2010, 03:59 AM
But how would i put my text file in there?
I would have to right all the questions in there?

Your code didn't work i got this


Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in /home/legsmith/public_html/burritos/hi.php on line 22

fail
01-07-2010, 04:22 AM
I forgot one ' " ' - see above where....

litoburrito
01-07-2010, 04:28 AM
That didn't work
but would that code do what i've been wanting?

I don't really want it to go to another page cuz i don't want to have all these pages.
Does it allow you to go back and redo the whole quiz?

tomws
01-07-2010, 04:30 AM
Maybe like this.....:


No, not like that at all. That's hard-coded - it doesn't take advantage of the text file.

litoburrito, stick with mlseim. He knows what he's doing. He'll probably be back and point you towards fopen (http://php.net/fopen) and related functions, or perhaps file_get_contents (http://php.net/file_get_contents). Do some reading on php file handling while you're waiting for him.

litoburrito
01-07-2010, 04:39 AM
php file handling?

fopen?
file get contents

haha so much to learn @_@

Thanks for telling me this

mlseim
01-07-2010, 04:53 AM
Give me a few minutes to get a basic script started ....

litoburrito
01-07-2010, 04:56 AM
alright
Thank You Thank You Thank You!

fail
01-07-2010, 04:59 AM
sure you can put it on one page with isset and function(). But that will requite a few hours php learning.

But for a ready to use code I suggest this link: http://www.getacoder.com

litoburrito
01-07-2010, 05:02 AM
i don't want to pay for anything. I have mlseim helping me.
Thanks for trying to help fail. I appreciate your effort.

mlseim
01-07-2010, 05:31 AM
Here is the basic flow ...
No answer checking yet, but just enough to get through the questions.

My test page is: http://www.catpin.com/quiz

Here is the code so far:
Name it "index.php" and copy and paste into the same directory as "quiz.txt" ...


<?php
session_start();

// This uses PHP SESSIONS, so it will remember what question they are on
// unless they close their browser ... in which case it will start over.
// This top part will verify members and not let them take it again (future addition).

// Find out if they are already taking the test and what question they are on.
if(isset($_SESSION['ques'])){
$q=$_SESSION['ques'];
}
else{
$_SESSION['ques'] = 1;
$q=1;
header ("location: index.php");
}

// see if the test is being reset.
if($_GET['reset']){
if(session_start()){
session_destroy();}
header ("location: index.php");
}

// put the whole test file into an array.
$lines=file("quiz.txt");

// Check to see if the test is finished.
if($lines[$q-1]){
// they are still taking the test, so do nothing.
}
else{
// they are done, so display something.
echo "You are finished taking the test.<br /><br />";
echo "<a href='index.php?reset=1'>Click Here to Reset</a>";
exit;
}

// Did they answer a question or are you showing them a question?
if($_POST['submit'] == "Submit Answer"){

// get the answer from the form.
$ans=$_POST['answer'];
echo "You answered: <b>$ans</b> <br /><br />";
$q=$q+1;
$_SESSION['ques'] = $q;

// this is where you would test the user's answer.

// for now, just display it and go on.
echo "<a href='index.php'>Next Question</a>";
}
else{

$parts=explode("|",$lines[$q-1]);

echo"
<form method='post'>
<b>Question $q</b><br />
$parts[1]<br />
Answer: <input type='text' name='answer' size='30'><br />
<input type='submit' name='submit' value='Submit Answer'>
</form>
";
}
?>



That's as much as I can do today .... see if you can expand on it.

EDIT:
On my webhost's server, the PHP SESSIONS expire real quick ... (I should change that).
If you start the test and leave for a while, it will expire in about 5 minutes?
Check your webhost's session timeout ... you might want to give the user more time to take the test.


EDIT AGAIN:
I'm now experimenting with a "correctness test" to see if I can come up with
an acceptable answer percent. It looks like any answer at 75% or above on
the "Levenshtein Percent Calculation" would be considered a correct answer.

It will be important to avoid questions that have numbers for answers ... as a
user would not know whether to use "1" or "one". Also, answers like "St." (street or saint?)
will be a problem as well.

Give me a new set of questions (quiz.txt) file ... let's see how it works out in percent.

Here is the test site again: http://www.catpin.com/quiz


.

litoburrito
01-07-2010, 06:44 PM
Alright, ill give you a new txt file later today. I uploaded the code to my website and it had some errors

it said that the
session_start();
&
header ("location: index.php");
were not needed or something like that so i just deleted it and it still worked alrite.
legouniverseforums.com/burritos/hi.php

i have no idea how to add the answers haha
i didn't even know that you could describe what you wanted it to do. os yeah im studing that code word for word and trying to figure out what everything means. haha

mlseim
01-07-2010, 06:55 PM
i didn't even know that you could describe what you wanted it to do.

If you're talking about the orange lines, those are comments inserted into the script.
They are only there for the programmer's sake ... they do nothing else.

The issue with session_start ... not sure what that is about.
Must be a thing with your webhost's server?

Here is the revised code with the code used to check the accuracy of answers.


<?php
session_start();


// This uses PHP SESSIONS, so it will remember what question they are on
// unless they close their browser ... in which case it will start over.
// This top part will verify members and not let them take it again (future addition).

// Find out if they are already taking the test and what question they are on.
if(isset($_SESSION['ques'])){
$q=$_SESSION['ques'];
}
else{
$_SESSION['ques'] = 1;
$q=1;
header ("location: index.php");
}

// see if the test is being reset.
if($_GET['reset']){
if(session_start()){
session_destroy();}
header ("location: index.php");
}

// put the whole test file into an array.
$lines=file("quiz.txt");

// grab the correct question from the array.
$parts=explode("|",$lines[$q-1]);

// Check to see if the test is finished.
if($lines[$q-1]){
// they are still taking the test, so do nothing.
}
else{
// they are done, so display something.
echo "You are finished taking the test.<br /><br />";
echo "<a href='index.php?reset=1'>Click Here to Reset</a>";
exit;
}

// Did they answer a question or are you showing them a question?
if($_POST['submit'] == "Submit Answer"){

// get the answer from the form.
$ans=$_POST['answer'];
echo "You answered: <b>$ans</b> <br />";
echo "Correct Answer: <b>$parts[2]</b> <br /><br />";

// make all characters lower-case.
$ans=strtolower($ans);
$parts[2]=strtolower($parts[2]);

// this is where you would test the user's answer.

// this first method uses "similar_text" .. not as accurate as Levenshtein.
$matched = similar_text($ans,$parts[2], &$p);
$p=intval($p);
echo "Correctness Match: <b>$matched</b> &nbsp;&nbsp; Percent: <b>$p%</b> <br />";

// calculate the distance between words using levenshtein method, output in percent.
// I'm thinking that anything above 75% would be considered correct?
$lev = similar($ans, $parts[2]);
$lev = intval($lev);
echo "Levenshtein Percent Calculation: <b>$lev%</b> <br /><br />";

$q=$q+1;
$_SESSION['ques'] = $q;

// for now, just display it and go on.
echo "<a href='index.php'>Next Question</a>";
}
else{

// give the user the question.
echo"
<form method='post'>
<b>Question $q</b><br />
$parts[1]<br />
Answer: <input type='text' name='answer' size='30'><br />
<input type='submit' name='submit' value='Submit Answer'>
</form>
";
}

// below is the function to get the Levenshtein Percent Calculation.
function similar($str1, $str2) {
$strlen1=strlen($str1);
$strlen2=strlen($str2);
$max=max($strlen1, $strlen2);

$splitSize=250;
if($max>$splitSize)
{
$lev=0;
for($cont=0;$cont<$max;$cont+=$splitSize)
{
if($strlen1<=$cont || $strlen2<=$cont)
{
$lev=$lev/($max/min($strlen1,$strlen2));
break;
}
$lev+=levenshtein(substr($str1,$cont,$splitSize), substr($str2,$cont,$splitSize));
}
}
else
$lev=levenshtein($str1, $str2);

$percentage= -100*$lev/$max+100;
// comment-out the next two lines ... seems to work better.
//if($percentage>75)//Adjust similar_text
//similar_text($str1,$str2,$percentage);

return $percentage;
}


?>

litoburrito
01-08-2010, 02:25 AM
ok Thanks i got it to see my answer and to analyze it
is there any way for that analyzing thing to not show?
and then how do i get it to show the next question?

mlseim
01-08-2010, 03:59 AM
Yes ...
I just was working on the basic "flow" of the script.

So many more decisions to make ...

Is it supposed to stop after each question and let them know
if it was right or wrong?

Is it supposed to tally the correct and incorrect answers?

Where is that tally kept? In a database?
Is it supposed to remember that tally?

Is it supposed to show them the correct answer?

And when you run your script the way it is now, doesn't it
take you to the next question? Does your script run the same
way mine does? http://www.catpin.com/quiz

Your script is about 5% done .... 95% more to write.
I won't be able to write the whole thing ... it would take me weeks to do.

litoburrito
01-08-2010, 04:35 AM
Is it supposed to stop after each question and let them know
if it was right or wrong? yes but if they got it wrong i would not want it to show the right answer

Is it supposed to tally the correct and incorrect answers? No because they will have to get them all right

Is it supposed to show them the correct answer? no

And when you run your script the way it is now, doesn't it
take you to the next question? Does your script run the same
way mine does? http://www.catpin.com/quiz
kinda of here look for yourself http://legouniverseforums.com/burritos/quiz/

Your script is about 5% done .... 95% more to write.
I won't be able to write the whole thing ... it would take me weeks to do.

Is there something that im looking for that could help me write it myself? What kind of stuff do i need?

mlseim
01-08-2010, 01:35 PM
No, your script is not working, and it has to do with the sessions problem
you had in POST #22. You need to fix that before anything else happens.

It works on my webhost's server, so something with your server is different.
You need to do some digging, and possibly search your webhost's help pages
to find out ...
what revision of PHP they are running?
how the PHP sessions are set-up ... do they have any coding examples?
what those errors mean that you got?

You can't go any further without fixing that problem.

fail
01-09-2010, 02:55 AM
It works on my webhost's server, so something with your server is different.

Hosts often run some virtual server software were it's not easy to update the core LAMP functions. That means, often you are stuck with outdated PHP and mySQL versions. They may run fine now, but I would be careful with any software updates.

A good way to find out is to create a page phpinfo.php and copy just this into it:



<?php

phpinfo();

?>


Here is a live example: http://example.com.customers.tigertech.net/phpinfo.php

litoburrito
01-10-2010, 08:46 AM
This is the error messages i get.
Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home/legsmith/public_html/burritos/quiz/index.php:4) in /home/legsmith/public_html/burritos/quiz/index.php on line 5

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/legsmith/public_html/burritos/quiz/index.php:4) in /home/legsmith/public_html/burritos/quiz/index.php on line 5

Warning: Cannot modify header information - headers already sent by (output started at /home/legsmith/public_html/burritos/quiz/index.php:4) in /home/legsmith/public_html/burritos/quiz/index.php on line 19

i don't know how to find the php revision but im guessing it's up to date.

EDIT: i did find this PHP version 5.2.12



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum