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

//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

    well first off your code isn't an algorithm, your more or less using comparison operators. a algorithm would be some sort of advanced conditional math.

    your code looks fine to me, it looks as if you may have programmed in something like asp or visual basic since your using that style syntax layout.

    you shouldn't have any problems with either script as long as you check the data passed to them.

    for example

    if (is_numeric($My_Donation)) {
    //execute script
    } else {
    //don't execute script
    }

    if you are having a certain problem post what line and what problem and if i'm still on i'll look into it for yah.

      The theory behind your code appears to be fine. You are misapplying some functions though.. for example:

      $numb = file( "./someTextFile.txt" );  //syntax error..  If syntax is corrected, it will return an array of lines of text.

      What you probably need is:

      $numb = fscanf($fp, "%f"); //gets a floating point value from a text file

      That's the only glaring error I see in what you already have coded.

      There is something important to remember when you are working with text files from a script: Web servers are multitasking machines. There is always the potential for two users to access your page at the same or very nearly the same time. If this happens and you don't take precautions, you will end up with what is called a race condition. Both instances of the script will open the file, read the same number, add their own number, and then write the result. You will end up only having the result of the instance that finished last.

      In order to get around this, you need to create a lock file. Basically it's an empty file that your script checks for before it attempts to open the data file.

      If the lock file does not exist, the script can assume it's safe. The script creates a lock file, opens the data file, does its thing, closes the data file, and removes the lock file.

      If the lock file does exist, pause for a half of a second and check it again. Do this up to a limit of times (say 25) and if after the limit of tries it still fails, give the user a polite "busy" message.

      If you're expecting a low traffic site, it is extremely unlikely that you will need this feature, but be aware that the potential exists. Most sites get around this problem by using a database system of some sort that has these locking features built in.

        Thanks for the quick responses/input, I really appreciate all of your help. 🙂

        Majik: Thanks for the tip on the lock file, very interesting... the site will not be busy, but I am interested to learn more--looking into that as I type this.

        Mw: Sorry if I called my code an algorithm, hehe, that is what the profs had us call our pre-code drafts in C++ and Javascript classes... Sorry if I confused anyone.

        Again, thanks for all of your input (Majik/Mw), very helpful... I will post my updated code as soon as I get something good working...

        😃

        Cheers!
        Micky

          Write a Reply...