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.
Results 1 to 7 of 7
  1. #1
    Senior Coder
    Join Date
    Apr 2010
    Posts
    1,447
    Thanks
    71
    Thanked 102 Times in 101 Posts

    Difference in value

    So I am stumped with this one. I've debugged and fixed all errors and part of it is working.

    PHP Code:
     function equip($id$link) {
          global 
    $link$char;
          
          
    $bquery mysqli_query($link"SELECT * FROM rod_blacksmith WHERE `id`='$id'");
          
    $brow mysqli_fetch_array($bquery);
          
          
    $myquery mysqli_query($link"SELECT * FROM rod_myequipment WHERE `id`='$id' AND `charname`='".$char['charname']."'");
          
    $myrow mysqli_fetch_array($myquery);
          
          
    $myquery2 mysqli_query($link"SELECT * FROM rod_myequipment WHERE equipped='1' AND `charname`='".$char['charname']."' AND `name`='".$myrow['name']."'");
          
    $myrow2 mysqli_fetch_array($myquery2);

          
          if (
    $myrow['power'] > $myrow2['power']) {
            
    $gainloss '<font color=green>Gained</font>';
            
    $value $myrow['power'] - $myrow2['power'];
          }
          elseif (
    $myrow['power'] < $myrow2['power']) {
            
    $gainloss '<font color=red>Loss</font>';
            
    $value $myrow2['power'] - $myrow['power'];
          }
          else {
            
    $gainloss '<font color=yellow>There Was No Change</font>';
            
    $value '0';
          }
          
           if (
    $brow['type'] == '1') {
               
    $type 'Weapon';
               
    $power 'Atk';
           }
           elseif (
    $brow['type'] == '2') {
             
    $type 'Armor';
             
    $power 'Def';
           }
           else {
             
    $type 'Shield';
             
    $power 'Def';
           }
           
           
          echo 
    "You have equipped the ".$brow['name']." and <b>$gainloss $value</b> $power";
          
     } 
    So I currently have the Test Sword equipped with 15 atk. When I reequip it, it says No change and is 0 atk, which is correct. However when I select Wooden Knife with 1 atk, it should say "Loss 14 atk" which is the difference. But it keeps saying "Gained 1 atk".

    $myrow is what I am trying to equip, $myrow2 is what I currently have equipped.

    Like I said all queries and all are running fine, it's just not matching up correctly. Thanks


    Difference in value-dbmatchup.jpg
    Last edited by myfayt; 07-01-2014 at 12:23 AM.
    Been a sign maker for 7 years. My business:
    American Made Signs

  • #2
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,041
    Thanks
    2
    Thanked 316 Times in 308 Posts
    Your code should be producing some php warnings and/or notices because the third query is not matching any rows and $myrow2 in an empty/false value when you are calling the code with and id of 1 (your Wooden Knife.)

    To see what your code is doing, and why it isn't working, you need to play computer and manually go through the code line by line in your mind's-eye and/or in your programming editor and figure out exactly what each statement is returning when the code is called with any specific input value(s.)

    For your Wooden Knife, the first query is returning the name and type where id = 1. The second query is returning the name (of the equipment) and power where id = 1 and matching the current character name. The third query is trying to find a row where equipped = 1, matching the current character name, and with a name (of the equipment) matching the name (of the equipment) from the second query. This won't ever be true.

    In case it hasn't already been stated in one of your threads, by having a poorly laid out database (there's no good reason to repeat information that's defined in one table in other tables, you should be referring to foreign information using it's id), code/tables that don't use meaningful names for the tables, columns, and php variables, things like using global to get values into functions, and absolutely no comments in the code, makes it harder for anyone looking at the code to figure out what it is doing. One thing I can guarantee about your code/database, if we cannot figure out what it is doing (and what it is supposed to be doing), what chance have you got at figuring out what it is doing or of writing code that does what you want?
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #3
    Senior Coder
    Join Date
    Apr 2010
    Posts
    1,447
    Thanks
    71
    Thanked 102 Times in 101 Posts
    I don't quite understand what you are saying about the queries. It doesn't match up.

    $brow doesn't matter as it's just to display the type and atk/def.

    $myrow checks the users equipment for a match on the equipment ID and it matches the users character name (CHARNAME). I can pull all records from the DB under those values.

    $myrow2 checks which piece is currently equipped. So EQUIPPED, CHARNAME and NAME (Equipment Name).

    It's pulling the correct values, as it says "You've equipped the Wooden Knife and Gained 1 Atk". or "You've equipped the Test Sword and There Was No Change 0 Atk". It is strange that I am not getting any kind of warnings/notices.

    It seems like you are saying that on the $myrow2 query that I can't call $myrow because it'd be null/empty? Would that be correct?
    Been a sign maker for 7 years. My business:
    American Made Signs

  • #4
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,041
    Thanks
    2
    Thanked 316 Times in 308 Posts
    $myrow2 checks which piece is currently equipped. So EQUIPPED, CHARNAME and NAME (Equipment Name).
    No, it's not. Because the Equipment Name you are using in the Where clause is the name you are retrieving from the second query. The name from the second query is the name for the NEW id, and so by definition, the name from the second query cannot match the currently equipped name when you are changing the id.

    If your goal for the third query is to get the currently equipped row for the current character, I suggest you sit down and devise and test a query that does exactly that.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #5
    Senior Coder
    Join Date
    Apr 2010
    Posts
    1,447
    Thanks
    71
    Thanked 102 Times in 101 Posts
    Ah so you are saying that for the equipped query I need to be using the id of $id instead?
    Been a sign maker for 7 years. My business:
    American Made Signs

  • #6
    Senior Coder
    Join Date
    Apr 2010
    Posts
    1,447
    Thanks
    71
    Thanked 102 Times in 101 Posts
    Really immature person giving me a bad reputation comment saying "U don't know your own code?". First you need to learn better English, second I am trying to get a second pair of eyes to help me figure out the proper structure to make it work.
    Been a sign maker for 7 years. My business:
    American Made Signs

  • #7
    Senior Coder
    Join Date
    Apr 2010
    Posts
    1,447
    Thanks
    71
    Thanked 102 Times in 101 Posts
    I fixed it, had to add TYPE to it.
    Been a sign maker for 7 years. My business:
    American Made Signs


  •  

    Posting Permissions

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