0

好的,下面是我的控制器中的一个 get_members 函数,我从互联网上提取和操作它......我可以从控制器中的 $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);
}
}
4

1 回答 1

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); ?>
于 2012-06-11T19:48:25.597 回答