Welcome Guest, Not a member yet? Register   Sign In
trying to load multiple functions within a function....
#1

[eluser]stevenazari[/eluser]
Hi guys, I know I've probably read the usermanual wrong but I have tried and searched the forums for help...

is there anyway I can load a function within another function or just call to it once the first function is completed?

I want to upload an mp3 and at the end of it do some repetative code (that is used throughout the site to get data)

I want the repetative code put into a function and then call the function when I want it...

I checked out here about loaders and didnt have any luck with that...

and I tried here (the functions page) and it said something about _remap() possibly being useful... can someone give some advice? the repetative function I have is called "function show()" so in the function I wanted to put it in I put in

Code:
function upload($show)
       {
       //did my magic
       $this->$show();
       }

but that didn't do anything.... the page should have redirected back to the upload page but it just stopped like it didn't load the function.

Any advice?
#2

[eluser]Ben Edmunds[/eluser]
Hey,

If it is a repetitive task that works with data then you might want to put it in a model. Then you can just load it with

Code:
$this->load->model('data_model');

and use it like

Code:
$this->data_model->do_something();

Also you have a problem with your syntax. When calling a function/method the syntax isn't:
Code:
function upload($show)
       {
       //did my magic
       $this->$show();
       }

it is:

Code:
function upload($show)
       {
       //did my magic
       $this->show();
       }

unless you are passing the name of the method you would like to call as $show, which probably isn't a good idea...
#3

[eluser]stevenazari[/eluser]
[quote author="Ben Edmunds" date="1255469309"]
Also you have a problem with your syntax. When calling a function/method the syntax isn't:
Code:
function upload($show)
       {
       //did my magic
       $this->$show();
       }

it is:

Code:
function upload($show)
       {
       //did my magic
       $this->show();
       }

unless you are passing the name of the method you would like to call as $show, which probably isn't a good idea...[/quote]

hehe I thought as much but on the userguide page it has:

Code:
function _remap($method)
{
    if ($method == 'some_method')
    {
        $this->$method();
//the error is above
    }
    else
    {
        $this->default_method();
    }
}

thought that might be wrong.

ok so I should put it in the model... gonna give it a whirl Big Grin hope it works!
#4

[eluser]BrianDHall[/eluser]
$this->$method() is the use of a variable function call - if you pass the remap function, say, "sha1" PHP would parse it as $this->sha1().
#5

[eluser]stevenazari[/eluser]
ahh ok,

well I used $this->show(); and it worked Smile which is great, am I doing it right though? would you guys recommend me do it a different way? I read about using the model and that pretty much seems like what I should have done? I can just copy that function over into a file in the models folder right? and just write the function and model opening code in the respective format?
#6

[eluser]jedd[/eluser]
If the function is required in one controller only, keep it in the controller and stick an _ at the start of its name (unless you actually want it to be publicly visible).
#7

[eluser]stevenazari[/eluser]
ok Smile well I'm accessing it through a javascript post... so that doesn't need to be public? or does it?
#8

[eluser]Colin Williams[/eluser]
Quote:If it is a repetitive task that works with data then you might want to put it in a model

If it is a task at all, it should be in a model.
#9

[eluser]stevenazari[/eluser]
oh... I've been putting them in the controller lols.

well I'll just move it into the model and call it. This is great now I can use the whole mvc lol... I just need to learn more and I'll be on my way Big Grin (Self taught is a pain >.<)




Theme © iAndrew 2016 - Forum software by © MyBB