SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Addict TegSkywalker's Avatar
    Join Date
    Nov 2001
    Location
    USA
    Posts
    214
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Why will count() work on a $_REQUEST and not $_SESSION ?

    I have an array of numbers that when sent to a page via a $_REQUEST, it is a bunch of numbers separated by commas like 102, 123, 324 and so forth. If I do a count() function for the $_REQUEST, it will give me a # of the values but if I do count() for $_SESSION, it will give me a value of 1.

    Am I doing something wrong here or is it not possible? Probably an easy answer but its annoying.

  2. #2
    SitePoint Addict Mal Curtis's Avatar
    Join Date
    Jul 2009
    Location
    New Zealand
    Posts
    327
    Mentioned
    14 Post(s)
    Tagged
    0 Thread(s)
    Does a print_r() of $_SESSION definitely show more than 1 value stored?

  3. #3
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,269
    Mentioned
    196 Post(s)
    Tagged
    2 Thread(s)
    AFAIK, SESSION variable values are serialized. So if you give one a value of
    $_SESSION['whatever'] = array('1', '2', '3');
    the value becomes
    array([0]=>array([0]=>'1',[1]=>'2',[2]=>'3'))
    i.e. count() == 1

    As Mal Curtis suggested, using print_r (or var_dump) will show you exactly what you're dealing with.

  4. #4
    SitePoint Addict TegSkywalker's Avatar
    Join Date
    Nov 2001
    Location
    USA
    Posts
    214
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If i do the print_r() it will of course list out the numbers in my array which is nice since you said it converts it into a more readable format. Is there a way to have it convert from being serialized and immediately take that list of numbers, count them, and store in a $_SESSION value? The reason I ask is because I want it to be registered before I move to the next page in a 5 page signup form.

  5. #5
    SitePoint Addict Mal Curtis's Avatar
    Join Date
    Jul 2009
    Location
    New Zealand
    Posts
    327
    Mentioned
    14 Post(s)
    Tagged
    0 Thread(s)
    Can you post the code assigning the request value to the session?

  6. #6
    SitePoint Addict TegSkywalker's Avatar
    Join Date
    Nov 2001
    Location
    USA
    Posts
    214
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    On Page 1, I have a list of states and here is a sample of about 5:

    PHP Code:
    <label for="1"><input id="1" name="area[]" value="1" type="checkbox">Auburn</label><br>
    <
    label for="2"><input id="2" name="area[]" value="2" type="checkbox">Birmingham</label><br>
    <
    label for="3"><input id="3" name="area[]" value="3" type="checkbox">Dothan</label><br>
    <
    label for="4"><input id="4" name="area[]" value="4" type="checkbox">Florence-Muscle Shoals</label><br>
    <
    label for="5"><input id="5" name="area[]" value="5" type="checkbox">Gadsden-Anniston</label><br
    Then on Page 2 I have
    PHP Code:
    <?
        $area 
    $_REQUEST["area"];
        while (list (
    $key_check,$area_check) = @each ($area))
            {
                
    $areas_check .= $area_check.",";
            }
        
    $all_areas_check substr($areas_check0, -1);

    if((
    $_SESSION["area"] =="")) {
        
    $_SESSION["area"] = $all_areas_check;
    }

    $count count($_SESSION["area"]);

    ?>
    And then later on in the HTML/PHP I was testing to see if it counted the number of items in the newly created "area" session and of course got a value of 1. The $planamount and $region variables were static to begin with so no need to focus on that.

    PHP Code:
    <?
      
    if(($count 3)) {
          
    $regions round($count 9.95 $planamount2);
          
    $subtotal = ($regions 29.85);
      } elseif ((
    $count 3)) {
          
    $regions round($planamount2);
          
    $subtotal = ($regions);
      }
    ?>
    And when I simply couldn't that to work I just dropped a simple echo command that gave me a value of 1

    PHP Code:
    <p>Number of items in session: <?=$_SESSION["count"]?></p>

  7. #7
    SitePoint Addict Mal Curtis's Avatar
    Join Date
    Jul 2009
    Location
    New Zealand
    Posts
    327
    Mentioned
    14 Post(s)
    Tagged
    0 Thread(s)
    Cool.

    First off, you don't need to actually remove them from the array, why not just do this?

    PHP Code:
    $_SESSION['area'] = $_REQUEST['area'];

    $count count($_SESSION['area']); 
    That way you're actually storing the array in the session (you can do that!)

    Secondly, there's an easier way to do your comma list

    PHP Code:
    // array to string
    $all_area_check implode(',',$_REQUEST['area']);

    // string to array
    $areas explode(',',$all_area_check); 

  8. #8
    SitePoint Addict TegSkywalker's Avatar
    Join Date
    Nov 2001
    Location
    USA
    Posts
    214
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It actually worked pretty well! Thanks again! The most helpful was the implode command since it did about the same as what I had but more efficient.

  9. #9
    SitePoint Addict Mal Curtis's Avatar
    Join Date
    Jul 2009
    Location
    New Zealand
    Posts
    327
    Mentioned
    14 Post(s)
    Tagged
    0 Thread(s)
    No problem, glad I could help.


Bookmarks

Posting Permissions

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