passing 1 (or more) param's to a controller function? |
[eluser]PeteSig[/eluser]
From the tutorials / examples / user guide, if you're only going to pass a single param you do: Code: // set default NULL value to $in so we don't crash & burn But if I were to pass more than 1 or more params would I do: Code: function doSomething($in = NULL) Is this correct / prefered method? Cheers Pete
[eluser]danmontgomery[/eluser]
Code: function doSomething($param1, $param2, $param3, ...) {
[eluser]PeteSig[/eluser]
Hmmm, but what if I don't know the exact number of param's that might be passed? I know there should be at least 1 but there could be (n>1). ----- Just found that Php manual covers this with 'func_num_args' and assoc'd func's, & my code snippet does it in much the same way using CI, so I guess it's ok. Cheers Pete (not only getting to grips with CI, but also php :-S )
[eluser]Frank Rocco[/eluser]
Would this work? Code: function doSomething($options = array()) {
[eluser]smilie[/eluser]
Frank, That says that $options is first param, which is array (and that is allowed). But generally, how would come that you do not know the number of params? Cheers, Smilie
[eluser]danmontgomery[/eluser]
If you don't know the number of params, then yes, I would use $this->uri->segment_array()
[eluser]Frank Rocco[/eluser]
Not sure I understand you. What are you trying to do? You could loop through the array.
[eluser]PeteSig[/eluser]
Thanks for replies. To elaborate on the background as to why I asked the question: I have a function 'delete($id)' which deletes an item from a db. I then decided to allow the user to select a number of items from a list and to delete all the selected items. So I created another function 'deleteSelected()' which gets all the selected item id's from $_POST, in the form of an array, and then runs throught them and does a delete($id) on them. I thought it might be cleaner if I could call the single 'delete()' func with 1 or many item id's! So, that's the background info, which I hope explains how come I don't know the number of parameters which might be passed. Also, I wasn't sure on what the best practice might be - to pass the id's as an array via $_POST, or as parameters via the URL!?!? I guess this is the real nub of the question - why pass via URL instead of via $_POST & / or visa versa? Cheers Pete
[eluser]Frank Rocco[/eluser]
Can you just check if the variable is an array? http://php.net/manual/en/function.is-array.php
[eluser]smilie[/eluser]
I would agree with Frank. Always send 1 param and then in the function check if it is array. It yes - loop it; If no - do a single item delete function. Cheers, Smilie |
Welcome Guest, Not a member yet? Register Sign In |