CodeIgniter Forums
Multiple method calls from library - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=11)
+--- Thread: Multiple method calls from library (/showthread.php?tid=66560)



Multiple method calls from library - ManOfHonor - 11-04-2016

Hi there,

I would like to know how can I call two methods from library in a loop? For example I have library called "Test" and I load it in my controller
PHP Code:
$this->load->library('test'); 

I have array with values, for example:
PHP Code:
$values = array(1020304050); 

Now I have to loop through that array and call two methods from my library:
PHP Code:
foreach($values as $value) {
    
$this->test->set($value);
    
$returned_value $this->test->get();
    echo 
$returned_value.'<br>';


My problem is that after the first call of these methods I can see returned value but after that I'm not getting any remaining values. How can I achieve that in CodeIgniter?


RE: Multiple method calls from library - ramadhansutejo - 11-04-2016

can you show your library?


RE: Multiple method calls from library - InsiteFX - 11-05-2016

Try making your $value array static.

You will then need a clear method to reset it.


RE: Multiple method calls from library - ManOfHonor - 11-05-2016

Thanks for yours reply. This is example of my library:
PHP Code:
defined('BASEPATH') OR exit('No direct script access allowed');

class 
Test extends CI_Controller
{
    protected 
$value;
        
    public function 
set($value)
    {
        
$this->value $value;
    }
        
    public function 
get()
    {
        return 
$this->value;
    }


InsiteFX, do you mean something like this:
PHP Code:
defined('BASEPATH') OR exit('No direct script access allowed');

class 
Test extends CI_Controller
{
    protected static 
$value;
        
    public function 
set($value)
    {
        
self::$value $value;
    }
        
    public function 
get()
    {
        return 
self::$value;
    }

    public function 
clear()
    {
        
self::value '';
    }




RE: Multiple method calls from library - InsiteFX - 11-06-2016

Yes, but like I said you will need to create a clear method to reset your value array.


RE: Multiple method calls from library - ramadhansutejo - 11-06-2016

you mean like this?
[Image: library_loop_juipk0.png]
try to re-write tour library to :
PHP Code:
class Test
{
    
    protected 
$ci;
    protected 
$value;

    public function 
__construct()
    {
 
       $this->ci =& get_instance();
    }

    public function 
set($value)
 
   {
 
       $this->value $value;
 
   }
 
       
    public 
function get()
 
   {
 
       return $this->value;
 
   }





RE: Multiple method calls from library - ManOfHonor - 11-08-2016

(11-06-2016, 06:44 PM)ramadhansutejo Wrote: you mean like this?
[Image: library_loop_juipk0.png]
try to re-write tour library to :
PHP Code:
class Test
{
    
    protected 
$ci;
    protected 
$value;

    public function 
__construct()
    {
 
       $this->ci =& get_instance();
    }

    public function 
set($value)
 
   {
 
       $this->value $value;
 
   }
 
       
    public 
function get()
 
   {
 
       return $this->value;
 
   }



Thanks for reply,

I made a mistake and tried to extend CI_Controller in my library. Also I forgot that if I need to use CodeIgniter resources I can't extend it but I have to assign CodeIgniter object to variable by reference like you do.

Regards,
ManOfHonor