CodeIgniter Forums
passing 1 (or more) param's to a controller function? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter Archived (from EllisLab) (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: passing 1 (or more) param's to a controller function? (/thread-35537.html)

Pages: 1 2


passing 1 (or more) param's to a controller function? - El Forum - 11-02-2010

[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
// if func called without a param!
// Not strictly required, but I read somewhere in the forum that its desirable!
function doSomething($in = NULL)
{
    if(NULL != $in)
    {
        // do something....
    }
    else
    {
        //nothing passed in so....
    }
}

But if I were to pass more than 1 or more params would I do:
Code:
function doSomething($in = NULL)
{
    if(NULL != $in)
    {
        $params = $this->uri->segment_array();
        foreach($params as $p)
        {
            // do something with $p ....
        }        
    }
    else
    {
        //nothing passed in so ....
    }
}

Is this correct / prefered method?

Cheers

Pete


passing 1 (or more) param's to a controller function? - El Forum - 11-02-2010

[eluser]danmontgomery[/eluser]
Code:
function doSomething($param1, $param2, $param3, ...) {



passing 1 (or more) param's to a controller function? - El Forum - 11-02-2010

[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 )


passing 1 (or more) param's to a controller function? - El Forum - 11-02-2010

[eluser]Frank Rocco[/eluser]
Would this work?
Code:
function doSomething($options = array()) {
if (isset($options['param1']))
...

if (isset($options['param2']))
...



passing 1 (or more) param's to a controller function? - El Forum - 11-02-2010

[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


passing 1 (or more) param's to a controller function? - El Forum - 11-02-2010

[eluser]danmontgomery[/eluser]
If you don't know the number of params, then yes, I would use $this->uri->segment_array()


passing 1 (or more) param's to a controller function? - El Forum - 11-02-2010

[eluser]Frank Rocco[/eluser]
Not sure I understand you.
What are you trying to do?
You could loop through the array.


passing 1 (or more) param's to a controller function? - El Forum - 11-02-2010

[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


passing 1 (or more) param's to a controller function? - El Forum - 11-02-2010

[eluser]Frank Rocco[/eluser]
Can you just check if the variable is an array?

http://php.net/manual/en/function.is-array.php


passing 1 (or more) param's to a controller function? - El Forum - 11-02-2010

[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