2012-06-11 97 views
0

好吧,下面是我的控制器中的一個get_members函數,我從interwebs中拉出並操作了......我可以從控制器中的$ output輸出信息,但我不想做那我......我無法弄清楚如何讓它成爲我的觀點的一部分,所以我可以自由地列出信息......傳遞兩個數組以查看

我知道它與索引函數中的$ data數組有關。 ..任何人都可以協助?

<?php if (! defined('BASEPATH')) exit('No direct script access allowed'); 

class Phonedirectory extends CI_Controller { 

function get_members($group=FALSE,$inclusive=FALSE) { 
    // Active Directory server 
    $ldap_host = "fps"; 

    // Active Directory DN 
    $ldap_dn = "OU=Users,DC=xxx,DC=org"; 

    // User attributes we want to keep 
    $keep = array(
     "samaccountname", 
     "displayName", 
     "telephonenumber" 
    ); 

    // Connect to AD 
    $ldap = ldap_connect($ldap_host) or die("Could not connect to LDAP"); 
    ldap_set_option ($ldap, LDAP_OPT_REFERRALS, 0); 
    ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, 3); 

    ldap_bind($ldap, "CN=LDAP Reader,OU=Users - Special,DC=xxx,DC=org", "xxx") or die("Could not bind to LDAP"); 

    // Begin building query 
    if($group) $query = "(&"; else $query = ""; 

    $query .= "(&(objectClass=user)(objectCategory=person))"; 

    // Filter by memberOf, if group is set 
    if(is_array($group)) { 
     // Looking for a members amongst multiple groups 
      if($inclusive) { 
       // Inclusive - get users that are in any of the groups 
       // Add OR operator 
       $query .= "(|"; 
      } else { 
       // Exclusive - only get users that are in all of the groups 
       // Add AND operator 
       $query .= "(&"; 
      } 

      // Append each group 
      foreach($group as $g) $query .= "(memberOf=OU=$g,$ldap_dn)"; 

      $query .= ")"; 
    } elseif($group) { 
     // Just looking for membership of one group 
     $query .= "(memberOf=OU=$group,$ldap_dn)"; 
    } 

    // Close query 
    if($group) $query .= ")"; else $query .= ""; 

    // Uncomment to output queries onto page for debugging 
    // print_r($query); 

    // Search AD 
    $results = ldap_search($ldap,$ldap_dn,$query); 
    $entries = ldap_get_entries($ldap, $results); 

    // Remove first entry (it's always blank) 
    array_shift($entries); 

    $output = array(); // Declare the output array 

    $i = 0; // Counter 
    // Build output array 
    foreach($entries as $u) { 
     foreach($keep as $x) { 
      // Check for attribute 
      if(isset($u[$x][0])) $attrval = $u[$x][0]; else $attrval = NULL; 

      // Append attribute to output array 
      $output[$i][$x] = $attrval; 
     } 
     $i++; 
    } 
    return $output; 
} 

public function index() { 
    $data = array('title' => 'Phone Directory', 'main_content' => 'pages/phoneDirectory');  
    $this->load->view('template/main', $data); 
} 
} 

回答

2

糾正我,如果我錯了,但它聽起來像你只是希望輸出發送到視圖? 如果是這樣的話,那麼這增加了索引功能

public function index() { 
    $data = array('title' => 'Phone Directory', 'main_content' => 'pages/phoneDirectory'); 
    $data['members'] = $this->get_members(); 
    $this->load->view('template/main', $data); 
} 

或不過要附加,爲您的數據陣列。 然後在視圖中你可以這樣做:

<?php echo print_r($members, TRUE); ?> 
+0

php短標籤默認在CI中禁用。 – Catfish