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 2 of 2
  1. #1
    New Coder
    Join Date
    Sep 2011
    Posts
    16
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Variable not updating always getting a NULL VALUE

    PHP Code:
    if(isset($_POST['add'])){
    foreach(
    $_SESSION as $ps => $value){
    $getcartname = (explode('_'$ps));
    if(
    $getcartname[0] == 'ps'){
    $getLine = (explode('_'$ps));
    $line $getLine[0];
    $getVal = (explode('_'$value));
    $arrayContent $getVal[0];
    $arrTotal += $arrayContent;
    }
    }
    if(
    $line == ''){
    $line 1;
    }
    else{
    $line++;
    }
    $_SESSION['ps_'.$line] = $num.'_'.$totalqtyt.'_'.$amount;
    tempList();

    The script is suppose to do...

    If add has been set, check the array with ps_ if $line is NULL meaning the array is empty, set the $line to 1 if not increment $line.

    My problem is it doesnt seem to increment line after the first 'clicking' of $_POST['add']; What seems to be the problem?
    Last edited by JustBob; 10-22-2011 at 04:03 PM.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    This can be simplified:
    PHP Code:
    $iTotal 0;
    $line 0;
    foreach (
    $_SESSION AS $ps => $value)
    {
        list(
    $type$line) = explode('_'$ps);
        if (
    strcascmp($type'ps') == 0)
        {
            list(
    $num$qty$amt) = explode('_'$value);
            
    $iTotal += $num;
        }
    }
    $line $line ? ++$line 1
    This of course does not accommodate for gaps. IMO this is also way too much work, sessions are serialized, and any type that can be serialized can be added to a session. This does include arrays and objects, so you can add as many dimensions as you want and can completely avoid the explode mess.


  •  

    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
    •