I am following the tutorial and i have stuck with this problem.
I get this error :
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: subview
Filename: admin/_layout_main.php
Line Number: 44
In tutorials controller it shows the code below about the subview
public function index() {
$this->data['users'] = $this->user_m->get();
$this->data['subview'] = 'admin/user/index';
$this->load->view('admin/_layout_main', $data);
}
The problem is when trying to use the data users and subviews into a view. As you can see subviews has a location of another view file, and call subviews in the main view like this from the tutorial:
<div class="span9">
<?php $this->load->view($subview); ?>
</div>
Plan B:
Controller:
public function index() {
$this->data['users'] = $this->user_m->get();
$this->data['subview'] = $this->load->view('admin/user/index');
$this->load->view('admin/_layout_main', $data);
}
View:
<?php echo $subview; ?>
I'm grasping at straws here. I use the Stencil template system for CI. So much easier.
Try as follows in controller
public function index() {
$this->load->model('user_m');
$data['users'] = $this->user_m->get();
$data['subview'] = 'admin/user/index';
$this->load->view('admin/_layout_main', $data);
}
In view file .
<div class="span9">
<?php echo $subview ; ?>
</div>
Try this code in your view:
<div class="span9">
<?php echo $subview; ?>
</div>
controller
public function index() {
$data['users'] = $this->user_m->get();
$data['subview'] = 'admin/user/index';
$this->load->view('admin/_layout_main', $data);
}
view
<?php echo $subview; ?>
Related
I am a beginner in the CodeIgniter. Working on one small basic project, while I am working on the List view I get an error " Trying to get property of non-object.
Please help me!
Here the screen shots.
Error
My code
Here is my view :
<ul id="actions">
<h4>List Actions</h4>
<li> Add Task</li>
<li> Edit List</li>
<li><a onclick="return confirm('Are you sure?')" href="<?php echo base_url();?>lists/delete/<?php echo $list_detail->id;?>"> Delete List</a></li></ul>
<h1><?php echo $list_detail->list_name; ?></h1>
Created on : <strong><?php echo date("n-j-Y",strtotime($list_detail->create_date)); ?></strong>
<br/><br/>
<div style="max-width:500px;"><?php echo $list_detail->list_body; ?></div>
<!-- <?php //echo print_r($query); ?>
-->
<br/><br/>
Here is Controller
public function show($id){
$data['list_detail'] = $this->List_model->get_lists($id);
$data['main_content']='lists/show';
$this->load->view('layout/main',$data);
}
Here is model
public function get_list($id){
$query=$this->db->get('lists');
$this->db->where('id', $id);
return $query->row();
}
This error is generating because you are trying to access an array in class object style.
Ex:
$data['list_details'] = 'some value';
and accessing it like:
$data->list_details; // it will generate the error "Trying to get property of non object"
After viewing your code. I think you have written wrong in your model function
Your model function must be like below
public function get_list($id) {
$this->db->where('id',$id);
$query = $this->db->get('lists');
return $query->row();
}
Also in your view before you print the value please check it with the condition of !empty($list_details). so if value is not there still it will not throw any error.
I hope this will help you.
From your controller you are calling the model function as get_lists().
But there is no function as get_lists in model. U need to change the function name in controller to get_list()
When your using $query->row() on your model function then on controller
Model Function
public function get_list($id) {
$this->db->where('id',$id);
$query = $this->db->get('lists');
return $query->row();
}
Controller
public function show($id){
// Gets data where it belongs with that id
$list_data = $this->model_name->get_list($id);
$data['id'] = $list_data->id;
$data['create_date'] = $list_data->create_date;
$data['list_body'] = $list_data->list_body;
$this->load->view('someview', $data);
}
When your using $query->row_array(); on your model function then on controller
public function get_list($id) {
$this->db->where('id',$id);
$query = $this->db->get('lists');
return $query->row_array();
}
Controller
public function show($id) {
// Gets data where it belongs with that id
$list_data = $this->model_name->get_list($id);
$data['id'] = $list_data['id'];
$data['create_date'] = $list_data['create_date'];
$data['list_body'] = $list_data['list_body'];
$this->load->view('someview', $data);
}
On view then you can access
<?php echo $id;?>
<?php echo $create_date;?>
<?php echo $list_body;?>
Update for multiple data
public function get_list() {
$query = $this->db->get('lists');
return $query->result();
}
Controller
$results = $this->model_name->get_list();
// Or
//$results[] = $this->model_name->get_list();
$data['list_detail'] = $results;
$this->load->view('someview', $data);
View
<?php foreach ($list_detail as $list) {?>
<?php echo $list->id;?>
<?php echo $list->create_date;?>
<?php echo $list->list_body;?>
<?php }?>
I'm working on a view that shows me the data from the database.
but the result is
Severity: Notice
Message: Undefined variable: data
Filename: mentee/index.php
UserModel.php
<?php
class UserModel extends CI_Model{
public function __construct(){
parent::__construct();
}
public function userProfile($nim){
$this->db->where('userNIM',$nim);
$query= $this->db->get('msuser');
return $query->result();
}
}
MainController.php
public function indexMentee()
{
$this->load->model('UserModel');
$userID=$this->session->userdata("userNIM");
$data['data'] = $this->UserModel->userProfile($userID);
$this->load->view('mentee/index', array('data' => $data));
}
View
<?php
die(var_dump($data));
if(is_array($data)){
foreach ($data as $profile):
?>
<div class="header-content">
<div class="header-content-inner">
<h1>Learning Center</h1>
<hr>
<h3> Welcome,<?php echo $profile->userName ?></h3>
</div>
</div>
<?php
endforeach;
}
?>
Please help me..Thanks a lot!
Instead of passing array('data' => $data)); just pass $data to the view.
First print session data. If session data present then pass data to view page
$data['details'] = $this->UserModel->userProfile($userID);
$this->load->view('mentee/index', $data);
And print print_r($details); on your view page
Right im new to this and a little out of my depth. Ok so I'm trying to bring in some data and im getting some errors and am I a little confused as to whats going on.
here is the model code:
public function get_webstore_sets($limit, $offset) {
$this->db->select("*");
$this->db->from('st_posts');
$this->db->where("type", "webstore");
$this->db->limit($limit, $offset);
$this->db->order_by('created', 'DESC');
$query = $this->db->get();
$data = $query->result_array();
return$data;
}
public function count_webstore_sets() {
$this->db->select("*");
$this->db->from('st_posts');
$this->db->where("type", "webstore");
$this->db->order_by('created', 'DESC');
$query = $this->db->get();
$count = $query->num_rows();
return $count;
}
and here is what im trying in the controller:
class Webstore extends MY_Controller {
public function __construct() {
parent::__construct();
$this->load->model("post");
$this->load->database();
$this->load->helper("url");
}
public function index() {
$this->data['posts'] = $this->post->get_webstore_sets($limit=null, $offset=null); // calling Post model method getPosts()
$data = array('page_title' => 'Store', 'video_content');
$this->layout("pages/webstore", $data);
}
}
and then bringing it in on HTML:
<?php foreach ($posts as $webstore): ?>
<div class="col-md-12" style="height: 225px;">
<div class="col-md-3">
<?php
$webstore['images'] = explode(".", $webstore['images']);
?>
<img class="thumbnail" width="250px" src="<?php echo $video['images'][1]; ?>.gif"/>
</div>
<div class="col-md-5">
<h3><?php echo $webstore['title']; ?></h3>
</div>
<div class="col-md-4 text-center">
Date Added: <?php echo date('d-m-y', strtotime($webstore['created'])) ?>
<hr>
Select or Exclusive to www.Borderlandbound.com<br> Please Visit Our Clips Store For <br>Many More Ultra-Hot Videos Like This
<hr>
</div>
</div>
<hr>
<?php endforeach; ?>
I know the connection is ok as the site is bringing in data from else where.
and this is the error im getting :
Severity: Notice
Message: Undefined variable: posts
Filename: pages/webstore.php
Line Number: 39
A PHP Error was encountered
Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: pages/webstore.php
Line Number: 39
I have blanked out the file path just in case ;) Edited for the above errors
In this code:
public function index()
{
$this->data['posts'] = $this->post->get_webstore_sets($limit=null, $offset=null); // calling Post model method getPosts()
$data = array('page_title' => 'Store', 'video_content');
$this->layout("pages/webstore", $data);
}
you are passing $data array to view, but not $this->data one. Also, 'video_content' element of $data array should have it's key to let you to approach to it in view. Or it is key itself missing value?
Change it to:
public function index() {
$this->data['posts'] = $this->post->get_webstore_sets($limit=null, $offset=null); // calling Post model method getPosts()
$this->data['page_title'] = 'Store';
$this->layout("pages/webstore", $this->data);
}
Now, you should be able to get $posts variable in view file. Just check what you have to do with value/key 'video_content'.
You are calling get_webstore_sets() without a parameter, so either fill in some parameters (limit, offset) or define the function as follows:
public function get_webstore_sets($limit=null, $offset=null) ...
I would suggest giving the parameter though in the function index() of class Webstore.
I'm working on the second half of the CodeIgniter beginners tutorial which can be found at http://www.codeigniter.com/userguide3/tutorial/news_section.html and like many others I can't get it to work properly.
The post index works fine but every time I click on "view article" I just get a 404 message.
I've looked over a number of posts about this and tried a number of solutions that were offered but so far nothing has worked which is driving me slightly crazy considering I copied and pasted the code from the tutorial to make sure I didn't copy it wrong by mistake.
My code is as follows:
Controller:
class News extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('news_model');
$this->load->helper('url');
}
public function index()
{
$data['news'] = $this->news_model->get_news();
$data['title'] = 'News archive';
$this->load->view('templates/header', $data);
$this->load->view('news/index', $data);
$this->load->view('templates/footer');}
public function view($slug = NULL)
{
$data['news'] = $this->news_model->get_news($slug);
if (empty($data['news']))
{
show_404();
}
$data['title'] = $data['news']['title'];
$this->load->view('templates/header', $data);
$this->load->view('news/view', $data);
$this->load->view('templates/footer');
}
}
Model:
class News_model extends CI_Model {
public function __construct()
{
$this->load->database();
}
public function get_news($slug = FALSE){
if ($slug === FALSE)
{
$query = $this->db->get('news');
return $query->result_array();
}
$query = $this->db->get_where('news', array('slug' => $slug));
return $query->row_array();}}
View:
view.php
echo '<h2>'.$news['title'].'</h2>';
echo $news['text'];
index.php
<h2><?php echo $title ?></h2>
<?php foreach ($news as $news): ?>
<h3><?php echo $news['title'] ?></h3>
<div class="main">
<?php echo $news['text'] ?>
</div>
<p>View article</p>
<?php endforeach ?>
routes.php
$route['news/create'] = 'news/create';
$route['news/(:any)'] = 'news/view/$1';
$route['news'] = 'news';
$route['(:any)'] = 'pages/view/$1';
$route['default_controller'] = 'pages/view';
Thanks in advance!
Did you managed to work it out?
What is the href in the anchor provides you with? I believe you should use the site_url() method to fix it, so the anchor should be:
<p>View article</p>
I swapped the
I am trying to access all the bands in my table and print them out in a list, but when I run it I get this error:
Severity: Notice
Message: Undefined property: CI_Loader::$model_bands
Filename: views/band_view.php
Line Number: 16
band_view.php:
<h3>List of Bands:</h3>
<?php
$bands = $this->model_bands->getAllBands();
echo $bands;
?>
model_bands.php:
function getAllBands() {
$query = $this->db->query('SELECT band_name FROM bands');
return $query->result();
}
Could someone pleease tell me why it is doing this?
Why do you need to do this, the correct way is to use the model methods inside a controller, then passing it to the view:
public function controller_name()
{
$data = array();
$this->load->model('Model_bands'); // load the model
$bands = $this->model_bands->getAllBands(); // use the method
$data['bands'] = $bands; // put it inside a parent array
$this->load->view('view_name', $data); // load the gathered data into the view
}
And then use $bands (loop) in the view.
<h3>List of Bands:</h3>
<?php foreach($bands as $band): ?>
<p><?php echo $band->band_name; ?></p><br/>
<?php endforeach; ?>
Did you load your model in the Controller?
$this->load->model("model_bands");
You need to change your code like
Controller
public function AllBrands()
{
$data = array();
$this->load->model('model_bands'); // load the model
$bands = $this->model_bands->getAllBands(); // use the method
$data['bands'] = $bands; // put it inside a parent array
$this->load->view('band_view', $data); // load the gathered data into the view
}
Then View
<h3>List of Bands:</h3>
<?php foreach($bands as $band){ ?>
<p><?php echo $band->band_name; ?></p><br/>
<?php } ?>
Your Model is ok
function getAllBands() {
$query = $this->db->query('SELECT band_name FROM bands');
return $query->result();
}
You forgot to load the model on your controller:
//controller
function __construct()
{
$this->load->model('model_bands'); // load the model
}
BTW, why are you calling the model directly from your view? Should it be:
//model
$bands = $this->model_bands->getAllBands();
$this->load->view('band_view', array('bands' => $bands));
//view
<h3>List of Bands:</h3>
<?php echo $bands;?>