Welcome Guest, Not a member yet? Register   Sign In
sql in helpers
#1

[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){
$query = $this->db->query('team', array('id' => $id));
$result = $query->result_array();
return $result["name"];
}


thanks in advanced :-)
#2

[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
#3

[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(){
return "name";
}

this worked. but as soon as i add the query I get issues.
#4

[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(){
return "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.
#5

[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
#6

[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.
#7

[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
#8

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

class Site {

    public function getTeamName($id)
    {
  $CI =& get_instance();
  $query = $CI->db->get_where('team', array('id' => $id));
  $team = $query->result_array();
  return $team[0]["name"];
    }
}

insted of using $this i used $CI =& get_instance(); and in the view:

Code:
<td>&lt;?=$this->site->getTeamName($row["home"]);?&gt; Vs </td>
<td>&lt;?=$this->site->getTeamName($row["away"]);?&gt;</td>

and BOOM!!!! its working. thank you all for your help.

I relay like Codeigniter but I'm just learning where to put things.
#9

[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 {
  public $CI;

  //Create a constructor and pass the $CI the instance
  function __construct()
  {
    $this->CI =& get_instance();
  }

  public function getTeamName($id)
  {
    //$CI =& get_instance();  Now you don't need this anymore

    //access with $this->CI instead of $CI now
    $query = $this->CI->db->get_where('team', array('id' => $id));
    $team = $query->result_array();
    return $team[0]["name"];
  }
}
#10

[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)
{
  $team = $this->CI->db
    ->get_where('team', array('id' => $id))
    ->row_array();  //row_array()
  return $team["name"]; //now don't need a 0 index
}




Theme © iAndrew 2016 - Forum software by © MyBB