[eluser]sb05[/eluser]
Hi Guys,
This is almost the last thing I need and then I'm done for my web application for school. I'm still new at PHP and Codeigniter so bear with me.
I have a view called
left_column_view.php where I display ads. The ad basically consists of a header with the ad name, the ad image and a link once you click the image. So basically 3 variables $ads->name, $ads->image_url and $ads_link. See below.
Code:
<h1><?php echo $ads->name; ?></h1>
<p><?php echo $ads->image_url; ?></p>
<p><?php echo $ads->link; ?></p>
This works, but the problem is it only works on one page, where I call the function view($ad_id) of my
ads.php Controller
But at my index function, my add, update, delete and tons of other functions, instead of the ad, I get
undefined variabe: ads
Code:
<?php
class Ads extends Controller {
// num of records per page
private $limit = 10;
function Ads(){
parent::Controller();
// load library
$this->load->library(array('table','validation'));
// load helper
$this->load->helper('url');
// load model
$this->load->model('adModel','',TRUE);
}
function index($offset = 0){
// offset
$uri_segment = 3;
$offset = $this->uri->segment($uri_segment);
// load data
$ads = $this->adModel->get_paged_list($this->limit, $offset)->result();
// generate pagination
$this->load->library('pagination');
$config['base_url'] = site_url('ads');
$config['total_rows'] = $this->adModel->count_all();
$config['per_page'] = $this->limit;
$config['uri_segment'] = $uri_segment;
$this->pagination->initialize($config);
$data['pagination'] = $this->pagination->create_links();
// generate table data
$this->load->library('table');
$this->table->set_empty(" ");
$this->table->set_heading('ID','Name', 'Image_url','Link', 'Client Name');
$i = 0 + $offset;
foreach ($ads as $ad){
$this->table->add_row(++$i, $ad->name, $ad->image_url, $ad->link, $ad->client_name,
anchor('ads/view/'.$ad->ad_id,'view',array('class'=>'view')).' '.
anchor('ads/update/'.$ad->ad_id,'update',array('class'=>'update')).' '.
anchor('ads/delete/'.$ad->ad_id,'delete',array('class'=>'delete','onclick'=>"return confirm('Are you sure want to delete this News ad?')"))
);
}
$data['table'] = $this->table->generate();
$this->load->vars($data);
// load view
$this->load->view('banner_view', $data);
$this->load->view('left_column_view', $data);
$this->load->view('right_column_view', $data);
$this->load->view('adList', $data);
$this->load->view('footer_view', $data);
}
function view($ad_id){
// set common properties
$data['title'] = 'ad Details';
$data['link_back'] = anchor('ads','Back to list of News ads',array('class'=>'back'));
// get ad details
$data['ads'] = $this->adModel->get_by_id($ad_id)->row();
// load view
$this->load->view('banner_view', $data);
$this->load->view('left_column_view', $data);
$this->load->view('right_column_view', $data);
$this->load->view('adView', $data);
$this->load->view('footer_view', $data); }
I think that this line gets the value from the variable I need
$data['ads'] = $this->adModel->get_by_id($ad_id)->row();
However when I paste this line in my other functions I still get the same error that ads is an undefined variable.
Also I prefer not to do this way cuz it's highly inefficient cuz I have so many controllers with so many functions.
So how can the variables in my left_column_view be displayed in all my other pages? (By the way all controllers load the left_column_view.php)
Just in case here is my adModel.php
Code:
<?php
class AdModel extends Model {
private $ads= 'ads';
var $upload_data ;
function ad(){
parent::Model();
}
function get_by_id($ad_id){
$this->db->where('ad_id', $ad_id);
return $this->db->get($this->ads);
}