Codeigniter: Get array generated in library to use it on view - php

I'm having issue trying to bring an array filled with user information in library to show it in view.
I have this, in the library, here is generated the array.
$filter = $this->login_attribute.'='.$username;
$filter = "(&(objectCategory=person)({$filter}))";
$fields = array("cn","samaccountname","mail","memberof","department","givename","sn","usercedula","telephonenumber","phisycaldeliveryofficename");
$search = ldap_search($this->ldapconn,$this->search_base,$filter,$fields);
$entries = ldap_get_entries($this->ldapconn, $search);
$entries is the array with the user information. ¿How I move the array to view page?
Edit: The function where $entries is, return other value, $entries is use to reach that value.

A library is just a class. You load it:
$this->load->library('my_library');
Then you can use any functions in that library:
$this->my_library->my_function('some_data');
If you are expecting any return values you would use them for whatever, in an if statement, to assign to a variable, etc etc.
if ($this->my_library->my_function('some_data')) // Expecting a return of TRUE or FALSE
$my_variable = $this->my_library->my_function('some_data');
In your library, your function does whatever you need, and simply returns the data, just like any other function.
public function my_function($data)
{
// do some stuff
// return some results - variable or array or whatever
return $results;
}
Does that help at all? In your case you just need to return $entries.

You can pass data to view by doing something likes this:
in controller:
$data['entries']=$entries;
$this->load->view('view_name',$data);
in library:
class library_name
{
private $ci;
public function __construct()
{
$this->ci = &get_instance();
}
public function your_function(){
//$entries=...
$data['entries']=$entries;
$this->ci->load->view('view_name',$data);
}
}
in view_name:
print_r($entries);

Related

Return array variable in PHP

I have a function and I'd like to return a variable to another function.
Can I return the array variable so I can use the variable at other function?
public function update_mdr_pameran() {
//global $araydatamdr;
$this->config->set_item('compress_output', FALSE);
$araydatamdr['mdr_debit'] = trim($this->input->post('mdr_debit'));
$araydatamdr['mdr_debit_npg'] = trim($this->input->post('mdr_debit_npg'));
$araydatamdr['mdr_debit_pl'] = trim($this->input->post('mdr_debit_pl'));
return $araydatamdr;
}
When I try to use $araydatamdr in another function, it became 0.
Am I missing something?
You can achieve this by calling function and setting its return value to another variable.
Method 1 :
class Test extends CI_Controller {
public function __construct() {
parent::__construct();
}
public function update_mdr_pameran() {
//global $araydatamdr;
$this->config->set_item('compress_output', FALSE);
$araydatamdr['mdr_debit'] = trim($this->input->post('mdr_debit'));
$araydatamdr['mdr_debit_npg'] = trim($this->input->post('mdr_debit_npg'));
$araydatamdr['mdr_debit_pl'] = trim($this->input->post('mdr_debit_pl'));
return $araydatamdr;
}
public function test_func() {
$araydatamdr = $this->update_mdr_pameran();
var_dump($araydatamdr);
}
}
Or you can also set $araydatamdr to $this reference.
Method 2 :
class Test extends CI_Controller {
public $araydatamdr;
public function __construct() {
parent::__construct();
$this->araydatamdr = [];
}
public function update_mdr_pameran() {
$this->config->set_item('compress_output', FALSE);
$this->araydatamdr['mdr_debit'] = trim($this->input->post('mdr_debit'));
$this->araydatamdr['mdr_debit_npg'] = trim($this->input->post('mdr_debit_npg'));
$this->araydatamdr['mdr_debit_pl'] = trim($this->input->post('mdr_debit_pl'));
}
public function test_func() {
$this->update_mdr_pameran();
var_dump($this->araydatamdr);
}
}
Cross out the echo $araydatamdr; Arrays can be printed using var_dump or print_r. Also you can return an array in php but personally i prefer to json_encode it first so i return a json as the output of my function something like:
return json_encode($araydatamdr);
Then it's a simple function call.
I don't know your project structure but i am giving general guidance. Apart from that i don't see anything else that could block your function.
I edit my post because i saw the issue is to call the function. There are 2 ways depending where you call it. If the function is in the same class as the other function you want to call it you simple go for :
$result=$this->update_mdr_pameran();
I see that your function has no arguments so you don't need to set any. If it's in another file:
1) include your php file at top like :
require 'myphpclass.php';
*tip make sure your path is right.
2) Create a new class object and then call the function like :
$class= new myClass();
$result=$class->update_mdr_pameran();

Filtering the xml data is not working

I use external xml, this works, but I have another problem. In my __construct I have $elem which is responsible for filtering the xml data. But it not working. Please help. I dont how to bite it.
class Property {
public $xmlClass;
public $array = [];
public $elem = '';
public function __construct($xml,$elem) {
$this->xmlClass=$xml;
foreach($xml->list->film->$elem as $result) {
$array = array_push($result);
}
}
}
$result_scenario = new Property($xml,'scenario');
print_r($result_scenario);
The array_push method does not work like that.
You need to pass the array as the first parameter, than pass the element you want to push on it. In your case, it would be array_push($this->array, $result);. The function returns the new number of elements in the array.
See the documentation here http://php.net/manual/en/function.array-push.php

php recursive function return before end

i have got the following recursive function:
private function myRecursiveFunction()
{
$results = [];
//stuff related to $results
...
if(!$done){
.....
$this->myRecursiveFunction();
}
return $results;
}
when i do var_dump($results) inside function i got the all array results
but when i call the function from another one i will got only the first element in $results array.
public function myFunction()
{
$results = $this->myRecursiveFunction();
}
I am not 100% sure what you wish to accomplish here, but recursive functions usually send their "workload" all the way to the innermost function, then the results are "added" on each other at the way back. If this is indeed what you want, you may need to change your code to something like this:
private function myRecursiveFunction()
{
$results = [];
//stuff related to $results
...
if(!$done){
.....
// Add the computed results of the recursive call to our data stack
$results[] = $this->myRecursiveFunction();
}
// Return the entire result array
return $results;
}

PHP - Method Chaining - Best Approach?

I need a way to have breadcrumbs that are not generated from the URL.
My thought process is to have a class, "Breadcrumbs" which will work as:
$breadcrumbs = new BreadCrumb('<<')->crumb('www.google.com', 'Google')->crumb('www.youtube.com', 'Youtube');
Which will generate an array, and with each method chain it will push back into the array, resulting in one array which I can then turn into a URL structure.
I have tried the following:
class Breadcrumbs {
public $del;
public $breadcrumbs;
public function __construct($del)
{
$this->breadcrumbs = array();
$this->del = $del;
}
public function crumb($uri, $name)
{
$this->breadcrumbs[] = array($uri => $name);
}
}
This, however, does not provide an accurate result, and get 'unexpected '->'' when trying to do the structure as I plan to do.
Any ideas to where I am going wrong?
To do method chaining, you need to return an object from your method call. Typically you'll want:
return $this; // in function crumb()
The correct syntax to instantiate an object and immediately call a method on it is:
(new BreadCrumb('<<'))->crumb(..)
Without the extra parentheses it's ambiguous to PHP what you want to do.
Aside: you don't need to initialise your array inside the constructor, you can do that while declaring the array:
public $breadcrumbs = array();
Aside: this is pretty inefficient:
.. = array($uri => $name)
You'll have a hard time getting that $uri key back out of the array, which makes your code unnecessarily complicated. You should simply do:
$this->breadcrumbs[$uri] = $name;
Alternatively, if your URIs aren't guaranteed to be unique, use a structure that's easier to work with later:
$this->breadcrumbs[] = array($uri, $name); // $arr[0] is the URI, $arr[1] the name
You need to return $this from crumb to be able to do method chaining.
public function crumb($uri, $name)
{
$this->breadcrumbs[] = array($uri => $name);
return $this;
}

pass variables in array as function paramaters?

I have a function that does something similar to this:
function load_class($name){
require_once('classes/'.$name.'.php');
return new $name();
}
what I want to do is modify it so I can do something like this
function load_class($name, $vars = array()){
require_once('classes/'.$name.'.php');
return new $name($array[0], $array[1]);
}
The general gist of it is.
I want to be able to pass in an array of values that, gets used as the parameters for the class.
I dont want to pass in the actual array.
is this possible?
Of course, it's called var args and you want to unpack them. http://php.net/manual/en/function.func-get-arg.php. Check out the examples... unpacking an array of arguments in php.
See Also How to pass variable number of arguments to a PHP function
if you are trying to load classes then you could use __autoload function
more information here
You can call functions this way with call_user_func_array, but in the case of a class constructor, you should use ReflectionClass::newInstanceArgs:
class MyClass {
function __construct($x, $y, $z) { }
}
$class = new ReflectionClass("MyClass");
$params = array(1, 2, 3);
// just like "$instance = new MyClass(1,2,3);"
$instance = $class->newInstanceArgs($params);
Your code might look like this:
function load_class($name, $vars = array()){
require_once('classes/'.$name.'.php');
$class = new ReflectionClass($name);
return $class->newInstanceArgs($vars);
}

Categories