0

I have an method in Code Igniter, and want access using this URL "kuesioner/test/school_id/6/parent_id/7" I using _remap function to dynamic order of params in URL. Code from this. This is my code :

public function test($school_id, $parent_id)
{
    echo "<pre>";
    print_r($schoold_id);
    print_r($parent_id);
    echo "</pre>";
}

public function _remap($method, $params)
{
    $map = array();
    for( $i = 1; $i < count( $params ); $i = $i + 2 )
    {
        $map[$params[$i-1]] = $params[$i];
    }

    if( $method[0] != '_' && method_exists( $this, $method ))
        return $this->$method($map);
}

Getting error on my controller that said Missing argument 2 for Kuesioner::test() ($parent_id is missing), it return array of map into single variable $school_id in test controller.

print_r($school_id) exactly show

  Array
  (
    [school_id] => 6
    [parent_id] => 7
  )

How to solve this, so that value can be put into every right variable..

Community
  • 1
  • 1
Ayip.Eiger
  • 11
  • 1
  • 2
  • 6

4 Answers4

1

Always assign parameter with default values like 0 or anything else it will prevent from unnecessary warning and errors.

public function test($school_id = 0, $parent_id = 0)
{
    echo "<pre>";
    print_r($schoold_id);
    print_r($parent_id);
    echo "</pre>";
}
Pramod Kumar Sharma
  • 7,851
  • 5
  • 28
  • 53
  • What I want is when I request "kuesioner/test/school_id/6/parent_id/7" in test method it would print $school_id = 6 and $parent_id = 7. That's it. – Ayip.Eiger May 14 '13 at 03:58
  • I think CI automatically do this without any extra efforts. I am suggesting you to initialize these parameter will help you to prevent from warnings and errors – Pramod Kumar Sharma May 14 '13 at 04:00
  • And when I request "kuesioner/test/parent_id/7", it should only giving value 7 to var $parent_id in test function. – Ayip.Eiger May 14 '13 at 04:01
  • But what happened now is in test method, it return only single variable for $school_id that valued Array ( [school_id] => 6 [parent_id] => 7 ) $parent_id always 0 right now. – Ayip.Eiger May 14 '13 at 04:03
0
public function test($params = NULL)
{
    print_r($this->uri->uri_to_assoc(3));
    // #CHECK http://ellislab.com/codeigniter/user-guide/libraries/uri.html
}
despina
  • 437
  • 3
  • 13
  • Yes I appreciate your solving, but I want to strict focus about my problem. Still want to know how to return value $map with right purpose to every parameter in test method. – Ayip.Eiger May 14 '13 at 07:50
0

Try to replace last line with this:

return call_user_func_array(array($this, $method), $map);

I think that would solve your issue,however if not, you can edit your "test" function to :

public function test($params)
{
  echo "<pre>";
  print_r($params['schoold_id']);
  print_r($params['parent_id']);
  echo "</pre>";
}
Walid Ammar
  • 4,038
  • 3
  • 25
  • 48
-1

why you don't try with $this->uri->uri_to_assoc(n). See detail : http://ellislab.com/codeigniter/user-guide/libraries/uri.html don't need to use _remap

JamesN
  • 387
  • 1
  • 9
  • Did you missing code in last? I have tryyour code, but it's return blank. There is no variable that take to test method. – Ayip.Eiger May 14 '13 at 04:23
  • yes. I miss return $this->$method($map); at last of function – JamesN May 14 '13 at 04:47
  • why you don't try with $this->uri->uri_to_assoc(n). http://ellislab.com/codeigniter/user-guide/libraries/uri.html . don't need to use _remap – JamesN May 14 '13 at 04:47
  • Please edit this answer according to your suggestion to use $this->uir->uri_to_assoc(n). You don't want to leave the answer misleading other users. – Marko Aleksić May 14 '13 at 06:15