sql in helpers |
[eluser]chrisevans_au[/eluser]
hey guys just started using Codeigniter the other day and loving it. I only have one query and its about SQL in helpers. I'm creating a little app for a football league to manage there players stats. I have a class called "team" and another class called "round". in the show_all_rounds view I'm trying to pass the team ID and get the team name. so i have created a helper called site_helper.php and i have a function get_team_name() below but I'm getting an error Message: Undefined variable: this Filename: helpers/site_helper.php is there a better way to achieve what I'm trying to do? Code: function get_team_name($id){ thanks in advanced :-)
[eluser]Chathuranga Tennakoon[/eluser]
hi, did you load your helper in the controller that you are calling the relevant helper class? otherwise please load the helper as below.(i have assumed that your site_helper.php is direcly inside the helper directory) Code: $this->load->helper('site_helper'); // i have assumed that your helper name is site_helper
[eluser]chrisevans_au[/eluser]
yes i included that in the constructor of the round controller. Can you put querys to the database in a helper? is that the best practice? i also tried just putting a basic function in to see if it would work EG: Code: function get_team_name(){ this worked. but as soon as i add the query I get issues.
[eluser]Chathuranga Tennakoon[/eluser]
[quote author="chrisevans_au" date="1336995858"]yes i included that in the constructor of the round controller. Can you put querys to the database in a helper? is that the best practice? i also tried just putting a basic function in to see if it would work EG: Code: function get_team_name(){ this worked. but as soon as i add the query I get issues. [/quote] as far as i know, the better practice is to have SQL queries in the model.
[eluser]chrisevans_au[/eluser]
I might need to re look into the way i have set it up. I'll have another crack tomorrow and let you know the result. thank you so much Chathuranga
[eluser]Chathuranga Tennakoon[/eluser]
[quote author="chrisevans_au" date="1336997420"]I might need to re look into the way i have set it up. I'll have another crack tomorrow and let you know the result. thank you so much Chathuranga[/quote] if you can share your code here, i can go though your code and find out what is actually happening. then i will be able to find out a better way of implementing your requirement based on MVC principles.
[eluser]CroNiX[/eluser]
Yes the database and everything else in CI are available to helpers, libraries, etc. You just have to bring the CI superglobal object into it. A procedural function (helper) doesn't know what the hell CI is unless you bring it in. See the section: Utilizing CodeIgniter Resources within Your Library http://ellislab.com/codeigniter/user-gui...aries.html
[eluser]chrisevans_au[/eluser]
thanks CroNiX i got it working, I used a Library instead of a helper in the controller i added: Code: $this->load->library('site'); and in the Library i added a file Site.php: Code: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); insted of using $this i used $CI =& get_instance(); and in the view: Code: <td><?=$this->site->getTeamName($row["home"]);?> Vs </td> and BOOM!!!! its working. thank you all for your help. I relay like Codeigniter but I'm just learning where to put things.
[eluser]CroNiX[/eluser]
Good deal. The only suggestion I would have is to make the CI object a property of your library so you don't have to do it for each method (assuming you might add more methods later). Code: class Site {
[eluser]CroNiX[/eluser]
Also, if you know your query is only going to return 1 row, you can use row() and row_array(). And since AR is all OOP, we can save a variable and chain everything together Code: public function getTeamName($id) |
Welcome Guest, Not a member yet? Register Sign In |