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