Codeigniter variables - codeigniter

I'm a codeigniter beginner. I'm using version 2.0.2 with a local server (php 5.3) and I have a start controller with code like this (just for testing obviously):
<?php
class Start extends CI_Controller
{
var $base;
function _construct()
{
parent::_construct();
$this->base = $this->config->item('base_url');
}
function hello()
{
$data['base'] = $this->base;
print_r ($data);
}
}
When I navigate to the hello function the $data['base'] array element is empty. Why should that be when the construct function has populated it with 'base_url' from the config file?
Seems that the variable $base is not available outside the construct function but I can't understand why or how to fix. Can anyone advise please?

Your constructor should be __construct() (2 underscores).
function __construct()
{
parent::__construct();
$this->base = $this->config->item('base_url');
}
Also, as other people have mentioned, if you load the 'url_helper', you can get the base_url by calling base_url().
$this->load->helper('url');
$this->base = base_url();

Did you know you can do
$this->load->helper('url');
$base = base_url();
Or even in the view:
<?php echo base_url(); ?>

Use it like this:
class Start extends CI_Controller
{
private $base = '';
function _construct()
{
parent::_construct();
$this->base = $this->config->item('base_url');
}
function hello()
{
$data['base'] = $this->base;
print_r ($data);
}
}
Or in the autoload.php configure:
$autoload['helper'] = array('url');
and then you can use base_url(); everywhere in your code.

Related

CodeIgniter 3.1.9 - Call to undefined method stdClass::base_url()

My controller code is :
class Tconfig extends CI_Controller {
public $config;
public function __construct($schoolId = 1) {
parent::__construct();
$this->load->database();
$this->load->model('Tconfig_model');
$this->config = $this->Tconfig_model->load_config($schoolId);
}
public function config() {
$data['config'] = $this->config;
$this->load->view('templates/user_header', $data);
$this->load->view('templates/user_menu', $data);
// printf("%s", base_url());
//$this->load->view('config', $data);
//$this->load_view('templates/user_footer', $data);
}
}
I have autoloaded url_helper
At this point, I am just trying to load a HTML file which includes a call to base_url()
The exact line where this function is called is :
<link href=<?php echo base_url();?>"assets/bootstrap337/css/bootstrap.min.css" rel="stylesheet">
When I run this code through a debugger, I get this
What I cannot figure out is why I get a call to an undefined method stdClass::base_url() in url_helper.php
I am pretty sure that it's something very obvious, but it's been a long day .. TIA!
You have to load url helper in application->Config->autoload.php file
Like this:-
$autoload['helper'] = array('url');
OR
Add this in construct function in controller
$this->load->helper('url');
The problem is - you overwrite the CI intern Variable config with your own - you can avoid this by renaming your variable
something like the following should work
class Tconfig extends CI_Controller
{
public $tcConfig;
public function __construct($schoolId = 1)
{
parent::__construct();
$this->load->database();
$this->load->model('Tconfig_model');
$this->tcConfig = $this->Tconfig_model->load_config($schoolId);
}
public function config()
{
$data['config'] = $this->tcConfig;
$this->load->view('templates/user_header', $data);
$this->load->view('templates/user_menu', $data);
}
}
use this in your header file.
<script>
var base_url = '<?php echo base_url(); ?>';
</script>
check whether you defined your base url in your config.php file. If not, define it like-
$config['base_url'] = 'your-base-url';
in your config.php file in application/config folder.

Codeigniter 1.7.2 and PHP 5.3.1, Data not passing to view from controller

I have an app using CI 1.7.2.
In controller i have $data['title'] = 'Sample Title';
In view
<?php echo $title; ?>
Should print it.
But not working.
I have another app with latest CI, in that passing like this works fine.
Anybody know reason for this strange issue?
You should pass the data to view like,
$this->load->view('view_file', $data);
There is another way to pass data to view use the library parser
class home extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->helper(array('form', 'url', 'date'));
$this->load->library('form_validation');
$this->load->library('session');
$this->load->library('parser');
}
public function index() {
$this->sssssssss();
}
function sssssssss($data = '') {
$header = $this->parser->parse('interface', array(), TRUE);
$data['interface'] = $header;
//bind all together and parse to template view
$this->parser->parse('template_view', $data);
}
}

Understanding codeigniter base controller structure

The following is a working example of how my Codeigniter website currently functions:
Model:
<?php
class Default_model extends CI_Model
{
function __construct()
{
// Call the Model constructor
parent::__construct();
}
function get_link()
{
$query = $this->db->query('SELECT * FROM links LIMIT 5');
return $query->result();
}
Controller:
<?php
class Home extends CI_Controller {
public function index()
{
$this->load->model('segment1/Page_model');
$data['link'] = $this->Page_model->get_link();
$this->load->view('page_view', $data);
}
}
View:
<h2>Link</h2>
<ul>
<?php if (isset($link)):?>
<?php foreach ($link as $row):?>
<li><?=$row->link?></li>
<?php endforeach;?>
<?php endif;?>
</ul>
I want to begin using a base controller for the above example, and while I've followed a few online examples - I can't quite get it right, and I'd appreciate some guidance...
I autoload the Model, no problem
The View file remains
I alter the config.php file
Controller:
<?php
class Home extends Main_Controller {
public function index()
{
$this->load->model('segment1/Page_model');
$this->load->view('page_view', $data);
}
}
MY_Controller
<?php
class MY_Controller extends CI_Controller
{
function __construct()
{
parent::__construct();
}
}
Now, here's where I get stuck - I can't quite figure out exactly what goes in the Main_Controller, and how it's structured...
Main_Controller:
<?php
class Main_Controller extends MY_Controller
{
function __construct()
{
parent::__construct();
//
// WHAT GOES IN HERE?
// SERIOUSLY, HELP!
//
}
}
Clearly, there's one big line of data missing from the original controller...
$data['link'] = $this->Page_model->get_link();
How does it all tie up?
Not exactly sure if I understand your question correctly, but if you want to avoid repeating this line:
$data['link'] = $this->Page_model->get_link();
What you can do is to put that in the constructor and create a public variable where you can store it.
i.e.
Main_Controller:
<?php
class Main_Controller extends MY_Controller
{
public $link;
function __construct()
{
parent::__construct();
$this->load->model('segment1/Page_model');
$this->link = $this->Page_model->get_link();
}
}
Controller:
<?php
class Home extends Main_Controller {
public function index()
{
$this->load->view('page_view', array('link' => $this->link));
}
public function another_page()
{
// you can keep using the value assigned to link in other
// methods without having to call Page_model->get_link() everytime
$this->load->view('page_view', array('link' => $this->link));
}
}

MY_Controller extend cant show data ( Codeigniter )

I have changed my application core/MY_Controller.php extendable. Before I could get data from mysql but Now I get Message: Undefined variable: records. How can i get data again ?
MY_Controller.php :
class MY_Controller extends CI_Controller {
protected $data = array();
function __construct() {
parent::__construct();
}
function render_page($view) {
//do this to don't repeat in all controllers...
$this->load->view('templates/header', $this->data);
//menu_data must contain the structure of the menu...
//you can populate it from database or helper
$this->load->view($view, $this->data);
$this->load->view('templates/footer', $this->data);
}
My home controller :
class Home extends MY_Controller {
function __construct() {
parent::__construct();
}
public function view($page = 'home')
{
$this->load->helper('text');
$this->data['records']= $this->services_model->getAll();
if ( ! file_exists('application/views/pages/'.$page.'.php'))
{
// Whoops, we don't have a page for that!
show_404();
}
$data['title'] = ucfirst($page); // Capitalize the first letter
$this->render_page('pages/'.$page,$this->data);
}
Services_model :
class Services_model extends CI_Model {
function getAll() {
$q = $this->db->get('services');
if($q->num_rows() > 0){
foreach ($q->result() as $row)
{
$data[] = $row;
}
return $data;
}
}
This is the where i get error in home.php view :
<ul class="blog-medium">
<?php foreach($records->result() as $row): ?>
<li><h1><?php echo $row->title; ?></h1></li>
<?php endforeach ?>
The error message shows : Message: Undefined variable: records
In your controller it looks like you are setting $data['records'] when you should be setting $this->data['records']
I found a solution. In my Home controller, I was using the view as :
$this->render_page('pages/'.$page,$this->data);
But I have to use it as here :
class Home extends MY_Controller {
function __construct() {
parent::__construct();
}
public function view($page = 'home')
{
$this->load->helper('text');
$this->data['records']= $this->services_model->getAll();
if ( ! file_exists('application/views/pages/'.$page.'.php'))
{
// Whoops, we don't have a page for that!
show_404();
}
$data['title'] = ucfirst($page); // Capitalize the first letter
$this->render_page('pages/'.$page,$data);
}
I dont know why but I tried this before. It did not make but it is working now.

passing data in model trough controller in codeigniter

I've heard about codeigniter couple of times so i was curious and thought why not. I took some tutorials and was very delighted to see how the framework worked.
Now i've the following problem i want to passing the data i've made in my model trough my controller and showing this in my view but i always run to the folowing error: Fatal error: Call to a member function query() on a non-object in C:\wamp\www\codeigniterTest\application\models\leden_model.php on line 9. The funny thing about this error is, when i google on this issue a lot of forum topics is about this issue but nowhere i get the right answer. my code looks like this.
codegniter version 2.03
class Leden extends CI_Controller {
function __construct(){
parent::__construct();
}
function index()
{
$this->load->model('leden_model');
$ledenModel = new Leden_model();
$data = $ledenModel->allLeden();
$this->load->view('leden_overzicht',$data);
}
}
<?php
class Leden_model extends CI_Model {
function __construct(){
parent::__construct();
}
function allLeden(){
$query = $this->db->query("SELECT * FROM leden");
foreach ($query->result_array() as $row)
{
echo $row['Naam'];
echo $row['Achternaam'];
echo $row['Email'];
}
return $query;
}
}
?>
When i'm doing the query in my controller then i'm getting the results i want, why not in my model?
my question is what am i doing wrong?
Leden_model.php
?php
class Leden_model extends CI_Model {
function __construct(){
parent::__construct();
}
function allLeden()
{
$data = array();
$this->db->select();
$query = $this->db->get('leden');
if ($query->num_rows() > 0)
{
foreach ($query->result_array() as $row)
{
$data[] = $row;
}
}
$query->free_result();
return $data;
}
Leden_controller.php
?php
class Leden extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->model('leden_model');
}
function index() {
$data['leden_data'] = $this->ledenModel->allLeden();
$this->load->view('leden_overzicht',$data); }
}
leden_overzicht.php
?php
if (count($leden_data))
{
foreach ($leden_data as $key => $list)
{
echo $list['Naam'] . " " . $list['Achternaam'] . " " . $list['Email'] . "";
} }
else {
echo "No data."; }
did you load database? example:
$this->load->database();

Resources