The forums have permanently moved to This forum will be kept around in read-only mode for archival purposes. To learn how to continue using your existing account on the new forums, check out this thread.

Results 1 to 6 of 6

Thread: Code Tester

  1. #1

    Code Tester

    So basically I want to write a program that will help cashiers memorize codes. Take the numbers from a TXT file likes such


    Choose a random line, and display a form like this

    Cucumber [text box] [submit button]

    The person enters the number (4040) into the text box and presses submit if they get it right it reloads with another random line, if they get it wrong they have to do it again.

    Can anyone help me out with this?

  2. #2
    You will need: - File(filename) - To read the .txt file - rand(min, max) - To grab one of the lines randomly - Explode(delimiter, string) - To seperate the question from the answer from the line you retrieved

    Now you have all the information you need, and you can display the form... then whenever you submit it: - $_POST - to retrieve the users answer and question

    Compare that to the file again... and display if he's correct...

    refresh the page.. and done!

    (im pretty sure you can understand that we are not here to create your code, give it a try... and post whenever you get stuck)

    You can never underestimate the stupidity of the general public.

  3. #3
    Thank you, I was completely lost, I haven't coded in PHP for a while

  4. #4
    Okay I can't get the post to work

    PHP Code:
    file('./file.txt'); //Choose File
    $line $file[array_rand ($file)]; //Random Line From File
    $split explode(","$line); //Split random line with ,
    $name $split[0]; //split[0] is the name of the product
    $number $split[1]; //split[1] is the product number
    <form action="cash.php" method="post">
    <?php echo "$name: "?><input name="numbers" type="text" /><input type="submit" />

  5. #5
    Why does this not work?

    PHP Code:
    if($_POST['numbers'] == $number){
    } else {

  6. #6
    I admit... i was bored...
    PHP Code:
    // read file
    $file file("cashfile.txt");
    // get questions and answers from file
    foreach($file as $key => $line){
    // seperate question and answer
    $exploded explode("," ,$line);
    // save question and answers for later use
    $questions[$key]['question'] = trim($exploded[0]);
    $questions[$key]['answer'] = trim($exploded[1]);
    // add some fancy title to the page
    echo "<h1>Quess the code!</h1><hr/>";
    // to ask or not to ask...
    // Lets see the user was correct
            // ----------------------------------------        
            // get the question id
    $id $_POST['id'];
    // compare the user answer with the real answer
    if($questions[$id]['answer'] == $_POST['answer']){
    "Congratulations! You are correct!";
            } else {
    "Poor kid... You are wrong this time... ";
    // option to try again...
    echo "<br/><br/><a href='cash.php'>Try again</a>";
        } else {
    // Generate the form with a random question
            // ----------------------------------------
            // get a random question
    $id array_rand($questions);
    $question ucfirst($questions[$id]['question']);
    // show the form, and add the question id so that we can check it later
    echo "<form method='post'>";
    "    <strong>".$question.":</strong>";
    "    <input type='hidden' name='id' value='".$id."' />";
    "    <input type='text' name='answer' />";
    "    <input type='submit' name='submit' value='Am i correct?' />";
    // add some fancy footer
    echo "<hr/><center><a href=''>Code tester by rvgate</a>";
    Last edited by RvGaTe; July 25th, 2009 at 02:20 PM. Reason: added trim() on line 11 and 12

    You can never underestimate the stupidity of the general public.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Home About Meet the Moderators Advertise

 Link to Us


Copyright 1999 - 2012