Page 1 of 1

FOR loops and CASES. Help with my file creator.

Posted: Fri Nov 10, 2006 11:10 am
by m0u53m4t
Im making a file, that will have a username and password sent to it and will make some files and stuff. This is what I have so far:

Code: Select all

$username = md5($_GET["username"]);
	$password = md5($_GET["password"]);
	$filename = "$username.php";
	
	if (!$handle = fopen($filename, 'a')) 
	{
		die ("Error while connecting, please try again.");
	} 
	else if (fwrite($handle, $password) === FALSE)
	{
		die("Error while writing.");
	}
	
	echo "Account Created to $filename";
	fclose($handle);
	
	for ($counter = 1; $counter <= 20; $counter++) 
	{
		switch($counter)
		{
			case 1:  $stat = "Attack";
			case 2:  $stat = "Cooking";
		}
	
		if (!$handle = fopen("$username$stat", 'a')) {
			die ("Error while connecting, please try again.");
		} 
		else if (fwrite($handle, 1) === FALSE)
		{
			die("Error while writing.");
		}
	}
	
	echo "\r\n $stat level set at 1.";
	fclose($handle);
I want to create a whole load of files, by having a for loop changing the variable m by 1 each time. This will then go through a case statement of 1 to 20, each with a different file name, then use my prewritten code to make each of those files contain the number one. How do I do the for loop? ATM, its just not changing, it repeats that many times, but the variable $counter isn't changing.

Jcart | I've edited your code for better indentation and whitespace, at least indent properly next time please

Posted: Fri Nov 10, 2006 12:02 pm
by John Cartwright
damn I accidentally changed my modifications to your code in your post.

Either way, try running the code now, as I changed

Code: Select all

for ($counter = 1; $counter <= 20; $counter++)
Changed += to increment (++)

Shot in the dark..

Posted: Fri Nov 10, 2006 12:32 pm
by RobertGonzalez
Your not breaking out of your switch cases. Try adding a break after each case. Not sure it'll work ,but it is worth a shot if nothing else is working.

Posted: Fri Nov 10, 2006 2:01 pm
by m0u53m4t
How would I break then?

Re: FOR loops and CASES. Help with my file creator.

Posted: Fri Nov 10, 2006 2:03 pm
by Luke

Code: Select all

switch($counter)
		{
			case 1:
                            $stat = "Attack";
                            break;
			case 2:
                            $stat = "Cooking";
                            break;
		}

Posted: Fri Nov 10, 2006 2:28 pm
by RobertGonzalez
Read up on the switch() control structure syntax, and the other controls that are available to you.