Kryllster Posted September 13, 2009 Share Posted September 13, 2009 I have several scripts that run fighting, buying, and so forth that have a lot of repetitious code. they mainly are in this form case 'fightone': // start our session to check for cheating if(!isset($_SESSION['loot_chance_run'])) { // if $_SESSION['loot_chance_run] not set, proceed and run it $_SESSION['loot_chance_run'] = true; // set the session var now // initialize outcome of fight and loot chances $outcome = mt_rand(1,100); $level_advance = 1; $gold = mt_rand(1000,4000); $diamonds = mt_rand(3,; $rubies = mt_rand(3,7); // First the fight if($outcome < 70){ // Connect to database include('includes/db_config.php'); include('includes/advance_level.php'); // Update database $sql="UPDATE $tbl_name SET onhand = onhand + $gold, diamond = diamond + $diamonds, rubie = rubie + $rubies, advance = advance + $level_advance WHERE username='$username'"; mysql_query($sql) or die (mysql_error()."<p>$sql</p>"); // Redirect to Success Page header("Location:mainview.php?show=fight_success&gold=$gold&diamonds=$diamonds&rubies=$rubies"); } else{ // Redirect to Defeat Page header("Location:mainview.php?show=fight_failure"); } } // Check for cheater else { header("Location:mainview.php?show=warn_enemy"); exit(); } break; they are all inside a case switch which it all works I just can't help but wondering (since I'm trying to move towards OOP) of I were to have them in funtions and included in an over database config? here is another example slightly different. switch($buy){ case 'firethunder': $skillbonus = mt_rand(1,5); // db config include('includes/db_config.php'); // Select data from database $sql = "SELECT * FROM $tbl_name WHERE username='$username'"; $result = mysql_query($sql); // Put info into array Hopefully while($row = mysql_fetch_assoc($result)) { // player config include('includes/player_config.php'); // check for money if they dont have it let them know else go with transaction if($row['onhand'] < '350000'){ header("Location: mainview.php?show=warn_money"); exit(); } else{ // Update database $sql="UPDATE $tbl_name SET onhand = onhand - 350000, skill1_name = replace(skill1_name, '$skill1_name', 'Fire Thunder'), skill1_bonus = $skillbonus WHERE username='$username'"; mysql_query($sql) or die (mysql_error()."<p>$sql</p>"); header("Location: mainview.php?show=d2buy_skills"); exit(); } break; } I guess what I'm asking is it better to have several small files included in each other and kind of have like one db script to run them instead of repeating all the code that I have been just copying and pasteing? I'm not sure if I put it right or not just trying to learn. Thanks, Quote Link to comment https://forums.phpfreaks.com/topic/174059-would-this-help/ Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.