Welcome Guest, Not a member yet? Register   Sign In
Multiple method calls from library
#1

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?
Reply
#2

can you show your library?
Caffeine IT Solutions | Bina Darma University
Reply
#3

Try making your $value array static.

You will then need a clear method to reset it.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#4

(This post was last modified: 11-05-2016, 02:18 PM by ManOfHonor.)

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 '';
    }

Reply
#5

Yes, but like I said you will need to create a clear method to reset your value array.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#6

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;
 
   }


Caffeine IT Solutions | Bina Darma University
Reply
#7

(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
Reply




Theme © iAndrew 2016 - Forum software by © MyBB