Author Topic: Switch Statement  (Read 860 times)

Offline AcidicOne

  • Level 16
  • *
  • Posts: 147
  • Reputation: +0/-0
    • View Profile
Switch Statement
« on: February 10, 2009, 01:52:02 AM »
So here is my dilemma the "Default" part of the switch is working perfectly, I Believe its a problem with either wrong deceleration of what "Race_id" value is, the insert part is working perfectly into db, and it for the moment regardless of race selection is running the "default" value for the stats.
So I am fairly certain the switch structure is correct, I just dont have the syntax correct for calling of the race id


Code: [Select]
<?php

require_once 'smarty.php';

if(
$_POST) {
$password $_POST['password'];
$confirm $_POST['confirm'];
if($password != $confirm) {
$error 'Passwords do not match!';
} else {
require_once 'config.server.php'; // our database settings
$conn mysql_connect($dbhost,$dbuser,$dbpass)
or die('Error connecting to mysql');
mysql_select_db($dbname);
$query sprintf("SELECT COUNT(id) FROM users WHERE UPPER(username) = UPPER('%s')",
mysql_real_escape_string($_POST['username']));
$result mysql_query($query);
list($count) = mysql_fetch_row($result);
if($count >= 1) { 
$error 'that username is taken.';
} else {
 $query sprintf("INSERT INTO users(username,race_id,password) VALUES ('%s','%s','%s');",
                
mysql_real_escape_string($_POST['username']),
                
mysql_real_escape_string($_POST['race_id']),
                
mysql_real_escape_string(md5($password)));
$result mysql_query($query);
$userID mysql_insert_id($conn);
              require_once 
'stats.php';
              
$race_id "%s";
            switch (
$race_id){
        case 
"1":
            
setStat('atk',$userID,'5');
            
setStat('def',$userID,'5');            
            
setStat('mag',$userID,'5');
            break;
        case 
"2":
            
setStat('atk',$userID,'7');
            
setStat('def',$userID,'7');            
            
setStat('mag',$userID,'7');
            break;
        default:
            
setStat('atk',$userID,'6');
            
setStat('def',$userID,'6');            
            
setStat('mag',$userID,'6');
            break;
            }
$message 'Congratulations, you registered successfully!';
}
}
}
$smarty->assign('error',$error);
$smarty->assign('message',$message);
$smarty->display('register.tpl');

?>

the template file just encase its needed

Code: [Select]
<form method='post' action='register.php'>
Username: <input type='text' name='username' id='username' value='{$smarty.post.username}' /><br />
Password: <input type='password' name='password' /><br />
Confirm Password: <input type='password' name='confirm' /><br />
        Race:<select name='race_id' size='1'>
<option value="1">race_1</option>
<option value="2">race_2</option>
<option value="3">race_3</option>
<option value="4">race_4</option>
<option value="5">race_3</option>
</select>
People Like You, Are the Reason People Like Me Need Medication

Offline Achanjiati

  • Level 2
  • *
  • Posts: 5
  • Reputation: +0/-0
    • View Profile
Re: Switch Statement
« Reply #1 on: February 10, 2009, 02:37:45 AM »
Code: [Select]
$race_id = "%s";
Just echo $race_id after that and you should see why the default is choosen.

Offline AcidicOne

  • Level 16
  • *
  • Posts: 147
  • Reputation: +0/-0
    • View Profile
Re: Switch Statement
« Reply #2 on: February 10, 2009, 04:56:47 AM »
Quote
%s Congratulations, you registered successfully!

Is what is returned, can I not use the race_id variable from the mysql post? or should i be calling it from the html somehow?
People Like You, Are the Reason People Like Me Need Medication

Offline lolninja

  • Level 19
  • *
  • Posts: 194
  • Reputation: +5/-0
  • BSc powered Programmer
    • View Profile
    • HTTPmmo
Re: Switch Statement
« Reply #3 on: February 10, 2009, 05:21:28 AM »
What you need to do is something like...

Code: [Select]
<?php
switch ($_POST['race_id']) {
case '1':
setStat('atk',$userID,'5');
setStat('def',$userID,'5');
setStat('mag',$userID,'5');
break;

case '2':
setStat('atk',$userID,'7');
setStat('def',$userID,'7');
setStat('mag',$userID,'7');
break;

default:
setStat('atk',$userID,'6');
setStat('def',$userID,'6');
setStat('mag',$userID,'6');
break;
}
?>


Though $_POST['race_id'] needs validating, to make sure that its allowed by a given user, and doesn't contain random stuff.

Offline JGadrow

  • Level 35
  • **
  • Posts: 1,133
  • Reputation: +23/-2
    • View Profile
Re: Switch Statement
« Reply #4 on: February 10, 2009, 07:20:54 AM »
On line 29, you set:

Code: [Select]
$race_id = "%s";
This is making it always equal to %s not the value received from the $_POST variable. Thus, it never matches any of your case statements and always reaches default.
Idiocy - Never underestimate the power of stupid people in large groups.


Offline AcidicOne

  • Level 16
  • *
  • Posts: 147
  • Reputation: +0/-0
    • View Profile
Re: Switch Statement
« Reply #5 on: February 10, 2009, 08:28:46 AM »
yeah i figured that was the problem, i think my problem with variables so far in php is that I am accustomed to VB where i can declare global variables.
People Like You, Are the Reason People Like Me Need Medication

Offline lolninja

  • Level 19
  • *
  • Posts: 194
  • Reputation: +5/-0
  • BSc powered Programmer
    • View Profile
    • HTTPmmo
Re: Switch Statement
« Reply #6 on: February 10, 2009, 08:46:13 AM »
Yeah going from VB to PHP maybe tricky, but its worth the hassle, it is possible to use global variables in PHP, but its really not a good idea to do, as it basically leads to some really bad habits, and horrifically complex code that is pretty much impossible to debug :S

Offline codestryke

  • Administrator
  • Level 33
  • *****
  • Posts: 589
  • Reputation: +22/-0
    • View Profile
    • eXtremeCast Games
Re: Switch Statement
« Reply #7 on: February 10, 2009, 12:16:14 PM »
Yeah going from VB to PHP maybe tricky, but its worth the hassle, it is possible to use global variables in PHP, but its really not a good idea to do, as it basically leads to some really bad habits, and horrifically complex code that is pretty much impossible to debug :S
Really?

Umm, unlike VB, PHP is actually cleaner to use global variables. In VB you can declare a global but then the name of the variable should be given some type of identifier because every function in every module or form has access to the global. Where as in PHP to use the global variable you must state so in the function like global $race_id;

This tells you immediatly that you are using a global where unlike VB you really don't have a clear indication if it's a global or not. Which why in VB they tell you that using globals is bad and creates hoffic complex code, this is not the case with PHP.

Like everything else with code if used properly globals can be very beneficial, used improperly it creates a mess to debug but that isn't the fault of any language, just lazy and careless programming.



 
Creating online addictions, one game at a time:

Offline lolninja

  • Level 19
  • *
  • Posts: 194
  • Reputation: +5/-0
  • BSc powered Programmer
    • View Profile
    • HTTPmmo
Re: Switch Statement
« Reply #8 on: February 10, 2009, 01:48:20 PM »
Ah must just be my experience of global variables and PHP then, personally I see very little need for them, as you can use have a singleton object which retains the same data, and allows you to monitor what's trying to access data during run time.

Sorry for going completely off topic :)

At some point it maybe worth looking into some design patterns, mainly the strategy pattern http://en.wikipedia.org/wiki/Strategy_pattern, this basically allows you to replace large unyielding switch statements with many tiny objects, the benefit of implementing something like this is that it allows you to completely customise the provided functionality.
For example say you wanted to add some dynamic race creation feature to your game, using a switch would require you to add a lot of business logic into the switch, the more business logic you add, the more which can go wrong, and possibly break your existing code base. Using a strategy pattern you could just add a new dynamic race object, and any errors in there would have little to no effect on your existing code.

 


SimplePortal 2.3.3 © 2008-2010, SimplePortal