Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 2 of 2 FirstFirst 12
Results 16 to 26 of 26
  1. #16
    New Coder
    Join Date
    Jan 2018
    Posts
    29
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by deathshadow View Post

    Where are your users -- their names, their id's, their grand totals, their stats -- STORED?!?
    It's all in phpMyAdim. I use phpMyAdim because it allows me to write my code without paying for a domain name.
    Here's an example of how I retreive Player 1's Aim stats.

    $get_aim_ALPHA = "SELECT aim FROM duel WHERE id = $ALPHA_player";
    $results_aim_ALPHA = mysqli_query($dbcon, $get_aim_ALPHA) or die('error getting data');
    $aim_RAW_ALPHA = mysqli_fetch_array($results_aim_ALPHA, MYSQLI_ASSOC);

    All of my stats have an extention of _ALPHA or _BETA which I use to match players. I use RAW to indicate these stats are the origin for futher calculations ... therefore $aim_Raw_ALPHA is the number of skills that player1 has assigned to the combat skill of aim. Another example is a step forward, the value of $aim_True_ALPHA, this is player1's aim, after the subtraction of player2's (BETA) evade, so the expression of "True" (in $aim_True_ALPHA) is the final value which is used to calculate chance to hit. All of the 9 combat skill use this method of naming variables .. ie RAW, True with the extention of _ALPHA or _BETA.

    I have linked these files
    include('connect.php'); // connect to phpMyAdmin database
    include('duel_stats_values.php');
    include('get_from_database.php');

    I access the health stats of BETA (player2) using this below which is contained in get_from_database.php (as is the example of aim above)

    $get_health_BETA = "SELECT health FROM duel WHERE id = $BETA_player";
    $results_health_BETA = mysqli_query($dbcon, $get_health_BETA) or die('error getting data');
    $health_RAW_BETA = mysqli_fetch_array($results_health_BETA, MYSQLI_ASSOC);

    Examples
    $aim_RAW_ALPHA (used with duel_stats_values.php file which contains all the multiplication figures to adjust all combat skills for game balance)
    $evade_RAW_BETA (used with duel_stats_values.php file which contains all the multiplication figures to adjust all combat skills for game balance)
    $evade_True_BETA (this is the final evade value after subtracting effects of BETA's relavant skills)
    $aim_True_ALPHA (this is the final aim value after subtracting $evade_True_BETA the evade of player2)
    $ctoHit_ALPHA (chance to hit, this is the sum of many combat skills including aim. Used together with a generated Random number to determine whether a hit or miss)
    $health_True_ALPHA
    $damage_True_ALPHA

    I hope my explanation is clear and you will see that my use of ALPHA and BETA to represent the match of player1 and player2 and is used throughout all of my code.

    In the duel_stats_values.php is
    $ALPHA_player = 1; // 1 is the id number
    $BETA_player = 2; // 2 is the id number
    I alter those numbers to chose different players to duel, then refreash my localhost screen to activate the duel. My purpose for asking help with this loop was to be able to use the loop to change the id of both players and perform multiple duels, sending the results to phpMyAdmin database for a large table displaying names and Win = 1 point, Tie = 0.5 point.

    I hope this additional info will enable you to advise me how to alter the loop code to change the id of $ALPHA_player and $BETA_player, when the loop is wrapped around my dueling code. I have wrapped it and it still provides your "dummie game results" and it didn't crash.

    Thanks from JoeGr

  2. #17
    New Coder
    Join Date
    Jan 2018
    Posts
    29
    Thanks
    12
    Thanked 0 Times in 0 Posts
    error I keep getting booted out of the forum every time i click on send

  3. #18
    New Coder
    Join Date
    Jan 2018
    Posts
    29
    Thanks
    12
    Thanked 0 Times in 0 Posts
    error I keep getting booted out of the forum every time i click on send
    Last edited by JoeGr; Aug 15th, 2018 at 05:31 PM.

  4. #19
    Senior Coder deathshadow's Avatar
    Join Date
    Feb 2016
    Location
    Keene, NH
    Posts
    3,013
    Thanks
    3
    Thanked 430 Times in 419 Posts
    It's not "all in phpMyAdmin" -- you have it in mysql. The thing phpMyAdmin exists to debug. You should likely still be writing CODE to create your tables, domain or no. If phpMyAdmin can connect to it, so can your PHP so why the phpMyAdmin?

    In fact, your use of mysqli proves that... even if you've got insecure nonsense since you're slopping variables into your query strings like its' still 2004. It's called prepare/execute, use it. Though using the trash that is mysqli in the same outdated manner as mysql_ shouldn't be a surprise since you've learned the function based approach to using it. You may want to consider switching to PDO.

    You also seem to be using multiple queries to do the job of one -- one for health, one for name... why not just get that all at once in one pass? Get the user -- ALL the information about that user, then use it.

    ASSUMING you have a table of id's and you want to do all players in the table, the first thing you should do is just get all the players -- hence the structure I used for doing so. You swap out match() with whatever your duelling routine is passing the players pulled from the database.

    I think you've got a major logic disconnect in how to use databases and process queries. PARTICULARLY all these extra "variables for nothing" you seem to be creating when you could just store it all as a single results array.

    See that table I had of all the players? Build something like that off your database table and use that to store all your different values, instead of all those extra "variables for nothing". That way your "alpha" and "beta" (or as I'd call it player1 and player2) could be JUST the id from the table.

    So... if player 1 (alpha) has id 0 in your database:

    $aim_RAW_ALPHA becomes $players[0]['aim']
    $evade_RAW_BETA becomes $players[0]['evade']

    ... and so forth, that or make it so that:

    Code:
    $alpha = [
      'id' => 0,
      'name' => 'whatever',
      'aim' => 0,
      'evade' => 0
      // etc, etc, etc.
    ];

    That way you have $alpha['id'], $alpha['name'], and so forth easily pulled in one fell swoop and stored back into the database likewise, easily operated on with better clarity. Converting it to an object many result in a speedup depending on how often "inside your logic" the properties are used, at the cost of some 'startup' time.

    For example you'd just: (this is PDO)

    Code:
    $stmt = $db->prepare('SELECT * FROM users WHERE id = ?');
    $stmt->execute([ $ALPHA_player ]);
    if ($alpha = $stmt->fetch()) {
    In mysqli it's a bit uglier to the tune of five to six times the code since it's prepare/execute model inhales upon the proverbial equine of short stature; one of the many reasons NOT to use mysqli and why I cannot fathom how/why anyone would use it in the first place instead of PDO.

    Though the ridiculously absurd hoops some people will jump through to make mysqli do what PDO does out of the box is mind-numbing.
    “There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies and the other way is to make it so complicated that there are no obvious deficiencies.” – C.A.R. Hoare, The 1980 ACM Turing Award Lecture
    http://www.cutcodedown.com

  5. Users who have thanked deathshadow for this post:

    JoeGr (Aug 18th, 2018)

  6. #20
    New Coder
    Join Date
    Jan 2018
    Posts
    29
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by deathshadow View Post
    It's not "all in phpMyAdmin" -- you have it in mysql. The thing phpMyAdmin exists to debug. You should likely still be writing CODE to create your tables, domain or no. If phpMyAdmin can connect to it, so can your PHP so why the phpMyAdmin?

    In fact, your use of mysqli proves that... even if you've got insecure nonsense since you're slopping variables into your query strings like its' still 2004. It's called prepare/execute, use it. Though using the trash that is mysqli in the same outdated manner as mysql_ shouldn't be a surprise since you've learned the function based approach to using it. You may want to consider switching to PDO.

    You also seem to be using multiple queries to do the job of one -- one for health, one for name... why not just get that all at once in one pass? Get the user -- ALL the information about that user, then use it.

    ASSUMING you have a table of id's and you want to do all players in the table, the first thing you should do is just get all the players -- hence the structure I used for doing so. You swap out match() with whatever your duelling routine is passing the players pulled from the database.

    I think you've got a major logic disconnect in how to use databases and process queries. PARTICULARLY all these extra "variables for nothing" you seem to be creating when you could just store it all as a single results array.

    See that table I had of all the players? Build something like that off your database table and use that to store all your different values, instead of all those extra "variables for nothing". That way your "alpha" and "beta" (or as I'd call it player1 and player2) could be JUST the id from the table.

    So... if player 1 (alpha) has id 0 in your database:

    $aim_RAW_ALPHA becomes $players[0]['aim']
    $evade_RAW_BETA becomes $players[0]['evade']

    ... and so forth, that or make it so that:

    Code:
    $alpha = [
      'id' => 0,
      'name' => 'whatever',
      'aim' => 0,
      'evade' => 0
      // etc, etc, etc.
    ];

    That way you have $alpha['id'], $alpha['name'], and so forth easily pulled in one fell swoop and stored back into the database likewise, easily operated on with better clarity. Converting it to an object many result in a speedup depending on how often "inside your logic" the properties are used, at the cost of some 'startup' time.

    For example you'd just: (this is PDO)

    Code:
    $stmt = $db->prepare('SELECT * FROM users WHERE id = ?');
    $stmt->execute([ $ALPHA_player ]);
    if ($alpha = $stmt->fetch()) {
    In mysqli it's a bit uglier to the tune of five to six times the code since it's prepare/execute model inhales upon the proverbial equine of short stature; one of the many reasons NOT to use mysqli and why I cannot fathom how/why anyone would use it in the first place instead of PDO.

    Though the ridiculously absurd hoops some people will jump through to make mysqli do what PDO does out of the box is mind-numbing.
    Thank you for going to all this much trouble. I am Autistic and dislexic. I am very slowly learning all these new programming laugages. It took 2 months to find out how to retrieve data using mysql, which drove me nuts. It took a year to get phpMyAdmin to work. I can't even use this crazy forum without getting booted, fortunately when I log back on it usually actions what I did when it boots me out, otherwise I could not even communicate here. I have to copy paste everything I write otherwise I risk losing what takes me about an hour to write. Now you want me to abandon mysql to PDO, when I cannot face learning this because I am so stressed that I have made almost zero progress in two weeks working full time on my game. I honestly do not understand how to do this loop and it is sad that after you've shown me so much patience, I might have to leave the testing untill I launch the Beta version of the game on the net, using human feedback. It took me two hours to write my last responce to you. Apart from you explaining the things I have done wrong, you give me no direction to what I actually need to do. To me your saying, why does anyone ride a donkey (mysql) when camels(PDO) are so much easier and faster. Well I am a novice and at this point in my learning curve a donkey is all I can manage. So when I ask could you help me getting the loop to run in my code. I end up reading your responce 4 times because i don't understand, I get stressed feel as sick as a dog then go and do an hour hard work in my garden to unstress, then come back with a determination to make this work. I need things put more simply, not more complicated, this is because of my autism. I do not read between the lines I lack intuative understanding and rely on truth and logic to try to make sense of things. I am very determined and that's about all I have going for me. All I want to do is know how to run the loop.

    Thank you for all the help.

  7. #21
    Senior Coder deathshadow's Avatar
    Join Date
    Feb 2016
    Location
    Keene, NH
    Posts
    3,013
    Thanks
    3
    Thanked 430 Times in 419 Posts
    I know there's a lot to take in but let PDO intimidate you -- it's still PHP and mysql, it just has a cleaner simpler syntax and promotes better practices. Most of what you're doing is little to no effort to change.

    For example what you have as:

    Code:
    $get_aim_ALPHA = "SELECT aim FROM duel WHERE id = $ALPHA_player";
    $results_aim_ALPHA = mysqli_query($dbcon, $get_aim_ALPHA) or die('error getting data');
    $aim_RAW_ALPHA = mysqli_fetch_array($results_aim_ALPHA, MYSQLI_ASSOC);
    1) You don't need the 'variable for nothing'

    2) You don't put the variable into the query.

    As such, assuming $db is your connected PDO object the same way your $dbcon is a connected mysqli object

    Code:
    $stmt = $db->prepare('SELECT aim FROM duel WHERE id = ?');
    $stmt->execute([$ALPHA_player]);
    if ($aim_RAW_ALPHA = $stmt->fetch(PDO::FETCH_ASSOC)) {
    Not a massive jump in code difference, uses the same underlying mysql (or mariadb) tables as the mysqli you're already using.

    PDO is just a different wrapper for SQL functions. It has a more consistent implementation, prevents multiple queries per string closing a security hole, and has a far simpler syntax for prepare/execute.

    Prepare/execute being the model you SHOULD be using if you are going to plug variables into your query strings. Any tutorial our other source telling you to put a variable into your query string -- at least for anything other than a table name -- is outdated outmoded rubbish and should be ignored.

    Though that's a hangup for a lot of beginners and experts alike out there; there's so much "web rot". Massively out of date or just plain bad tutorials that nobody is taking down. It's frustrating for beginners, it's frustrating for those of us trying to help others, it's frustrating when people make up lame excuses to justify old techniques that shouldn't even be taught anymore -- or worse outright bad practices that just make everything harder to work with.

    Alright, let's say you have all your players in a table called "players". It has an unique key id field, a name field, a wins field, and a losses field. Let's assume you've connected to PDO in a fashion similar to your mysqli connection and it's called $db

    Code:
    // get ALL players from the database
    $stmt = $db->query('SELECT * FROM players');
    /*
    	normally I would NEVER dump all players into an array as this could take
    	a LOT of memory and time, but if you're only running a few dozen and
    	this is just for testing it's fine. Otherwise I would set a LIMIT on the
    	above query.
    */
    $players = $stmt->fetch(PDO::FETCH_ASSOC);
    $playersCount = count($players);
    $playersStop = $playersCount - 1;
    
    for ($i = 0; $i < $playersStop; $i++) {
    	for ($j = $i + 1; $j < $playersCount; $j++) {
    		for ($k = 0; $k < MATCHES_PER_LOOP; $k++) {
    			match($players[$i], $players[$j]);
    		}
    	}
    }
    Using as the match function whatever it is your game uses to run a duel. That would loop through all players in your database to make sure they face each-other for at least one set of rounds.

    What you're thinking of as "alpha" and "beta" being what you'd have MATCH side:

    Code:
    function match($alpha, $beta) {
    Where $alpha would contain all that information I outlined as a structure above.

    ... and seriously don't let this get you down. You've dived into the deep end of the programming pool here in an area with a particularly nasty undertow, so far you're at least treading water which is more than many people accomplish.
    “There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies and the other way is to make it so complicated that there are no obvious deficiencies.” – C.A.R. Hoare, The 1980 ACM Turing Award Lecture
    http://www.cutcodedown.com

  8. Users who have thanked deathshadow for this post:

    JoeGr (Aug 18th, 2018)

  9. #22
    New Coder
    Join Date
    Jan 2018
    Posts
    29
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Thank you once again deathshadow. I now realise I have omitted to say I am using MAMP and this has phpMyAdmin installed. I don't know if PDO can be used in MAMP, I did a search and the answer was complicated and I didn't understand it. This was at the root of why I couldn't use phpMyAdmin for a year, I had installed phpMyAdmin manually not realizing it was already installed by MAMP and it messsed up the settings. So I am back to square one again, sorry. I have no clue how to continue.

  10. #23
    Senior Coder djm0219's Avatar
    Join Date
    Aug 2003
    Location
    North Carolina
    Posts
    1,527
    Thanks
    4
    Thanked 243 Times in 240 Posts
    The bundle, MAMP, that you are using has no affect on whether or not you may use PDO. Any PHP version at or above 7 has it. Hopefully the bundles have it enabled in PHP by default (if not, shame on them).
    Dave .... HostMonster for all of your hosting needs

  11. Users who have thanked djm0219 for this post:

    JoeGr (Aug 18th, 2018)

  12. #24
    Senior Coder deathshadow's Avatar
    Join Date
    Feb 2016
    Location
    Keene, NH
    Posts
    3,013
    Thanks
    3
    Thanked 430 Times in 419 Posts
    Quote Originally Posted by djm0219 View Post
    Any PHP version at or above 7 has it.
    At or above 5.1 circa 2005. PDO is now a teenager.

    Part of why people just now getting around to switching away from the old mysql_ functions is 100% grade A "derp".

    But yeah, basically if you can connect using mysqli, you can connect using PDO. The ONLY thing that might be messed up is if MAMP (which I'm utterly unfamiliar with) wants you to use sockets instead of ports for access... in which case your DSN would look more like this:

    mysql:dbname=test;unix_socket=/opt/lampp/var/mysql/mysql.sock

    Whilst a normal port-based DSN goes like this:

    mysql:dbname=test;host=localhost

    Not sure if MAMP has that problem, I know XAMPP on Windows does... won't make a proper port based connection so you have to go to the socket method.

    Really though connecting isn't THAT different from mysqli. Where in mysqli you have:

    Code:
    $mysqli = new mysqli(
      'localhost',
      'my_user',
      'my_password',
      'my_db'
    );
    in PDO you have:

    Code:
    $db = new PDO(
      'mysql:dbname=my_db;host=localhost',
      'my_user',
      'my_password'
    );
    Pretty smooth sailing from there... biggest different would probably be using try/catch for PDO instead of $mysqli->connect_error
    Last edited by deathshadow; Aug 17th, 2018 at 01:24 PM.
    “There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies and the other way is to make it so complicated that there are no obvious deficiencies.” – C.A.R. Hoare, The 1980 ACM Turing Award Lecture
    http://www.cutcodedown.com

  13. Users who have thanked deathshadow for this post:

    JoeGr (Aug 18th, 2018)

  14. #25
    Senior Coder benanamen's Avatar
    Join Date
    Oct 2015
    Posts
    1,479
    Thanks
    4
    Thanked 151 Times in 146 Posts
    OP, Take a look at this tutorial to get you going with PDO https://phpdelusions.net/pdo

    Download my PDO Bumpstart Database and study the code. I wrote it for beginners just like you to get going quickly with basic functionality with PDO.
    https://github.com/benanamen/pdo_bumpstart_ver1.6
    To save time, lets just assume I am almost never wrong.

    The XY Problem
    The XY problem is asking about your attempted solution (X) rather than your actual problem (Y). This leads to enormous amounts of wasted time and energy, both on the part of people asking for help, and on the part of those providing help.

    "This text has been encoded with ROT26. If you can read this you must have found a backdoor. Congratulations!"

  15. Users who have thanked benanamen for this post:

    JoeGr (Aug 18th, 2018)

  16. #26
    New Coder
    Join Date
    Jan 2018
    Posts
    29
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Many thanks to each of you djm0219, deathshadow and benanamen. Although I was excited about moving on to other parts of my game as I thought my duelling was almost complete. I see I must follow your helpful advise and move over to PDO. The kindness and sense of comunity here is far behond what I experted, thank you. I might need a little time to get my head round all of this as I don't find changing course fits my nature well, the timming of this curve ball is not good for my head, but I can see it will be massively benefitial for my game and it's future players.


 
Page 2 of 2 FirstFirst 12

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •