title: Query database in helper file Codeigniter 3
body: <p>I was trying to add a function to a helper file to collect extra info from the Db. This got errors as the the document id I am trying to use to call the function is put out as an array and not what I want...</p>
  
  <p>My Model looks as follow:</p>
  
  <pre><code>public function get_categoryads()
  {
  
      $this-&gt;db-&gt;select('ads.id AS id, ads.userid AS userid, ads.adnr AS adnr,
    ads.location AS town, search_town.townFileName AS townlink, search_prov.provLabel AS province, 
    search_prov.subLink AS provlink,text, ad_image.image AS image,addate, r_option, R_rand,
    adcat.name AS catname,  adcat.clinkname AS clinkname, adsubcat.name AS subcatname, adsubcat.linkname AS subcatlink,
    adcat.id AS catid, adsubcat.id AS subcatid, ads.r_option AS r_option, ads.R_rand AS R_rand, 
    adcat.catcol1 AS catcol1, adcat.catcol2 AS catcol2, search_town.townId,ads.townId');
    $this-&gt;db-&gt;from('ads');
    $this-&gt;db-&gt;join('adsubcat', 'adsubcat.id=ads.subcatid');
    $this-&gt;db-&gt;join('adcat', 'adcat.id=ads.catid');
    $this-&gt;db-&gt;join('search_town', 'search_town.townId=ads.townId');
    $this-&gt;db-&gt;join('search_region', 'search_region.regionId=search_town.relRegionId');
    $this-&gt;db-&gt;join('search_prov', 'search_prov.provId=search_region.relProvId');
    $this-&gt;db-&gt;join('ad_image', 'ad_image.adid=ads.id');
    $array = array(
    'ad_image.picorder'=&gt; 1,
    'ad_image.aproved'=&gt; 1,
      'adcat.id' =&gt; 9,
     'scam' =&gt; 0,
      'adactive' =&gt; 1
      );
  
    $this-&gt;db-&gt;where($array);
    $this-&gt;db-&gt;group_by("ads.id");
    $this-&gt;db-&gt;order_by('addate','desc');
    $this-&gt;db-&gt;limit(5); 
    $query = $this-&gt;db-&gt;get(); 
    return $query-&gt;result_array();
  }
  </code></pre>
  
  <p>My controller looks as follow:</p>
  
  <pre><code>class Categories extends CI_Controller {
            public function __construct()
            {
                  parent::__construct();
                  $this-&gt;load-&gt;model('categorylist_model');
                  $this-&gt;load-&gt;helper('text');
            }
              public function index()
              {
              $this-&gt;load-&gt;helper('url');
              $data = array('sitename'=&gt;'Title', 'page_title' =&gt; 'Categories');
            $data['categories'] = $this-&gt;categorylist_model-&gt;get_categoryads();
            $this-&gt;load-&gt;view('templates/header', $data);
              $this-&gt;load-&gt;view('templates/navbar');
              $this-&gt;load-&gt;view('templates/breadcrumbs');
              //$this-&gt;load-&gt;view('templates/category_selector');
              $this-&gt;load-&gt;view('content_categories');
              $this-&gt;load-&gt;view('templates/footer');
            }
  }
  </code></pre>
  
  <p>I am now not to sure how to add the extra query to the Controller? Or should I add it inside the model itself and how?</p>
  
  <p>I tried to add the function to the controller as follow (Not correct):</p>
  
  <pre><code>class Categories extends CI_Controller {
            public function __construct()
            {
                  parent::__construct();
                  $this-&gt;load-&gt;model('categorylist_model');
                  $this-&gt;load-&gt;helper('text');
            }
              public function index()
              {
              $this-&gt;load-&gt;helper('url');
              $data = array('sitename'=&gt;'Title', 'page_title' =&gt; 'Categories');
            $data['categories'] = $this-&gt;categorylist_model-&gt;get_categoryads();
            $this-&gt;load-&gt;view('templates/header', $data);
              $this-&gt;load-&gt;view('templates/navbar');
              $this-&gt;load-&gt;view('templates/breadcrumbs');
              //$this-&gt;load-&gt;view('templates/category_selector');
              $this-&gt;load-&gt;view('content_categories');
              $this-&gt;load-&gt;view('templates/footer');
            }
            public function tLink( $aid ) {  
            $ci=&amp; get_instance();
            $ci-&gt;load-&gt;database(); 
  
            $sql ="SELECT adfields.f_value AS fvalue FROM adfields
                  JOIN field_name ON field_name.id=adfields.f_id
                  WHERE adid='$aid' GROUP BY adfields.f_id ORDER BY field_name.keyf ASC"; 
  
            $query = $ci-&gt;db-&gt;query($sql);
            $row = $query-&gt;row();
            return $row-&gt;fvalue;
            }
  
  }
  </code></pre>
  
  <p>and calling it in the with tLink($adid) but gives me Fatal error: Call to undefined function tLink()</p>