Page 1 of 1

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

Posted: Sat Oct 23, 2004 6:20 am
by mhulse
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).

Code: Select all

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

Posted: Wed Oct 27, 2004 10:46 am
by kettle_drum
Everything looks fine. There is however one way you can reduce your code. In both an if or switch statement the code is run from the start to end. So the first case is fine - checking that the number is between 0 and 49, this will run if the number is between 0 and 49. Then on the second case you check to see that betwen 50 and 99. On this case you can simply ask the question "is it less than 100" as if its below 50, then the first case has already checked that fact and run the code.