We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 2912
    • 315 Posts
    Hi All,
    
    I have modified (ShowUserList) a small snippet which I would like to use twice on one page.  Well once in a side bar from a template and again on another page showing different details.  
    
    The problem is when I goto the page which gives me things twice I dont see the entire picture!
    
    The code: (please excuse any bad programming practices)
    [code:]
    <?php
    // ShowUserList - MODx snippet to display the users assigned to a given web group.
    // the webgroup is set in the snippet properties tab or the snippet call.
    // &webgroup=webgroup;text;webgroupname
    // [[ShowUserList? &webgroup=`webgroupname` &type=`count` &tpl=`mylist`]]

    /**
    &type parameter.
    default: both
    option: 1 - count - display a total number only
    option: 2 - list - display a list only

    &tpl parameter.
    default = ''
    Optional value for a template chunk name.
    **/
    // Default template
    $deftpl ='<h3>[+count+] Members Registered.</h3>

    <ul>[+users+]<ul>';

    $type = isset($type) ? $type : 'both';
    $tpl = isset($tpl) ? $modx->getChunk($tpl) : '';
    $output = $tpl;

    // check for valid web user group
    if(!isset($webgroup) || $webgroup == '') { return; } // quietly do nothing if webgroup is not set
    $result = $modx->db->select('id',$modx->getFullTableName('webgroup_names'),"name = '".$webgroup."'");
    if($modx->db->getRecordCount($result) != 1) { return; } // quietly do nothing if webgroup does not exist

    $table1 = $modx->getFullTableName('web_users');
    $table2 = $modx->getFullTableName('webgroup_names');
    $table3 = $modx->getFullTableName('web_groups');
    $results = $modx->db->select("tbl1.username", "$table1 AS tbl1, $table2 AS tbl2, $table3 AS tbl3", "tbl2.name='$webgroup' AND tbl3.webgroup=tbl2.id AND tbl1.id = tbl3.webuser", 'tbl1.username ASC');

    if ($type == 'count' or $type == 'both') {
    $amount = $modx->db->getRecordCount($results);
    }

    if ($type == 'list' or $type == both) {
    $userlist .= '<ul>';
    while($users = $modx->db->getRow($results)) {
    $userlist .= '<li>'.$users['username'].'</li>';
    }
    $userlist .='</ul>';
    }

    $modx->setPlaceholder('count',$amount);
    $modx->setPlaceholder('users',$userlist);

    return $output;
    ?>
    <br /><br />The snippet shows either a list of users and the number involved or just a list or just a number. If I have the number in a small box on the side of my main content its ok but as soon as I go to another page which gives me the number and the list of users, the users just dont show.<br /><br />How can I make the snippet work.
      BBloke
      • 21257 MODX Staff
      • 730 Posts
      You might try giving the snippet an additional parameter - id. If each call to the snippet has a unique id, and you append the id to the placeholder names, that might solve the problem of the same placeholders being set twice.
      So if your snippet call was:
      [[ShowUserList? &webgroup=`webgroupname` &type=`count` &tpl=`mylist` id=`side`]]
      


      And you prepended the id to the name of the placeholder (this is just an example of changing one placeholder):
      $modx->setPlaceholder("$id-count",$amount);
      


      Then in your template for the side, you would refer to the count placeholder like:
      [+side-count+]
      


        Mike Schell
        Lead Developer, MODX Cloud
        Email: [email protected]
        GitHub: https://github.com/netProphET/
        Twitter: @mkschell
        • 2912
        • 315 Posts
        Thanks for that netProphET. I will make the changes and it sound like it will certainly do the business.. I’ll post back shortly.

          BBloke
          • 2912
          • 315 Posts
          Thanks again.. here’s the amended code and working a treat:

          <?php
          // ShowUserList - MODx snippet to display the users assigned to a given web group.
          // the webgroup is set in the snippet properties tab or the snippet call.
          // &webgroup=webgroup;text;webgroupname
          // [[ShowUserList? &webgroup=`webgroupname` &type=`count` &tpl=`mylist` &id=`new`]]
          // You can also put [+count+] and [+users+] placeholders in the body of your content and it will still work.
          // When you use &id pleae prefix your placeholders and seperate them with .
          // Ex: [[ShowUserList? &id='new' &webgroup=`Registered Users`]]
          // [+new.count+] [+new.users+]
          
          /**
          &type parameter. (default: both)
          
          option: 1 - count - display a total number only
          option: 2 - list - display a list only
          
          &tpl parameter. (default = '')
          Optional value for a template chunk name complete with placeholders.
          
          &id parameter
          Optional name to add to placeholders to allow for multiple uses on a webpage.
          **/
          
          // Default template
          $deftpl ='<h3>[+count+] Members Registered.</h3><br /><ul>[+users+]<ul>';
          
          $type = isset($type) ? $type : 'both';
          $id = isset($id) ? $id : '';
          $tpl = isset($tpl) ? $modx->getChunk($tpl) : '';
          $output = $tpl;
          
          // check for valid web user group
          if(!isset($webgroup) || $webgroup == '') { return; } // quietly do nothing if webgroup is not set
          $result = $modx->db->select('id',$modx->getFullTableName('webgroup_names'),"name = '".$webgroup."'");
          if($modx->db->getRecordCount($result) != 1) { return; } // quietly do nothing if webgroup does not exist
          
          $table1 = $modx->getFullTableName('web_users');
          $table2 = $modx->getFullTableName('webgroup_names');
          $table3 = $modx->getFullTableName('web_groups');
          $results = $modx->db->select("tbl1.username", "$table1 AS tbl1, $table2 AS tbl2, $table3 AS tbl3", "tbl2.name='$webgroup' AND tbl3.webgroup=tbl2.id AND tbl1.id = tbl3.webuser", 'tbl1.username ASC');
          
          if ($type == 'count' or $type == 'both') {
              $amount = $modx->db->getRecordCount($results);
          }
          
          if ($type == 'list' or $type == both) {
          $userlist .= '<ul>';
              while($users = $modx->db->getRow($results)) {
                    $userlist .= '<li>'.$users['username'].'</li>';
              }
          $userlist .='</ul>';
          }
          
          //placeholder definition
          if ($id !='') {
          	$count .= $id.'.count';
          	$users .= $id.'.users';
          } else {
          $count = 'count';
          $users = 'users';
          }
          
          $modx->setPlaceholder($count,$amount);
          $modx->setPlaceholder($users,$userlist);
          
          return $output;
          ?>
          
            BBloke