Page 1 of 1

CASE loops not working for me.

Posted: Fri Nov 10, 2006 6:42 pm
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);


}

?>

Posted: Fri Nov 10, 2006 6:46 pm
by shiznatix
$loop == $loop+1;

your doing a comparison and not reassigning the value. fix that and you should be good.

Posted: Fri Nov 10, 2006 6:53 pm
by Burrito
you also need to break out of your switch when you match a value.

Posted: Sat Nov 11, 2006 5:53 am
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);


}

?>

Posted: Sat Nov 11, 2006 8:25 am
by RobertGonzalez
Was this the same problem as this one you posted about?