 |
Account Login
|
 |
 |
Latest Articles
|
 |
 |
IRC Channel
|
 |
 |
Associates
|
 |
 |
Associates
|
 |
|
 |
 |
|
 |
12-26-2007, 08:08 PM
|
#1 (permalink)
|
|
The Acquainted
Join Date: Oct 2007
Location: florida
Posts: 110
Thanks: 36
|
I present... my first original script
Hey all, this is my first ever 100% original script. No snippets copied from outside sources or nothin'. It's a pretty simple quizzer script that I've been messing with just to have a project to experiment with and whatnot. I'm sure it's pretty elementary to some of you. =P
I'm looking for any feedback/tips. All are appreciated!
Originally it was just the ifelse statements in the pquiz.php file, then I decided to put what I've read about OOP to test and advance the script a little bit more.
See it in action here: Quiz Link
And of course, the source...
index.php
HTML Code:
<html>
<head>
<title>PHP Quizzer</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<form method="post" action="pquiz.php">
<p>Same questions, same answers. I'll make a more challenging quiz later on. =)</p>
<br />
What is the best language? <input name="questionone" type="text" />
<br />
<br />
What is the best language? <input name="questiontwo" type="text" />
<br />
<br />
What is the best language? <input name="questionthree" type="text" />
<br />
<br />
What is the best language? <input name="questionfour" type="text" />
<br />
<br />
What is the best language? <input name="questionfive" type="text" />
<br />
<input type="submit" value="Send!" />
</form>
</body>
</html>
pquiz.php
PHP Code:
<?php
// Get me mah classes! include 'class.php';
// Call up the Class, give vars user input (answers), then run them through the appropriate functions of the class.
$zaQuizone = new zlQuiz(); $zaQuiztwo = new zlQuiz(); $zaQuizthree = new zlQuiz(); $zaQuizfour = new zlQuiz(); $zaQuizfive = new zlQuiz();
$zaQuizone->qone = $_POST['questionone']; $zaQuiztwo->qtwo = $_POST['questiontwo']; $zaQuizthree->qthree = $_POST['questionthree']; $zaQuizfour->qfour = $_POST['questionfour']; $zaQuizfive->qfive = $_POST['questionfive'];
$zaQuizone->questionone(); $zaQuiztwo->questiontwo(); $zaQuizthree->questionthree(); $zaQuizfour->questionfour(); $zaQuizfive->questionfive();
?>
class.php
PHP Code:
class zlQuiz {
// pub vars public $qone; public $qtwo; public $qthree; public $qfour; public $qfive;
// Question one function questionone() { echo "<b>Question #1:</b> What is the best language?<br /><br /><i>Your answer: ".$this->qone." <br /><br />";
if ($this->qone == 'PHP') { echo '<font color="#009900">CORRECT</font><br /><hr /><br />'; } else { echo '<font color="#CC0000">INCORRECT</font><br />Correct Answer: PHP<br /><hr /><br />'; }
}
// Question two function questiontwo() { echo "<b>Question #2:</b> What is the best language?<br /><br /><i>Your answer: ".$this->qtwo." <br /><br />";
if ($this->qtwo == 'PHP') { echo '<font color="#009900">CORRECT</font><br /><hr /><br />'; } else { echo '<font color="#CC0000">INCORRECT</font><br />Correct Answer: PHP<br /><hr /><br />'; }
}
// Question three function questionthree() { echo "<b>Question #3:</b> What is the best language?<br /><br /><i>Your answer: ".$this->qthree." <br /><br />";
if ($this->qthree == 'PHP') { echo '<font color="#009900">CORRECT</font><br /><hr /><br />'; } else { echo '<font color="#CC0000">INCORRECT</font><br />Correct Answer: PHP<br /><hr /><br />'; }
}
// Question four function questionfour() { echo "<b>Question #4:</b> What is the best language?<br /><br /><i>Your answer: ".$this->qfour." <br /><br />";
if ($this->qfour == 'PHP') { echo '<font color="#009900">CORRECT</font><br /><hr /><br />'; } else { echo '<font color="#CC0000">INCORRECT</font><br />Correct Answer: PHP<br /><hr /><br />'; } }
// Question five function questionfive() { echo "<b>Question #5:</b> What is the best language?<br /><br /><i>Your answer: ".$this->qfive." <br /><br />";
if ($this->qfive == 'PHP') { echo '<font color="#009900">CORRECT</font><br /><hr /><br />'; } else { echo '<font color="#CC0000">INCORRECT</font><br />Correct Answer: PHP<br /><hr /><br />'; }
}
} // End of class
?>
|
|
|
|
12-26-2007, 10:21 PM
|
#2 (permalink)
|
|
The Frequenter
Join Date: Dec 2007
Location: In my basement
Posts: 386
Thanks: 47
|
PHP Code:
include 'class.php';
would be a smart security feature.
Also, I would answer English to all of those questions.
Other than that nice job.
|
|
|
|
The Following User Says Thank You to Aaron For This Useful Post:
|
|
12-26-2007, 10:52 PM
|
#3 (permalink)
|
|
The Acquainted
Join Date: Oct 2007
Location: florida
Posts: 110
Thanks: 36
|
Quote:
Originally Posted by Aaron
PHP Code:
include 'class.php';
would be a smart security feature.
Also, I would answer English to all of those questions.
Other than that nice job.
|
lol, well, the quiz is just dummy content as this is a project for me work on to develop my php skills. Seriously though, the more I learn the more this project will evolve (ie adding members system, more content, etc etc).
thanks
|
|
|
|
12-26-2007, 11:01 PM
|
#4 (permalink)
|
|
The Frequenter
Join Date: Dec 2007
Location: In my basement
Posts: 386
Thanks: 47
|
When you get that members system working sent it to me? :3
|
|
|
12-26-2007, 11:15 PM
|
#5 (permalink)
|
|
The Acquainted
Join Date: Oct 2007
Location: florida
Posts: 110
Thanks: 36
|
=P
I'll prob post the next version of this script on here.
Also, I took your advice and changed the include line on pquiz.php to this...
PHP Code:
@(include("class.php")) OR die ("Error: Classes are not in session");
|
|
|
|
12-27-2007, 03:09 AM
|
#6 (permalink)
|
|
The Contributor
Join Date: Sep 2007
Posts: 31
Thanks: 0
|
@ suppresses errors so "Error: Classes are not in session" won't be shown if it fails.
|
|
|
|
|
The Following User Says Thank You to daz For This Useful Post:
|
|
12-27-2007, 06:59 AM
|
#7 (permalink)
|
|
The Acquainted
Join Date: Oct 2007
Location: florida
Posts: 110
Thanks: 36
|
Quote:
Originally Posted by daz
@ suppresses errors so "Error: Classes are not in session" won't be shown if it fails.
|
It showed the die error message when I checked it.. *shrug*
|
|
|
|
12-27-2007, 04:57 AM
|
#8 (permalink)
|
|
Wizard
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
|
There is really no reason to put it in a class, objects are for reusable data and that isn't reusable. Something like this would be using a class for what its meat for.
pquiz.php
PHP Code:
<?php //I will only do one question for time reaons
// Get me mah classes! include 'class.php';
// Call up the Class, give vars user input (answers), then run them through the appropriate functions of the class.
$zaQuizone = new zlQuiz(); $zaQuizone->input = $_POST['questionone']; $zaQuizone->question = "What is the best language?"; $zaQuizone->answer = 'php'; $zaQuizone->success = '<font color="#009900">CORRECT</font><br /><hr /><br />'; $zaQuizone->failure = '<font color="#CC0000">INCORRECT</font><br />'; $zaQuizone->num = 1; $zaQuizone->question();
?>
class.php
PHP Code:
<? class zlQuiz { public $question; public $answer; public $success; public $failure; public $input; public $num;
// Question one function question() { echo "<b>Question #$num:</b> $this->question?<br /><br /><i>Your answer: $this->input <br /><br />";
if ($this->input == $this->answer) { echo $success; } else { echo $failure . "Correct Answer: $this->answer<br /><hr /><br />"; } } } ?>
Now you can make any amount of questions with any content without changing the class file.
Last edited by Village Idiot : 12-27-2007 at 05:28 AM.
|
|
|
|
|
The Following User Says Thank You to Village Idiot For This Useful Post:
|
|
12-27-2007, 06:55 AM
|
#9 (permalink)
|
|
The Acquainted
Join Date: Oct 2007
Location: florida
Posts: 110
Thanks: 36
|
Cool stuff, VI. I obviously still need to get a better grasp of the OOP philosophy. I had to start somewhere though.
That definitely puts OOP into a much better perspective for a novice like myself to understand. Appreciate that!
|
|
|
|
12-27-2007, 11:50 AM
|
#10 (permalink)
|
|
The Addict
Join Date: Nov 2007
Location: UK
Posts: 319
Thanks: 18
|
I got an error when not actually typing anything in
PHP Code:
Parse error: syntax error, unexpected ';' in /home/.pupo/easy/ir.tribalstylemusic.net/images/pquiz.php on line 6

|
|
|
12-27-2007, 06:23 PM
|
#11 (permalink)
|
|
The Acquainted
Join Date: Oct 2007
Location: florida
Posts: 110
Thanks: 36
|
Quote:
Originally Posted by Rendair
I got an error when not actually typing anything in
PHP Code:
Parse error: syntax error, unexpected ';' in /home/.pupo/easy/ir.tribalstylemusic.net/images/pquiz.php on line 6

|
Yea, I was changing some stuff last night and didn't finish. Sorry about that!
|
|
|
|
12-27-2007, 06:28 PM
|
#12 (permalink)
|
|
The Frequenter
Join Date: Sep 2007
Posts: 360
Thanks: 24
|
Quote:
Originally Posted by obolus
Yea, I was changing some stuff last night and didn't finish. Sorry about that!
|
Use version control system. 
__________________
Necessity is the mother of invention.
My blog
|
|
|
|
12-27-2007, 01:39 PM
|
#13 (permalink)
|
|
Moderateur
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
|
The following is only my own opinion, so feel free to openly disagree! An essential starting point with OOP is starting to think about 'things' as discrete objects (which can interact with other objects, but that's not important here). The main point of this post is to 'correct' (in my eyes) your class naming. The object being created in your code is not a Quiz (class zlQuiz) but a single Question. The Question will have your properties of question, answer, etc.
Then, thinking logically, a Quiz class would contain a series of Questions within it. At the moment, every question is a Quiz and the quiz itself isn't structured and certainly isn't flexible -- an essential concept for reuse.
Finally, here's an idea. Sometimes more than one answer might want to be accepted, for example "PHP" might be the correct answer but someone might enter "php" which would be wrong according to your code. Someone could answer "C#.NET", "C#", "c sharp", "C# .net" all being correct (assuming the question has that answer!) but how would you allow all of those variants to be correct? That's just a point for obolus to think about.
It's great to see you sharing your thoughts, code, ideas and progression with us and I'm looking forward to see things moving along for you.
|
|
|
|
|
The Following User Says Thank You to Salathe For This Useful Post:
|
|
12-27-2007, 06:29 PM
|
#14 (permalink)
|
|
The Acquainted
Join Date: Oct 2007
Location: florida
Posts: 110
Thanks: 36
|
Quote:
Originally Posted by Salathe
The following is only my own opinion, so feel free to openly disagree! An essential starting point with OOP is starting to think about 'things' as discrete objects (which can interact with other objects, but that's not important here). The main point of this post is to 'correct' (in my eyes) your class naming. The object being created in your code is not a Quiz (class zlQuiz) but a single Question. The Question will have your properties of question, answer, etc.
Then, thinking logically, a Quiz class would contain a series of Questions within it. At the moment, every question is a Quiz and the quiz itself isn't structured and certainly isn't flexible -- an essential concept for reuse.
Finally, here's an idea. Sometimes more than one answer might want to be accepted, for example "PHP" might be the correct answer but someone might enter "php" which would be wrong according to your code. Someone could answer "C#.NET", "C#", "c sharp", "C# .net" all being correct (assuming the question has that answer!) but how would you allow all of those variants to be correct? That's just a point for obolus to think about.
It's great to see you sharing your thoughts, code, ideas and progression with us and I'm looking forward to see things moving along for you.
|
For sure, for sure. Like I said earlier, the idea/concept/philosophy/principles of OOP are new to me and this is a starting point for me. I value all the feedback I've gotten so far though.
The next version of this script will evolve into something much more in-depth, secure, and grasp the concept of OOP better as well. I enjoy it so far enough that I want to start working on it now! =P
|
|
|
|
12-29-2007, 04:05 AM
|
#15 (permalink)
|
|
The Wanderer
Join Date: Dec 2007
Location: 127.0.0.1
Posts: 19
Thanks: 7
|
Hello,
a quiz was also one of my first projects but your codes are much better than mine were: I didn't know anything about OO and all the code was just copy and pasted. :)
Regarding Village Idiot's proposal: I never let my classes output anything unless it's explicitly wished (e.g. in template engines). I think classes should be portable meaning you can use them in different types of applications (console, PHP-Gtk applications, websites, etc.) and output (SOAP, XML-RPC, HTML, etc.). The advantage is that we don't need to change the basic class, only the code for the generation of the output.
Back to topic: If you have many questions it might be better to store them in a database. Anyway, I came up with a PHP-only solution. It's just an idea for an implementation since I didn't have the time to write the code.
PHP Code:
<?php
//we create an instance of the class "Question" for every question $question0 = new Question(); $question0->setQuestion('What is the best language?'); //string $question $question0->addAnswer('English', true); //string $answer, boolean $correct $question0->addAnswer('French', true); $question0->addAnswer('Spanish', true); $question0->addAnswer('German', false);
$question1 = new Question(); $question1->setQuestion('What is the best scripting language?'); $question1->addAnswer('PHP', true); $question1->addAnswer('Ruby', false); $question1->addAnswer('Python', false);
//add more questions...
//"Questions" is a stack of questions which might be stored internally in an array $stack = new Questions();
//now we'll add all previously created questions to the 'stack' $stack->addQuestion($question0); //object $question $stack->addQuestion($question1);
if (isset($_POST['answers']) && is_array($_POST['answers']) && (count($_POST['answers'] > 0)) { //we need to set all client's answers //attention: you need to check if the keys really do exist otherwise you'll get some warnings by PHP unless you're not supressing them (e.g. by error_reporting(0)) $question0->setUserAnswer($_POST['answers'][0]); $question1->setUserAnswer($_POST['answers'][1]);
//even better would be to iterate over $_POST['answers'] because you don't have to set the answers manually //very important - the client could have modified the keys, so we reindex the array $_POST['answers'] = array_values($_POST['answers']); foreach ($_POST['answers'] as $num => $answer) { if (isset(${'question' . $num})) { //we check if that variable does really exist ${'question' . $num}->setUserAnswer($answer); } }
//now we'll check the input foreach ($stack->checkAnswers() as $question) { //$question is an instance of the class "Question" which we used above echo 'Question: ' . $question->getQuestion() . '<br />' . PHP_EOL;
foreach ($question->getAnswers() as $answer) { //$answer is an instance of the class "Answer" echo 'Answer: ' . $answer->getText() . ' (' . ($answer->isCorrect() ? 'correct' : 'wrong') . ')<br />' . PHP_EOL; echo 'Your input was ' . ($answer->isUserInputCorrect() ? 'correct' : 'wrong') . '<br />' . PHP_EOL; echo '<br />' . PHP_EOL;
//here we could do things like increasing a variable to get the number of corrected answered questions }
echo '<hr />'; } } else { //now we'll generate the form foreach ($stack->getQuestions() as $question) { //echo the input fields //... } }
So here's a short summary of all classes I used:
Answer
+ getText() : string
+ isCorrect() : string
+ isUserInputCorrect() : boolean
Question
+ setQuestion(string $question) : void
+ getQuestion() : string
+ getAnswers() : array
+ addAnswer(string $answer, boolean $correct) : void
+ setUserAnswer(string $answer) : void
Questions
+ addQuestion(Question $question) : void
+ checkAnswers() : array
+ getQuestions() : array
Last edited by deflated : 07-18-2010 at 01:51 PM.
|
|
|
|
LinkBacks (?)
LinkBack to this Thread: http://www.talkphp.com/absolute-beginners/1801-i-present-my-first-original-script.html
|
| Posted By |
For |
Type |
Date |
| TalkPHP - Powered by vBulletin |
This thread |
Refback |
12-26-2007 09:27 PM |
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Hybrid Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|