Page 1 of 1

A simple question

Posted: Wed Apr 02, 2008 1:22 pm
by DanielCh
~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.


Hi all ! I have a website for helping south american people to learn english for free. I've been trying to work out a simple script, it should just store about 20 words and their spanish translation. Eg:
red = rojo
green = verde
etc

Then, the visitor types their answer into a text box, and it goes to the next word, until the end when it tells them - you got 14 / 20 right etc

Well I have absolutely no skills.. so after a lot of asking, I finally found someone to make something for me:

Code: Select all

<?php
// Start by putting some variables that I'll edit.
$correctcount = 0; // The correct count.
$wrongcount = 0; // The correct count.
 
$string = 'What the user typed'; // what the user put.
 
$word[0] = 'red'; $answer[0] = 'rojo'; $returned_answer[0] = 'What they put'; // Words, with answers
$word[1] = 'green'; $answer[1] = 'verde'; $returned_answer[1] = 'What they put';
 
foreach($word as $key => $value){ // Go through questions and compare answers.
if($returned_answer[$key] == $answer[$key]){
echo "Correct! $value is the same as: $answer[$key]";
$correctcount = $correctcount + 1; // Add to correct counts.
} else {
echo "Wrong :( $value is not: $returned_answer[$key], it is $answer[$key]";
$wrongcount = $wrongcount + 1;// Add to wrong counts.
}
}
 
echo "You got $correctcount out of ".$correctcount+$wrongcount;  // Say the score.
 
?>
But now they have dissapeared and I have no idea what to do next. Obviously I understand this $word[1] = 'green'; part, I can just duplicate that to make more.. but apart from that, what do I actually do with this? I tried to just copy and paste it into my website code but nothing much happened. Imagine you are talking to an alien who has never seen PHP before, if anyone can help me, thankyou!!


~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.

Re: A simple question

Posted: Wed Apr 02, 2008 2:22 pm
by pickle
That script just tallies up the scores, so you'll have to build something that records what the user answers.

If you've never touched PHP before, this is probably a bit out of your skill set. Normally in cases like this we recommend users learn the basics of PHP & try to do it on their own. Today I'm feeling generous though. This script'll pretty much do what you want with the exception that it will display all the words at once, rather than one at a time.

Code: Select all

<?PHP
/* Make new words with their translation in this array */
$words = array('red'=>'roja',
           'green'=>'verde');
 
 
 
 
$correct_count = 0;
$total_count = count($words);
 
if(isset($_POST['submit']))
{
  foreach($words as $eng=>$esp)
  {
    if($_POST['answers'][$eng] == $esp)
      $correct_count++;
  }
 
  echo "You got $correct_count/$total_count correct";
}
 
 
echo "<form method = 'post' action = ''>";
foreach($words as $eng=>$esp)
{
  echo "$eng: <input type = 'text' name = 'answers[$eng]' value = '".$_POST['answers'][$eng]."'/><br />";
}
echo "<input type = 'submit' name = 'submit' value = 'Check' />";
 
?>
Put that in a file that ends with .php (such as test.php). Put your usual page stuff (<html><body>...etc) around what I've posted there & it'll work. It's not pretty by any means, but it works.

Re: A simple question

Posted: Wed Apr 02, 2008 9:43 pm
by DanielCh
Well Pickle I think its an understatement to say you are generous!
Thanks a lot, I think I can almost make it work now :D

Re: A simple question

Posted: Wed Apr 02, 2008 10:48 pm
by DanielCh
I have it working here! - http://www.comespanish.com/lala.php
:D
Just one thing which would be awesome.. how would i make a Clear all and a Display all button?
So when the user clicks it, all the answers come to the boxes, incase they don't know anything. Is easy to do?

By the way I understand what you mean about I should learn it for myself, but trust me I read a lot of stuff and it's just impossible.. you know how some peoples brains simply cannot mix with some types of skills, or how sometimes really intelligent people dont have feelings, or really feeling people dont have intelligence. Like that!

Thanks again

Re: A simple question

Posted: Thu Apr 03, 2008 9:47 am
by pickle
Replace the contents of the foreach loop with this - this will show the answers when asked.

Code: Select all

$value = (isset($_POST['show'])) ? $words[$eng] : $_POST['answers'][$eng];
echo "$eng: <input type = 'text' name = 'answers[$eng]' value = '$value'/><br />";
Add these to the end of the code to add a 'reset' and 'show' buttons.

Code: Select all

echo '<input type = "reset" name = "reset" value = "Clear form" />';
echo '<input type = "submit" name = "show" value = "Show answers" />';
 

Re: A simple question

Posted: Thu Apr 03, 2008 11:37 am
by DanielCh
That's so good.. look at this! http://www.comespanish.com/lala3.php

Okay, I only have 1 more question in my whole php life, I swear to god - with the "clear form" button, it clears the forms to how they are when the page was loaded. So after someone clicks to get all the answers, then they click 'clear form' to have another try at it, it doesn't clear because it goes back to how it was when it was loaded - with all the answers.

How can I make it clear the forms back to empty. even after the person has clicked on the answers button ?

!! Thanks so much

Re: A simple question

Posted: Thu Apr 03, 2008 11:46 am
by pickle
Put a simple text link that just points to the page. That will load up the page fresh & clean.

Re: A simple question

Posted: Thu Apr 03, 2008 11:50 am
by DanielCh
Oh yeah that's right, thanks
:)