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

    Cucumbers,4040
    Onions,3403
    Mushrooms,2323

    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:

    http://www.php.net/manual/en/function.file.php - File(filename) - To read the .txt file
    http://www.php.net/manual/en/function.rand.php - rand(min, max) - To grab one of the lines randomly
    http://www.php.net/manual/en/function.explode.php - 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:

    http://www.php.net/manual/en/reserve...ables.post.php - $_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

    cash.php
    PHP Code:
    <?php
    $file 
    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" />
    </form>
    file.txt
    Code:
    banana,1
    orange,2
    grape,3
    lemon,4

  5. #5
    Why does this not work?

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


  6. #6
    I admit... i was bored...

    http://pc.rvgate.nl/kirupa/cash.php
    PHP Code:
    <?php
        
    // 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...
        
    if(isset($_POST['submit'])){
            
    // 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']){
                echo 
    "Congratulations! You are correct!";
            } else {
                echo 
    "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'>";
            echo 
    "    <strong>".$question.":</strong>";
            echo 
    "    <input type='hidden' name='id' value='".$id."' />";
            echo 
    "    <input type='text' name='answer' />";
            echo 
    "    <input type='submit' name='submit' value='Am i correct?' />";
            echo 
    "</form>";
        }
        
        
    // add some fancy footer
        
    echo "<hr/><center><a href='http://www.kirupa.com/forum/showthread.php?t=331753'>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 kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012