Welcome Guest, Not a member yet? Register   Sign In
uri_to_assoc problem
#1

[eluser]smith[/eluser]
When controller is executed, first uri_to_assoc(model) will execute and set false to var3 if var3 is not found inside uri segment, but second execution (controller), inside index function will not set var1 and var2 to false if they are not found inside uri segment. Looks like uri_to_assoc can only be called once?

Code Sample:

controller:
Code:
class Some_class extends Controller {

    var $data = array();
    function Some_class()
    {
        parent::Controller();
        
        $this->load->database();
        $this->load->model('my_model');
        $this->my_model->some_function();
    }
    
    function index()
    {
        $default = array('var1','var2');
        $array = $this->uri->uri_to_assoc(3, $default);
        print_r ($array);
    }
}

model:
Code:
class My_model extends Model {

    function some_function()
    {
        $default = array('var3');
        $array = $this->uri->uri_to_assoc(3, $default);
        print_r ($array);
    }
}

I would expect to have this as a result:

Array ( [var3] => )
Array ( [var1] => [var2] => )

But instead i am getting this:

Array ( [var3] => )
Array ( [var3] => )
#2

[eluser]gtech[/eluser]
Hello, I read your post and cut and pasted your code exactly.

I typed the URL (http://localhost/..<myinstall>../index.php/some_class/) in firefox on a windows platform and I got the following result:
Code:
Array ( [var3] => ) Array ( [var1] => [var2] => )
#3

[eluser]smith[/eluser]
Same here.
But when i do this:

http://localhost/..<myinstall>../index.p...dex/var3/2

i get:
Array ( [var3] => 2 ) Array ( [var3] => 2 )

var1 and var2 were not set to false.

if i do this:

http://localhost/..<myinstall>../index.p...dex/var2/2

result:
Array ( [var2] => 2 [var3] => ) Array ( [var2] => 2 [var3] => )

var1 was not set to false.
#4

[eluser]gtech[/eluser]
its because in the URI code the array is cached

see line 202

libraries/URI.php
Code:
$this->keyval[$n] = $retval;

when you call the uri_to_assoc function again it checks the cached value and just returns it

see line 150
libraries/URI.php
Code:
if (isset($this->keyval[$n]))
{
  return $this->keyval[$n];
}



comment this line out and the code works like you want.. OR

Code:
&lt;?php
class Some_class extends Controller {

    var $data = array();
    function Some_class()
    {
        parent::Controller();
        
        $this->load->database();
        $this->load->model('my_model');
        $this->my_model->some_function();
    }
    
    function index()
    {
        $default = array('var1','var2');
        // ADD THIS LINE TO REMOVE THE URI CACHE
        $this->uri->keyval = array();
        $array = $this->uri->uri_to_assoc(3, $default);
        print_r ($array);
    }
}
?&gt;
#5

[eluser]smith[/eluser]
Thank you very much Smile
#6

[eluser]gtech[/eluser]
No problems, I learned something myself




Theme © iAndrew 2016 - Forum software by © MyBB