Codeigniter PHP MVC confusion (new CI child on block)
This is my first experience with Codeigniter and need help. I need to do a Db lookup with JOINS and WHERE statements following the CI 3 tutorial and web resources, but got myself firmly in the wall and not 100% sure where and what I am doing wrong ...
My Categorylis_model.php looks like this: class Categorylist_model extends CI_Model {
public function __construct()
{
$this->load->database();
}
public function get_categoryads()
{
$this->db->select('ads.id AS id, ad_image.image AS image, 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,
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');
$this->db->where("adcat.id='2' AND ads.subcatid!='843' AND scam=0 AND adactive=1 AND ads.userid=ad_image.user AND
ad_image.picorder=1 AND ad_image.aproved =1 AND ads.subcatid !='843' AND ads.subcatid !='841'");
$this->db->group_by("ads.userid");
$this->db->order_by('addate','desc');
$this->db->limit(10);
return $query->row_array();
}
}
The Categories.php controller looks like this:
defined('BASEPATH') OR exit('No direct script access allowed');
class Categories extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('categorylist_model');
}
public function index()
{
$this->load->helper('url');
$data = array('sitename'=>'CategoryAds', 'page_title' => 'Categories');
$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 view()
{
$data['categories'] = $this->categorieslist_model->get_categoryads();
}
}
My view has the following:
<?php foreach ($categories as $ad_item): ?>
<?php echo $ad_item['text'] ?>
<?php endforeach ?>
I am getting the following error when viewing the page.
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: categories
Filename: views/content_categories.php
Line Number: 114
Backtrace:
File: /usr/www/XXXXXXX/ci/application/views/content_categories.php
Line: 114
Function: _error_handler
File: /usr/www/XXXXXXX/ci/application/controllers/Categories.php
Line: 18
Function: view
File: /usr/www/XXXXXXX/ci/index.php
Line: 292
Function: require_once
source to share
Your controller will be as follows:
public function index() {
$this->load->helper('url');
$data['metadata'] = array('sitename'=>'CategoryAds', 'page_title' => 'Categories');
//directly
$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('content_categories', $data);
$this->load->view('templates/footer');
}
/** No need I think
public function view() {
$data['categories'] = $this->categorieslist_model->get_categoryads();
} **/
Your submission will be - content_categories.php
<?php foreach ($categories as $ad_item): ?>
<?php echo $ad_item['text'] ?>
<?php endforeach ?>
My workflow with Codeigniter looks like this:
Cat_model.php in the models directory
public function get_category() {
// active records code with return
}
CatCon.php in controller directory
function __construct() {
parent::__construct();
$this->load->model('cat_model');
}
function index() {
$data['categories'] = $this->cat_model->get_category();
// pass the data variable to view
$this->load->view('content_categories', $data);
}
content_categories.php in the view directory
print_r($categories);
source to share