Array not making any sense

Discussion in 'PHP' started by Dirty-Rockstar, Oct 23, 2008.

  1. #1
    field type is decimal(65,3) Default 0.000



    
    $maxstat=max(array(
    $result[physical] => 'Physical',
    $result[mental] => 'Mental',
    $result[position] => 'Position',
    $result[timing] => 'Timing',
    $result[distance] => 'Distance',
    $result[momentum] => 'Momentum'));
    
    
    print"
    For reference, Your top trained stat is <b>$maxstat</b></i>";
    
    PHP:
    Ok if i edit the table and have Physical = 1.000 i get:
    "For reference, Your top trained stat is Physical"

    mental=1.000 (resetting all other back to 0.000)
    "For reference, Your top trained stat is Momentum"

    position=1.000 (resetting all other back to 0.000)
    "For reference, Your top trained stat is Position"

    timing=1.000 (resetting all other back to 0.000)
    "For reference, Your top trained stat is Timing"

    distance=1.000 (resetting all other back to 0.000)
    "For reference, Your top trained stat is Momentum"

    momentum=1.000 (resetting all other back to 0.000)
    "For reference, Your top trained stat is Momentum"

    With the highlighted areas, why is that happening? how can i fix it?

    edit: I tried changing the name of the field with issues. same result
     
    Dirty-Rockstar, Oct 23, 2008 IP
  2. ToddMicheau

    ToddMicheau Active Member

    Messages:
    183
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    58
    #2
    Erm, well, see what you have there is a string array. Setting the array slot to a string will of course return a string. . . Very confused at what your doing there, if you post more code then we might be able to help.

    I'm pretty sure that code snippet wouldn't even run, it would error out since the $result[physical] isn't an array slot. You can assign array values like:

    
    $result['physical'] = ' ';
    $result[$physical] = ' ';
    $result[0] = ' ';
    
    //But not 
    $result[physical] = ' ';
    
    PHP:
    physical without being a string or variable wouldn't agree with php. . .
     
    ToddMicheau, Oct 23, 2008 IP
    Dirty-Rockstar likes this.
  3. Dirty-Rockstar

    Dirty-Rockstar Guest

    Messages:
    252
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #3
    
    else{
    $sql="SELECT physical,mental,position,timing,distance,momentum  FROM users where id='$users[id]'";
    $query=mysql_query($sql) or die(mysql_error());
    $result=mysql_fetch_array($query);
    	
    
    
    $maxstat=max(array(
    $result['physical'] => 'Physical',
    $result['mental'] => 'Mental',
    $result['position'] => 'Position',
    $result['timing'] => 'Timing',
    $result['distance'] => 'Distance',
    $result['momentum'] => 'Momentum'));
    
    
    	
    print "<i>I am the trainer of $game. Please tell me how hard you want to train and the type of training you want. You have <b>$users[steps]</b> steps to use.
    <br /><br /><br />";
    
    
    	print"
    For reference, Your top trained stat is <b>$maxstat</b></i>";
    }
    
    PHP:
    i added the ' ' which i really should make a habit of doing and i get the same issue. here is the entire code that is applicable to the issue. the var $users is from another query in an included file. no issue there
     
    Dirty-Rockstar, Oct 23, 2008 IP
  4. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #4
    Ain't sure if this is it, but if you check the user notes for the max function at php.net, someone mentions that max operates off of the values. Looks to me like you are setting the key to the number. Can test it at the least by reversing the assignment lines. Could be wrong but think its worth checking. Like so if it isn't clear.

    $maxstat=max(array(
    'Physical' => $result['physical'],
    ...
     
    shallowink, Oct 23, 2008 IP
  5. Dirty-Rockstar

    Dirty-Rockstar Guest

    Messages:
    252
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #5


    "for reference, Your top trained stat is 1.000"

    I dont want the amount printed, I want the word printed. in this situation, 1.000 is in the mental field, all the rest to 0 but if i revert the array back to the way i had it i get

    "For reference, Your top trained stat is Momentum"

    so MAX is doing its thing, the problem is the array i think but i dont see how
     
    Dirty-Rockstar, Oct 23, 2008 IP
  6. ToddMicheau

    ToddMicheau Active Member

    Messages:
    183
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    58
    #6
    Oooooooh! You just wanted the string printed out of the highest stat! xD

    I was under the impression you were trying to give them their stats lol. Well that's easy, you just need to sort the results from the sql query, then echo the topmost keyname. Something like this:

    
    $sql="SELECT physical,mental,position,timing,distance,momentum  FROM users where id='$users[id]'";
    $query=mysql_query($sql) or die(mysql_error());
    $result=mysql_fetch_array($query);
    
    //Changes start here:
    
    arsort($result);
    reset($result);
    $maxstat = key($result);
    
    //Changes end here.
        
    print "<i>I am the trainer of $game. Please tell me how hard you want to train and the type of training you want. You have <b>$users[steps]</b> steps to use.
    <br /><br /><br />";
    
    
        print"
    For reference, Your top trained stat is <b>$maxstat</b></i>";
    }
    
    PHP:
    Let me know how that works out, it worked in all the tests I tried.
     
    ToddMicheau, Oct 23, 2008 IP
  7. Dirty-Rockstar

    Dirty-Rockstar Guest

    Messages:
    252
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #7

    it works but its printing the value of the field.

    that is if i block quote this

    
    /*$maxstat=max(array(
    $result['physical'] => "Physical",
    $result['mental'] => "Mental",
    $result['position'] => "Position",
    $result['timing'] => "Timing",
    $result['distance'] => "Distance",
    $result['momentum'] => "Momentum"));
    */
    
    PHP:
    which i am assuming you wanted me too. Printing the value i dont want, I want to print the name of the stat itself if thats possible. thanks for the new functions tho. is there anything else i can add to make this work>?
     
    Dirty-Rockstar, Oct 23, 2008 IP
  8. Pos1tron

    Pos1tron Peon

    Messages:
    95
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Simple code and yours didn't make much sense... so I started again (there's some notes in the code):
    <?php
    
    $statsInfo = array(
        /**
        *    Obviously, you'll want to change this to using something like
        *    mysql_fetch_assoc() instead of manually setting the values here.
        *    
        *    I'd do this if you posted more code showing how you get that info 
        *    here for usage.
        **/
        'Physical' => 5,
        'Mental' => 12,
        'Position' => 94,
        'Timing' => 154,
        'Distance' => 1,
        'Momentum' => 75
    );
    
    foreach($statsInfo as $statName => $statValue) {
        if(max($statsInfo) == $statValue) {
            $statsInfo['Highest'] = $statName;
        }
    }
    
    /**
    *    In the below, you close an <i> tag at the end, but don't open one... I'm guessing *    *    it's opened prior to this bit of code, but just pointing it out.
    **/
    echo 'For reference, Your top trained stat is <b>'.$statsInfo['Highest'].'</b></i>';
    
    ?>
    PHP:
     
    Pos1tron, Oct 23, 2008 IP
  9. Dirty-Rockstar

    Dirty-Rockstar Guest

    Messages:
    252
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Yeah, that fixed it. a few posts down i show more code where the data is a db query. And that <i> tag opens farther up. Shotty coding i know, but you have to start somewhere. with some tweaking your code fixed my issue totally.

    Thanks
     
    Dirty-Rockstar, Oct 23, 2008 IP
  10. ToddMicheau

    ToddMicheau Active Member

    Messages:
    183
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    58
    #10
    0.o Mine did print the field name, that's what the key() function does. It first resorts the returned sql array from highest to lowest, then updates the index. Then calls the key() function. This works perfectly on all the associative arrays I tried it on. It wasn't my code, but okay. . . (I was trying to avoid looping)
     
    ToddMicheau, Oct 23, 2008 IP