SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    .* draziW tnioPetiS *. bronze trophy
    Join Date
    Jun 2004
    Location
    "Then I figure the most good good guy will win."
    Posts
    1,666
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Image based on text file var: Check my algorithm?

    Hello,

    Before I get too deep in it, I would really appreciate some input on the below algorithm... Hopefully my comments are detailed enough for all to know what I want to do, but just in case, here is a summary:

    User is asked to input a # (donation amount) into input box.
    User will then submit the form.
    On submit, the above # adds itself to text-file...
    The page refreshes and, depending on # stored in text file, a specific image is displayed.

    This is for a donation form... for every 50$ accumulated, a new image is displayed on the page (image of puzzle-pieces coming together).

    PHP Code:
    <?php

    //first, user submits number through web-based form, which adds to number stored in text file.

    /*
     Form:
     Input text field
     Submit button
     Write to text file
     End form.
    */

    //On submit, open text file with a mode of "r+" for reading and writing:
    $fp fopen"./someTextFile.txt" "r+" );

    //If no file exists, or error, or can't open, output this:
    if(!$fp)
    {
        echo 
    "Couldn't open the data file. Try again later.";
        exit;
    }

    //read-in current # stored in above file, put in variable $numb:
    $numb file"./someTextFile.txt" );

    //Now add # from user input form, and $numb # from file:
    $numb $numb $user_input;

    //Now write $numb back to text file
    fwrite$fp$numb );

    //Close text file:
    fclose$fp );
    ?>


    Not sure of best way to do the above and make it jive with switching image (script below):


    <?php

    //On submit, the number submitted gets added to number stored in text file and page refreshes.

    //Now comes below script that determines what image to display, depending on number-total read from text file:

    //Step one:
    //Open text file, read in data/number, close text file
    $fp fopen"./someTextFile.txt""r" );

    //If no file exists, or error, or can't open, output this:
    if(!$fp)
    {
        echo 
    "Couldn't open the data file. Try again later.";
        exit;
    }

    //read-in current # stored in above file, put in variable $num:
    $num file"./someTextFile.txt" );

    //Close text file:
    fclose$fp );

    //Now, use if statement, or, preferably a switch statement to do comparisons, and then echo out appropriate image on page:

    if (($num >= 0)&&($num 50))
        
    //display "img_0-50.jpg";
    elseif (($num >= 50)&&($num 100))
        
    //display "img_50-100.jpg";
    elseif (($num >= 100)&&($num 150))
        
    //display "img_100-150.jpg";
    elseif (($num >= 150)&&($num 200))
        
    //display "img_150-200.jpg";
    ...
    ...
    ...
    elseif ((
    $num >= 950)&&($num 1000))
        
    //display "img_950-1000.jpg";

    /////////////////////////////////////////////////
    //Or, do the above if statement as a switch statement instead:
    //(can I use bitwise operators (&&) in the case conditional???)
    /////////////////////////////////////////////////

    switch($num) {
        case ((
    $num >= 0)&&($num 50)):
            
    //display "img_0-50.jpg";
            
    break;
        case ((
    $num >= 50)&&($num 100)):
            
    //display "img_50-100.jpg";
            
    break;
        case ((
    $num >= 100)&&($num 150)):
            
    //display "img_100-150.jpg";
            
    break;
        ...
        ...
        ...
        case ((
    $num >= 950)&&($num 1000)):
            
    //display "img_950-1000.jpg";
            
    break;
    }

    ?>
    I am a bit of a noob when it comes to PHP, so I would love any help you all can send my way... my main concern is security and keeping the code clean...

    Many thanks in advance!
    Cheers
    Micky

  2. #2
    SitePoint Guru
    Join Date
    Jun 2004
    Location
    Finland
    Posts
    703
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, there are a few problems there. Anyway, formatting is great and comments are excellent - maybe even too specific Now, let's see..

    First of all, you should know that file() reads the contents of a file into an array. Each line becomes a separate item. file_get_contents() reads the entire file into a string.

    I'd do the incrementing like this:
    PHP Code:
    $don_file "./someTextFile.txt";

    $previous_donations = (float) file_get_contents($don_file); // get the contents, make it a float.

    $user_donated = (float) $_POST["donation_amount"]; // or $_GET["donation_amount"], if you're using GET as the form method.

    if ($user_donated 0) die("I'm not giving you any money :("); // stop processing if the user donated a negative amount..

    if ($user_donated == 0) die("Oh, you donated nothing.. How nice."); // No need to continue processing if there's nothing to add.

    $file_handle fopen($don_file,"w"); // open for writing only, truncate the file.

    fwrite($file_handle,$previous_donations $user_donated); // write the total amount of donations..

    fclose($file_handle); // close the handle. 
    And for reading:
    PHP Code:
    $don_file "./someTextFile.txt";

    $donations = (float) file_get_contents($don_file);

    $donation_step 50;

    $step_images = array(
      
    "img_0-50.jpg",
      
    "img_50-100.jpg",
      
    "img_100-150.jpg",
      
    "img_150-200.jpg"); // The images. The reason why I'm putting them into an array is because it allows you to have images with different names. You may put "smilie.gif" there if you want. I didn't list all the images because I'm kinda lazy :)

    $current_step floor($donations $donation_step); // The current step :)

    $image_src $step_images[$current_step]; // we have the image! 
    Very simple. Hope you like it
    Last edited by Sorccu; Oct 23, 2004 at 06:53. Reason: Changed a comment

  3. #3
    SitePoint Enthusiast timtsf's Avatar
    Join Date
    Oct 2004
    Location
    england
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Good Job m8

  4. #4
    .* draziW tnioPetiS *. bronze trophy
    Join Date
    Jun 2004
    Location
    "Then I figure the most good good guy will win."
    Posts
    1,666
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    WOW! Amazing!

    Thanks so much for reply and guidance, I am learning so much from you code!

    file_get_contents(), exactly what I needed... I had a feeling that file() was not the (best) way to do it... Also, I would have never thought to convert my input to floats, but makes so much sense (Coming from a beginner-level C++ and Javascript bkgrnd)... and the second part using an array! I am blown away! Hehe, it is all sooooo clean and perfect, thank you so much... Like I said, I am learning a ton by looking over your code--thanks Sorccu! You rock!

    I love PHP, especially well written PHP... Thanks!

    Oh, and great job on the error checking!


Bookmarks

Posting Permissions

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