switch?

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

Post Reply
hypernol
Forum Newbie
Posts: 1
Joined: Tue Jan 13, 2009 5:02 pm

switch?

Post by hypernol »

Ok, I have this code. It is meant to run as a cronjob. It is supposed to run, check each players rank, then add turns to each account dependent upon which rank the have.

The script work to an extent. However, it will only execute the case "Member". I originally had the member case set and a default instead of a case without the member clasification. It would only execute the default on all members meaning everyone recived only 15 turns.

I then changed the default into case "member" leaving it where it was (at the bottom). Tested it, and again it was only adding 15 turns to all players.

I then moved the case "member" to the start of the script thinking that maybe it was executing the final case every time. I was wrong, even when moving the case to the top of the script it was still the only one to be executed.

Code: Select all

 
<?php
$config_server = "*******"; //Database host
$config_database = "*******"; //Database name
$config_username = "********"; //Database username
$config_password = "********"; //Database password
$secret_key = "********"; //Secret key, make it a random word/sentence/whatever
 
 
include('../adodb/adodb.inc.php'); //Include adodb files
$db = &ADONewConnection('mysql'); //Connect to database
$db->Connect($config_server, $config_username, $config_password, $config_database); //Select table
 
$db->SetFetchMode(ADODB_FETCH_ASSOC); //Fetch associative arrays
$ADODB_FETCH_MODE = ADODB_FETCH_ASSOC; //Fetch associative arrays
//$db->debug = true; //Debug
 
$query = $db->execute("select `rank` from `players` where `id`");
 
$player1 = $query->fetchrow();
foreach($player1 as $key=>$value)
{
    $player->$key = $value;
}
 
switch ($player->rank) 
 
{ 
case "Ghetto": 
 
$query = $db->execute("update `players` set `turns`=turns + 30"); 
break; 
 
case "Player": 
 
$query = $db->execute("update `players` set `turns`=turns + 45"); 
break; 
 
case "Pimp": 
 
$query = $db->execute("update `players` set `turns`=turns + 60"); 
break; 
 
case "OG": 
 
$query = $db->execute("update `players` set `turns`=turns + 100"); 
break; 
 
case "Member";
 
$query = $db->execute("update `players` set `turns`=turns + 15"); 
break; 
} 
?>
 
 
If anyone might be able to tell me what I am doing wrong, please let me know.
Thank you in advance.
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: switch?

Post by Mark Baker »

Do you think that perhaps the switch statement should be inside your foreach($player1 as $key=>$value) loop.
I'm not sure about how it's accessing $player either, I assume you're trying to access each individual $player in the loop; but it looks like you're only accessing the last one retrieved by your query, and then updating the records of all players based on that one

and what is the database query select `rank` from `players` where `id` meant to retrieve?
Post Reply