We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 25551 ☆ A M B ☆
    • 1,231 Posts
    I need some help removing duplicate variables in a SESSION array. I want to show all the web groups a user is a member of in a session array and not have it loop through and show the same values over and over.

    My code for gathering the web groups and adding them to the session.

    $groups = $modx->db->select("*", "modx_web_groups", "webuser = 10 ") or die ("table not found");
    if  ( mysql_num_rows($groups) )  {
    
    	while( $row = mysql_fetch_array($groups ) ) {
    
    		extract($row);
    		$row['webgroup'];
    		$row['webuser'];
    		$mywebgroup  = $row['webgroup'];
    		$mywebuser  = $row['webuser'];
    
    $_SESSION [webUserGroupNames] [] = $mywebgroup;
    $_SESSION [webDocgroups] [] = $mywebgroup;
    	}
    }


    If a user is a member of 5 groups I would like it to show up like so

    [webUserGroupNames] => Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] )

    [webDocgroups] => Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] )

    but what I have just keeps looping through and adding them over and over to the session. It doesn’t affect it at all but will have a session string of 20,000 variables be problematic? Also I want to keep the session info simple so I can do my debugging with out 20,000 characters on my screen!

    Any help would be great.
      Ross Sivills - MD AugmentBLU Edinburgh, Scotland UK
      AugmentBLU - MODX Partner

      BLUcart - MODX Revolution E-Commerce & Shopping Cart
      • 4041
      • 788 Posts
      Maybe this will help smiley

      $groups = $modx->db->select("*", "modx_web_groups", "webuser = 10 ") or die ("table not found");
      if  ( mysql_num_rows($groups) )  {
              $mywebgroup  =array();
              //$mywebuser  =array();
              while( $row = mysql_fetch_array($groups ) ) {
                      extract($row);
                      $row['webgroup'];
                      //$row['webuser'];
                      $mywebgroup[]  .= $row['webgroup'];
                      //$mywebuser[]  .= $row['webuser'];
               }
               
      $_SESSION[webUserGroupNames] = $mywebgroup;
      $_SESSION[webDocgroups]      = $mywebgroup;
      
      }


      I commented out the $webuser parts since they didn’t seem to be useful at the moment. I also have some code that gets the actual group names and populates the $_SESSION[webUserGroupNames] if you need it.
        xforum
        http://frsbuilders.net (under construction) forum for evolution
        • 25551 ☆ A M B ☆
        • 1,231 Posts
        Perfecto! Thanks a lot Breezer.
          Ross Sivills - MD AugmentBLU Edinburgh, Scotland UK
          AugmentBLU - MODX Partner

          BLUcart - MODX Revolution E-Commerce & Shopping Cart
          • 32699 ☆ A M B ☆
          • 427 Posts
          http://php.net/manual/en/function.array-unique.php

          You are going about this the hard way. Once the user is logged in, their group information is already in the $_SESSION. You don’t need to add them in every time. And I do not remember ever seeing an instance where there were multiple instances of a group name placed in the $_SESSION by MODx.

          You can treat $_SESSION[’webUserGroupNames’] like any other array.

          Maybe, you need to make a protected page and then place a snippet in it with:

          <?php
          print_r($_SESSION);
          ?>


          you could also
          print_r($_SESSION['webUserGroupNames']);


          To see what is in it. It will only work on logged in people, as that is how the $_SESSION is created.

          A $_SESSION of 20,000 variables? I would be considering a cleaner solution.

          20,000 variables x 100,000 people or even 150 people is huge on the memory.

          Using extract() is not a decent solution either.

            Get your copy of MODX Revolution Building the Web Your Way http://www.sanitypress.com/books/modx-revolution-building-the-web-your-way.html

            Check out my MODX || xPDO resources here: http://www.shawnwilkerson.com
            • 25551 ☆ A M B ☆
            • 1,231 Posts
            The reason I need this is cause users are able to add and remove themself from groups via Ajax. The plugin makes sure they are in the correct group to access certain pages. Not only that but the plugin is also integrated in to SMF as well. wink
              Ross Sivills - MD AugmentBLU Edinburgh, Scotland UK
              AugmentBLU - MODX Partner

              BLUcart - MODX Revolution E-Commerce & Shopping Cart