Codeigniter Question - Solve your codeigniter question

  • Home
  • Business
    • Internet
    • Market
    • Stock
  • Parent Category
    • Child Category 1
      • Sub Child Category 1
      • Sub Child Category 2
      • Sub Child Category 3
    • Child Category 2
    • Child Category 3
    • Child Category 4
  • Featured
  • Health
    • Childcare
    • Doctors
  • Home
  • Business
    • Internet
    • Market
    • Stock
  • Downloads
    • Dvd
    • Games
    • Software
      • Office
  • Parent Category
    • Child Category 1
      • Sub Child Category 1
      • Sub Child Category 2
      • Sub Child Category 3
    • Child Category 2
    • Child Category 3
    • Child Category 4
  • Featured
  • Health
    • Childcare
    • Doctors
  • Uncategorized

Thứ Ba, 8 tháng 9, 2015

Query database in helper file Codeigniter 3

 Unknown     21:43     No comments   

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...
My Model looks as follow:
public function get_categoryads()
{

    $this->db->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->db->from('ads');
  $this->db->join('adsubcat', 'adsubcat.id=ads.subcatid');
  $this->db->join('adcat', 'adcat.id=ads.catid');
  $this->db->join('search_town', 'search_town.townId=ads.townId');
  $this->db->join('search_region', 'search_region.regionId=search_town.relRegionId');
  $this->db->join('search_prov', 'search_prov.provId=search_region.relProvId');
  $this->db->join('ad_image', 'ad_image.adid=ads.id');
  $array = array(
  'ad_image.picorder'=> 1,
  'ad_image.aproved'=> 1,
    'adcat.id' => 9,
   'scam' => 0,
    'adactive' => 1
    );

  $this->db->where($array);
  $this->db->group_by("ads.id");
  $this->db->order_by('addate','desc');
  $this->db->limit(5); 
  $query = $this->db->get(); 
  return $query->result_array();
}
My controller looks as follow:
class Categories extends CI_Controller {
          public function __construct()
          {
                parent::__construct();
                $this->load->model('categorylist_model');
                $this->load->helper('text');
          }
            public function index()
            {
            $this->load->helper('url');
            $data = array('sitename'=>'Title', 'page_title' => 'Categories');
          $data['categories'] = $this->categorylist_model->get_categoryads();
          $this->load->view('templates/header', $data);
            $this->load->view('templates/navbar');
            $this->load->view('templates/breadcrumbs');
            //$this->load->view('templates/category_selector');
            $this->load->view('content_categories');
            $this->load->view('templates/footer');
          }
}
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?
I tried to add the function to the controller as follow (Not correct):
class Categories extends CI_Controller {
          public function __construct()
          {
                parent::__construct();
                $this->load->model('categorylist_model');
                $this->load->helper('text');
          }
            public function index()
            {
            $this->load->helper('url');
            $data = array('sitename'=>'Title', 'page_title' => 'Categories');
          $data['categories'] = $this->categorylist_model->get_categoryads();
          $this->load->view('templates/header', $data);
            $this->load->view('templates/navbar');
            $this->load->view('templates/breadcrumbs');
            //$this->load->view('templates/category_selector');
            $this->load->view('content_categories');
            $this->load->view('templates/footer');
          }
          public function tLink( $aid ) {  
          $ci=& get_instance();
          $ci->load->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->db->query($sql);
          $row = $query->row();
          return $row->fvalue;
          }

}
and calling it in the with tLink($adid) but gives me Fatal error: Call to undefined function tLink()

Answer:
Your PHP Error mentioned in comments is correct, either you're:
  • Passing through an array instead of a string for $aid For example, you're passing through array( 0 => 45 ) instead of 45.
  • Attempting to echo the results, which is a an array/object. Instead, use Code Igniter's row() command, then cherry pick the result you require.

function tLink( $aid ) {  
    $ci=& get_instance();
    $ci->load->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->db->query($sql);
    $row = $query->row();
    return $row->fvalue;
}
Lastly, it's better to load this model within the Controller rather than having to require it at the helper(Normally used for the view functions)
/**
 *
**/
class Link_model extends CI_Model { 

    public function getLink( $aid ) { 
          $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->db->query($sql);
         $row = $query->row();
         return $row->fvalue;
    }
}

/**
 *
**/
class Categories extends CI_Controller {

    public function index() {
        $this->load->model( 'Link_model' );

        $categories = array();
        $getCategoryIds = $this->categorylist_model->get_categoryads();

        foreach( $getCategoryIds as $aid ){
             $categories = array(
                 "id"   => $aid,
                 "link" => $this->link_model->getLink( $aid )
             );
        }

        $data = array(
            "categories" => $categories
        );

        $this->load->view('templates/header', $data );
    }

}

Now finally in your view:
<div class="container">
    <?php if ( isset( $categories ) && count( $categories ) > 0 ): ?>
        <?php foreach( $categories as $category ): ?>
            <?=( $category["link"] )?>
        <?php endforeach; ?>
    <?php endif; ?>
</div>


  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Gửi email bài đăng nàyBlogThis!Chia sẻ lên XChia sẻ lên Facebook
Bài đăng Mới hơn Bài đăng Cũ hơn Trang chủ

0 nhận xét:

Đăng nhận xét

Popular Posts

  • MongoWriteConcernException. the (immutable) field '_id' was found to have been altered to _id
    while making updat in mongodb in codeigniter I have the following error plz help me to solve this issue, Type: MongoWriteConcernException ...
  • How to perform inline editing in Codeigniter PHP using AJAX?
    I saw an example in native PHP which allows inline editing or records and also updates them.  PHP Inline Editing  Now as per this code i h...
  • How to select data from 4 tables
    I want to select data from 4 tables using the Codeigniter framework. The 4 tables have a similar column structure. I want to get the table...
  • Codeigniter URL Routing Issue
    I'm new to codeigniter and want to set my URLs on website currently I'm using version 3.0.1 URL looks like http : //oti.nhmp.net...
  • Query database in helper file Codeigniter 3
    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 u...
  • codeigniter drop down value send view to controller and display results
    I am working with codeigniter project and I have a problem. I have tried several times but still not have a solution. My view has dropdo...

Recent Posts

Unordered List

Pages

  • Trang chủ

Text Widget

Blog Archive

  • ▼  2015 (7)
    • ▼  tháng 9 (7)
      • How to select data from 4 tables
      • MongoWriteConcernException. the (immutable) field ...
      • Query database in helper file Codeigniter 3
      • MySql select field_1 where X = field_2 for all sup...
      • How to perform inline editing in Codeigniter PHP u...
      • codeigniter drop down value send view to controlle...
      • Codeigniter URL Routing Issue

Giới thiệu về tôi

Unknown
Xem hồ sơ hoàn chỉnh của tôi
Được tạo bởi Blogger.

Sample Text

Copyright © Codeigniter Question - Solve your codeigniter question | Design by PHP Tutorial