CodeIgniter Forums
Segment/passing to method problem - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Segment/passing to method problem (/thread-21283.html)



Segment/passing to method problem - El Forum - 08-05-2009

[eluser]rossmurphy[/eluser]
I have a method in a controller like this:

Code:
function index($additionalData = array ()) {

}

I need to be able to pass

Code:
roomId=34

But i have to do it using

Code:
$this->uri->segment(4)

Which is fine. BUT, when i try and pass this

Code:
example.com/controller/index/34

It tries to pass it into the function, and creates an error because i use this internally to pass arrays... is there another way of doing this?

Thanks.


Segment/passing to method problem - El Forum - 08-05-2009

[eluser]bretticus[/eluser]
Do not assume that the parameter will be an array by default. Especially if it's optional. Do the type checking within the method (to tell you the truth I cannot think of one use case where an array needs to be passed from a controller method since the controller is "hooked" via the URL. Why call controller methods internally? Perhaps you can enlighten me.)

Code:
function index($additionalData = null) {
if is_array($additionalData) {
  // handle extra params.
}
}



Segment/passing to method problem - El Forum - 08-05-2009

[eluser]rossmurphy[/eluser]
Say i have a reg user method called index and then a method called process. index loads the view that contain the reg form then when submitted form calls the process method....then depending on the outcome will either call the index method and pass an array of new params to merge with the exisiting array to be passed into the views (such as errors) or it will redirect the user to a confirmation/activaion page....


Segment/passing to method problem - El Forum - 08-05-2009

[eluser]jcavard[/eluser]
You should avoid reserved names, such as 'Index'.
http://ellislab.com/codeigniter/user-guide/general/reserved_names.html

You could also have one controller to show form and to process it.

Code:
function controllerName()
{
  // check if form is submitted
  if($this->input->post('submitted') == 1)
  {
    // process form validation and everything else
    // ...
  }
  else
  {
    // load form view here
  }
}

in your form view, just add
Code:
<input type="hidden" name="submitted" value="1" />
So, when the form is posted, this value is 1, otherwise the value is undefined and you just display the view with the form (no processing yet).

hope this help!


Segment/passing to method problem - El Forum - 08-05-2009

[eluser]bretticus[/eluser]
[quote author="rossmurphy" date="1249518730"]Say i have a reg user method called index and then a method called process. index loads the view that contain the reg form then when submitted form calls the process method....then depending on the outcome will either call the index method and pass an array of new params to merge with the exisiting array to be passed into the views (such as errors) or it will redirect the user to a confirmation/activaion page....[/quote]

Forgive my ignorance here, but shouldn't you just load the view with the form instead of calling the controller index method from another controller method? You can always pass that data to the view with the second parameter. Also, the form validation class can do this automatically.


Segment/passing to method problem - El Forum - 08-05-2009

[eluser]rossmurphy[/eluser]
i don't call the index function from another method, at first. The form is simply loaded. But i use another method to process the form data and do some xml parsing and THEN call the form method and reload the form depending on certain vars. As jcavard pointed out, i can do this all in one method, was just wondering if anyone had another option for this so i could make use of methods in m controllers.

Thanks anyway to both of you, looks like i will rethink my structure.