CodeIgniter Forums
Calling method inside controller from view - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Calling method inside controller from view (/showthread.php?tid=27902)



Calling method inside controller from view - El Forum - 02-23-2010

[eluser]huzzi[/eluser]
I like to know how to call a method inside a controller from a view, I don't want to put it inside a helper or in the model.

eg:

Code:
class Homepage extends MY_Controller {

   function __construct() {
        parent::__construct();
    }

function viewBasket(){
return something;
}

}

Then, inside the view.

Code:
<html>
.....

<?=viewBasket()?>

OR


<?=$this->viewBasket()?>

.....
</html>

I tried the above but I get error

Fatal error: Call to undefined method CI_Loader::viewBasket()


Please help.


Calling method inside controller from view - El Forum - 02-23-2010

[eluser]n0xie[/eluser]
Use the Search


Calling method inside controller from view - El Forum - 02-23-2010

[eluser]huzzi[/eluser]
[quote author="n0xie" date="1266990291"]Use the Search[/quote]

Thanks... i did search but this didn't come up.


Calling method inside controller from view - El Forum - 02-24-2010

[eluser]Zeeshan Rasool[/eluser]
<?=$this->viewBasket()?>
to
<?=$this->homepage->viewBasket()?>
but its not good way of calling method . call functions living inside your controller


Calling method inside controller from view - El Forum - 02-24-2010

[eluser]n0xie[/eluser]
[quote author="Zeeshan Rasool" date="1267024672"]<?=$this->viewBasket()?>
to
<?=$this->homepage->viewBasket()?>
but its not good way of calling method . call functions living inside your controller[/quote]
That doesn't work.


Calling method inside controller from view - El Forum - 02-24-2010

[eluser]SpooF[/eluser]
Gather all your data in the controller, pass it to the view and then render your page there. I dont see why you would want to call a method of your controller in your view.


Calling method inside controller from view - El Forum - 02-24-2010

[eluser]Zeeshan Rasool[/eluser]
[quote author="n0xie" date="1267026067"][quote author="Zeeshan Rasool" date="1267024672"]<?=$this->viewBasket()?>
to
<?=$this->homepage->viewBasket()?>
but its not good way of calling method . call functions living inside your controller[/quote]
That doesn't work.[/quote]
Oh sorry, i thought you need to call a func from model in the view. Why you need this calling you can call model function in the view ( Although its not good approach) but there is no meaning of calling controller function in view Smile


Calling method inside controller from view - El Forum - 02-25-2010

[eluser]ascotan[/eluser]
[quote author="SpooF" date="1267026308"]Gather all your data in the controller, pass it to the view and then render your page there. I dont see why you would want to call a method of your controller in your view.[/quote]

This.

Your view should not call the controller. This is what is technically known as "a$$backwordsness". You controller file should collect all your data from the model it needs to render, then pass that data to the view. The view will then render that data.


Calling method inside controller from view - El Forum - 09-28-2010

[eluser]developer10[/eluser]
[quote author="ascotan" date="1267132822"][quote author="SpooF" date="1267026308"]Gather all your data in the controller, pass it to the view and then render your page there. I dont see why you would want to call a method of your controller in your view.[/quote]

This.

Your view should not call the controller. This is what is technically known as "a$$backwordsness". You controller file should collect all your data from the model it needs to render, then pass that data to the view. The view will then render that data.[/quote]

that is like that in theory.
what would you suggest in a situation when i need to forach some records which come from the same table. But, when displaying the data, i need to catch lets say, value of ID and to use it for displaying count of rows from some other table.

i struggled for a long time in order to achieve that via using arrays but to no avail. Would you agree that using of

Code:
echo $this->bids->countBids($value['project_id']);

is a suitable solution for getting count when in loop?


Calling method inside controller from view - El Forum - 09-28-2010

[eluser]techgnome[/eluser]
I'd loop through the data, and use the ID to get more info, then using the ID as the key, add the returned data to an array... all in the model... from the view, it *should* be as simple then as echo $bidcounts[$projectid] -- if it's a single value ... or echo $moredata[$projectid]->bidcount ... at least that's how I'd start off... seems to make sense to me.

-tg