CASE loops not working for me.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Locked
User avatar
m0u53m4t
Forum Contributor
Posts: 101
Joined: Wed Apr 19, 2006 7:47 am
Location: Wales

CASE loops not working for me.

Post by m0u53m4t »

My code all works so far, but its suppost to make files with each of the case names, but doesn't, just 6 all called attack atm. Any ideas?

Code: Select all

<?php

$username = $_GET["username"];
$password = $_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 "$username's account has been created";
 fclose($handle);

$loop=1;

for ( $counter = 1; $counter <= 6; $counter ++ ) {
$loop == $loop+1;

switch($loop){

case 1:  $stat = "Attack";
case 2:  $stat = "Cooking";
case 3:  $stat = "Crafting";
case 4:  $stat = "Defence";
case 5:  $stat = "Firemaking";
case 6:  $stat = "Fishing";

}

  if (!$handle = fopen("$username$stat.php", 'a')) {
    die ("Error while connecting, please try again.");
  } else {
  if (fwrite($handle, '<title>1</title>') === FALSE){
    die("Error while writing.");
  }
}
echo "<br>$stat level set at 1.";
 fclose($handle);


}

?>
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

$loop == $loop+1;

your doing a comparison and not reassigning the value. fix that and you should be good.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

you also need to break out of your switch when you match a value.
User avatar
m0u53m4t
Forum Contributor
Posts: 101
Joined: Wed Apr 19, 2006 7:47 am
Location: Wales

Post by m0u53m4t »

Thanks guys! This is it fully corrected. Im sure I'll have some questions about this later though...

Code: Select all

<?php

$username = $_GET["username"];
$password = $_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 "$username's account has been created";
 fclose($handle);

$loop=0;

for ( $counter = 1; $counter <= 5; $counter ++ ) {
$loop = $loop+1;

switch($loop){

case 1: $stat = "Attack"; break; 
case 2:  $stat = "Crafting"; break; 
case 3:  $stat = "Defence"; break; 
case 4:  $stat = "Firemaking"; break; 
case 5:  $stat = "Fishing"; break; 

}

  if (!$handle = fopen("$username$stat.php", 'a')) {
    die ("Error while connecting, please try again.");
  } else {
  if (fwrite($handle, '<title>1</title>') === FALSE){
    die("Error while writing.");
  }
}
echo "<br>$stat level set at 1.";
 fclose($handle);


}

?>
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Was this the same problem as this one you posted about?
Locked