Viewing 1 reply thread
  • Author
    Posts
    • #1573
      Marcus
      Member

      Hey guys, I’ve been using the piklist function for quite a while now, and what I love is that I can use an array after it, to build the choices for a select dropdown.

      So my code is:

      
        ,'choices' =>  piklist(
          get_users(
           array(
            'orderby' => 'user_login'
            ,'order' => 'asc'
            ,'role' => 'parent'
           )
           ,'objects'
          )
          ,array(
           'ID'
           ,'user_login'
          )
        )
      

      The get_users functions works within the piklist function to get the data in an array, however, the array right after get_users allows me to populate the value for the option with ID, and the option text with user_login.

      Great stuff.

      My question is this… In the array, can I combine results, so that the option would include something like:

      
      array (
      'ID'
      ,'user_login'.' - '.'display_name'
      )
      

      Is this possible currently? I know I’m not doing it right, but I’d love to know if its possible.

      Thanks

      Marcus

    • #1574
      Marcus
      Member

      Wow, decided to take a hack at making this idea work. So in class-piklist.php in the includes directory of piklist, I changed these lines: (around the 20th line after the piklist function starts)

      
      $_value = $arguments[1];
      $list[$_key] = is_object($value) ? $value->$_value : $value[$_value];
      

      to this:

      
      if (is_array($arguments[1])) {
        $tmpp = '';
        foreach($arguments[1] AS $pk=>$pval) {
          if ((is_object($value) && isset($value->$pval)) || (is_array($value) && isset($value[$pval]))) {
            $tmpp .= is_object($value) ? $value->$pval : $value[$pval];
          } else {
            $tmpp .= $pval;
          }
        }
        $list[$_key] = $tmpp;
      } else {
        $_value = $arguments[1];
        $list[$_key] = is_object($value) ? $value->$_value : $value[$_value];
      }
      

      which allows me to make an array of the secondary value in the piklist array argument, such as this:

      
      ,array(
       'ID'
       ,array('user_login',' - (','display_name',')')
      )
      

      And the result is option text that is more readable and flexible, inside my dropdown EG:

      super_admin – (Marcus Eby)
      bobjay – (Robert Jay)
      etc…

      The ideas are limitless with piklist.
      Thanks.

      Marcus

Viewing 1 reply thread
  • You must be logged in to reply to this topic.