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
    New Coder
    Join Date
    May 2005
    Location
    Paraguay
    Posts
    36
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Modifying values in a multidimensional array

    Hi;

    I'm trying to modify some quantities in an array, but obviously I'm doing something wrong.

    The original array is as follows;

    +---------+---------------------+---------+---------+---------+-----------------
    | szruta | fechastk | codprod | cantini | cantfin | desprod
    |
    +---------+---------------------+---------+---------+---------+-----------------
    | 0301500 | 2005-09-13 00:00:00 | 0101 | 20 | 40 | LUCKY 20
    |
    | 0301500 | 2005-09-13 00:00:00 | 0102 | 15 | 15 | LUCKY 10
    |
    | 0301500 | 2005-09-13 00:00:00 | 0301 | 10 | 10 | CAPRA
    |
    +---------+---------------------+---------+---------+---------+-----------------

    This array is defined as session_var['stk_movil'].
    The following script adds or substract the 'cantfin' field according to the post_vars 'oper' and 'cantoper'.

    PHP Code:
    $codprod $HTTP_POST_VARS['codprod'];
    $cantoper $HTTP_POST_VARS['cantoper'];
    $oper $HTTP_POST_VARS['oper'];

    foreach (
    $HTTP_SESSION_VARS['stk_movil'] as $r){
            if (
    $r['codprod']==$codprod){
               
               if (
    $oper == 'RCR'
                 
    $r['cantfin']+=$cantoper;
               if (
    $oper == 'DES'
                
    $r['cantfin']-=$cantoper;
               if (
    $oper == 'CAR'
                echo 
    'CARGA';
     
            }

    But, when I take a look on the modified array, the 'cantfin' remains the same and nothing has changed. I tryied printing the results of the calculation and it was done perfect. The problem is that the array doesn't 'save' the result in the cantfin field.

  • #2
    Regular Coder devinemke's Avatar
    Join Date
    Dec 2004
    Location
    NYC
    Posts
    443
    Thanks
    0
    Thanked 12 Times in 11 Posts
    Quote Originally Posted by vsalinas
    But, when I take a look on the modified array, the 'cantfin' remains the same and nothing has changed. I tryied printing the results of the calculation and it was done perfect. The problem is that the array doesn't 'save' the result in the cantfin field.
    because you are never writing the new calculations back to the $HTTP_SESSION_VARS['stk_movil'] array.

    i would show you an example but i'm a bit confused about your array structure. what are the keys/values exactly. show the outputs from print_r().

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,978
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Unless your php version is < 4.1, I'd recommend upgrading to superglobals as well, instead of the depreciated $HTTP_*_VARS.
    Other than that, array $r is correctly created. Using a var_dump on your $r should create what you need it to, but you'll need to reconstruct your $_SESSION array. This assumes as well that sessions have been created, as your current script does not show this existance.

    I'm with Devinemke with your structure as well. I'd assume that your $_SESSION['stk_movil'] is like so (possibly more/less fields):
    Code:
    Array (
         [0] => Array (
              [szurta] => 0301500
              [fechastk] => 2005-09-13 00:00:00
              [codprod] => 0101
              [cantini] => 20
              [cantfin] => 40
              [desprod] => LUCKY 20
         )
         [1] => Array (
              [szurta] => 0301500
              [fechastk] => 2005-09-13 00:00:00
              [codprod] => 0102
              [cantini] => 15
              [cantfin] => 15
              [desprod] => LUCKY 10
         )
         [2] => Array (
              [szurta] => 0301500
              [fechastk] => 2005-09-13 00:00:00
              [codprod] => 0301
              [cantini] => 10
              [cantfin] => 10
              [desprod] => CAPRA
         )
    )
    If this is the structure you currently have as your $_SESSION, you shouldn't experience any problems (assuming that cantfin is (int) type, or at least a numerical string for comparison).
    If you want to update your $_SESSION, like so:
    PHP Code:
    foreach ($_SESSION['stk_movil'] as $r){ 
            if (
    $r['codprod']==$codprod){ 
                
               if (
    $oper == 'RCR')  
                 
    $r['cantfin']+=$cantoper
               if (
    $oper == 'DES')  
                
    $r['cantfin']-=$cantoper
               if (
    $oper == 'CAR')  
                echo 
    'CARGA'

            } 
         
    $_SESSION[] = $r;

    Untested, but I'm thinking that will work. Let us know!

  • #4
    New Coder
    Join Date
    May 2005
    Location
    Paraguay
    Posts
    36
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi, thanks both for you help, as I'm a begginer in php.
    The session_var 'stk_movil' has the following structure (taken from a print_r()):

    PHP Code:
    Array ( 
        [
    0] => Array ( 
            [
    0] => 0301500 
            
    [szruta] => 0301500 
            
    [1] => 2005-09-13 00:00:00 
            
    [fechastk] => 2005-09-13 00:00:00 
            
    [2] => 0101 
            
    [codprod] => 0101 
            
    [3] => 20 
            
    [cantini] => 20 
            
    [4] => 40 
            
    [cantfin] => 40 
            
    [5] => LUCKY 20  
            
    [desprod] => LUCKY 20 
        [
    1] => Array ( 
            [
    0] => 0301500 
            
    [szruta] => 0301500 
            
    [1] => 2005-09-13 00:00:00 
            
    [fechastk] => 2005-09-13 00:00:00 
            
    [2] => 0102 
            
    [codprod] => 0102 
            
    [3] => 15 
            
    [cantini] => 15 
            
    [4] => 15 
            
    [cantfin] => 15 
            
    [5] => LUCKY 10 
            
    [desprod] => LUCKY 10 
        [
    2] => Array ( 
            [
    0] => 0301500 
            
    [szruta] => 0301500 
            
    [1] => 2005-09-13 00:00:00 
            
    [fechastk] => 2005-09-13 00:00:00 
            
    [2] => 0301 
            
    [codprod] => 0301 
            
    [3] => 10 
            
    [cantini] => 10 
            
    [4] => 10 
            
    [cantfin] => 10 
            
    [5] => CAPRA
            
    [desprod] => CAPRA ) ) 
    After the suggestion of Fou-lu, the array only shows the last row of 'stk_movil':

    PHP Code:
    Array ( 
        [
    0] => 0301500 
        
    [szruta] => 0301500 
        
    [1] => 2005-09-13 00:00:00 
        
    [fechastk] => 2005-09-13 00:00:00 
        
    [2] => 0301 
        
    [codprod] => 0301 
        
    [3] => 10 
        
    [cantini] => 10 
        
    [4] => 10 
        
    [cantfin] => 10 
        
    [5] => CAPRA
        
    [desprod] => CAPRA 

  • #5
    Regular Coder devinemke's Avatar
    Join Date
    Dec 2004
    Location
    NYC
    Posts
    443
    Thanks
    0
    Thanked 12 Times in 11 Posts
    PHP Code:
    foreach ($_SESSION['stk_movil'] as $key => $value)
    {
        if (
    $value['codprod'] == $_POST['codprod'])
        {
            if (
    $_POST['oper'] == 'RCR') {$_SESSION['stk_movil'][$key]['cantfin'] += $_POST['cantoper'];}
            if (
    $_POST['oper'] == 'DES') {$_SESSION['stk_movil'][$key]['cantfin'] -= $_POST['cantoper'];}
            if (
    $_POST['oper'] == 'CAR') {echo 'CARGA';}
        }
    }

    print_r($_SESSION['stk_movil']); 

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,978
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Oh yeah... guess it would help if I actually dumped the variables back into the 'stk_movil' array instead of overwriting them, lol. My bad.

    Devinemke's will dump each back into the session superglobal for you.

    Now, you need to be using the $_SESSION superglobals for this. As I recall, if your using the old method of registering your session variables, you will need to manually alter your cookies for the changes to take an effect. Don't hold me to that, its been awhile since I used sessions let alone pre-superglobal sessions. Give Devinemke's code a shot, if you have trouble we'll need to see your session setting's methods.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #7
    New Coder
    Join Date
    May 2005
    Location
    Paraguay
    Posts
    36
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you both!!! everything work perfect now!!!
    Also thanks for the suggestion about superglobals, I'll take it into account...


  •  

    Posting Permissions

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