How to call a model from view - php

Hi i am trying to call a model from view but i am getting error as.
A PHP Error was encountered Severity: Notice Message: Undefined property:
CI_Loader::$Stopsmap_Model Filename: stopsmap/index.php Line Number: 59
i tryed this below code in view.But no luck
foreach ($routes as $route_details):
//calling model
//$trip_max_min_sequence_details = $this->load->Stopsmap_Model->fullTripdetails($trips->trip_id); i tried this also but same error
//$this->load->model('Stopsmap_Model');
//$data = $this->stopsmap_model->fullTripdetails($route_details->route_id); i tried this also but same error
$this->load->model('fixedtransit_model/Stopsmap_Model');
$get_route_related_trips = $this->Stopsmap_Model->getRoutetrips($route_details->route_id);
if(count($get_route_related_trips)>=1):
echo '1 stop';
endif;
endforeach;
thanks in advance for the help

In your controller , you can have like this:
function routes(){
//call model
$this->load->model('fixedtransit_model/Stopsmap_Model');
$routes = $this->topsmap_model->get_routes();
foreach ($routes as $route_details){
$get_route_related_trips[] = $this->Stopsmap_Model->getRoutetrips($route_details->route_id);
}
$data['related_routes'] = $get_route_related_trips;
//load view and pass data
$this->load->view('myview' , $data);
}
and in your view:
<?php foreach($related_routes){
//do something
}
?>

on my end
i have created a model named accountmodel in applications/models dir
and load in view like (would be best if you load model in your controller):-
$this->load->model('AccountModel','Account');
//AccountModel (model class name)
//Account (object) you can further use this model using object
for more info :- http://ellislab.com/codeigniter/user-guide/general/models.html

Related

$home is undefined when trying to fetch data

I am trying to fetch data from my admin_homes table, here is what my controller looks like:
public function index()
{
$home = AdminHome::all();
return view('admin.home.index', compact('home'));
}
I use foreach ($home as $homes) in my blade view but when I run it, I get an error saying "$home is undefined". How to solve this?

Undefined property: update_user::$update_model - codeigniter

I am a noob at both PHP and codeigniter, so please be gentle.
I was messing around with this file and got this error.
Message: Undefined property: update_user::$update_model
Filename: controllers/update_user.php
Fatal error: Call to a member function update_user_id() on null in C:.....update_user.php on line 20
here is my code
<?php
class update_user extends CI_Controller{
function __construct(){
parent::__construct();
$this->load->model('user_model');
}
function show_user_id() {
$id = $this->uri->segment(3);
$data['users'] = $this->update_model->show_users();
$data['single_user'] = $this->update_model->show_user_id($id);
$this->load->view('update_view', $data);
}
function update_user_id() {
$id= $this->input->post('did');
$data = array(
'userName' => $this->input->post('dname'),
'userPass' => $this->input->post('dpass'),
);
$this->update_model->update_user_id($id,$data);
$this->show_user_id();
}
}
this is line 20
$this->update_model->update_user_id($id,$data);
anybody can give any pointers to what is wrong?
thanks in advance
This line:
$this->update_model->update_user_id($id,$data);
Should be:
$this->user_model->update_user_id($id,$data);
Also, every other place where you have used update_model, it should be changed to user_model.
This is assuming that you have only a user_model and no update_model. That's what your code shows.
The error is indicating the problem clearly. you are using update_model model which you haven't loaded in your update_user Controller. If this is it than load the update_model like this $this->load->model('update_model'); else change the code:
$this->update_model->update_user_id($id,$data);
to
$this->user_model->update_user_id($id,$data);
Also please share your model code if there is any issue.

Codeigniter undefined property on core/Model.php

Controller
public function editItem(){
$this->load->helper('form');
$this->load->model('ItemModel');
$data['items'] = $this->ItemModel->itemlist();
$item_details = $this->ItemModel->edititem($this->input->get('id'));
$data2['item_name'] = $item_details->name; //THIS IS LINE 28
$data2['item_description']= $item_details->description;
$data2['item_price'] = $item_details->price;
$this->load->view('item/item_edit',$data2);
}
There's an error to my view, and I don't know why
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Item::$name
Filename: core/Model.php
Line Number: 77
Backtrace:
File: C:\xampp\htdocs\itwa213\application\controllers\Item.php
Line: 28
Function: __get
File: C:\xampp\htdocs\itwa213\index.php
Line: 292
Function: require_once
I already checked my autoload on config and it's properly configured with
$autoload['libraries'] = array('database');
You could try this
public function editItem() {
$this->load->helper('form');
$this->load->model('ItemModel');
$data['items'] = $this->ItemModel->itemlist();
$item_details = $this->ItemModel->edititem($this->input->get('id'));
$data2['item_name'] = $item_details['name']; //THIS IS LINE 28
$data2['item_description'] = $item_details['description'];
$data2['item_price'] = $item_details['price'];
$this->load->view('item/item_edit',$data2);
}
Your controller function change to like this
public function editItem(){
$this->load->helper('form');
$this->load->model('ItemModel');
$data['items'] = $this->ItemModel->itemlist();
$data2['item_details'] =$this->ItemModel->edititem($this->input->get('id'));
$this->load->view('item/item_edit',$data2);
}
and in your view page
echo $item_details->name;
You should look at your code for the get function you use. try to replace the $this->input->get('id') to existing id number first. check whether it success or not. If success by using existed id number, so it should your get function not working or something wrong. i'm seldom use $this->input->get() but $this->input->post().
on your edit link, you can use:
edit.
so the id is the third segment uri. use $this->uri->segment(3); to get the third segment and store into $item_id variable into your controller. so, no need to use $this->input->get(); to get the item_id value.

rows from db in header, which set to display in all page

In CodeIgniter I'm try to made header with code from DB, my controller code:
public function index()
{
$this->load->model('main_model');
$data['result'] = $this->main_model->get_tipsters();
$this->load->view('template/header_view',$data);
}
And header_view:
<?php foreach($result->result() as $row): ?>
<div id="tipster"><img src="<?=$row->photo;?>" /><br /><?=$row->name;?></div>
<?php endforeach; ?>
Header work's only It self view file, but not In others pages.
I Including header like this in controllers:
$this->load->view('template/header_view');
$this->load->view("/bets/index",$data);
$this->load->view('template/footer_view');
Getting this error
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: result
Filename: template/header_view.php
Line Number: 14
Fatal error: Call to a member function result() on a non-object in /home/user/domains/test.com/public_html/application/views/template/header_view.php on line 14
Line 14 is foreach, I have copied early.
Since your header view file is always expecting $result, you'll need to provide it for all your controller methods:
$this->load->model('main_model');
$data['result'] = $this->main_model->get_tipsters();
$data['main'] = $this->main_model->get_main_data(); //example
$this->load->view('template/header_view',$data);
$this->load->view("bets/index",$data);
$this->load->view('template/footer_view');
This can become cumbersome, so consider creating a MY_Controller file that extends CI_Controller - more about that here.
You can make a function in your Common_model
for fecthing result on your header file.
and directly get result from that function by calling it in a view file.
common_model.php
function your_function()
{
// code for fetching data for header
/// return your result here
}
call directly this function in a view file as
$rs = $this->Common_model->your_function();
Note: common_model is load by default .if disable then you need to load model in a view file.
Best way to load CI object on view element file(header_view) and load and call model method with CI object
$CI = & get_instance()
$CI->load->model('main_model');
$result = $CI->main_model->get_tipsters();
<?php foreach($result->result() as $row): ?>
<div id="tipster"><img src="<?=$row->photo;?>" /><br /><?=$row->name;?></div>
<?php endforeach; ?>
and remove code from controller

Asset Library not loading correctly

I am trying to implement the CI Asset manager found here. After I placed the files in the correct locations and then tried to call the assets in my main view I get the following error
A PHP Error was encountered
Severity: Notice
Message: Undefined property: CI_Loader::$assets
Filename: index/index.php
Line Number: 18
What am I forgetting to do that is causing this error?
Line 16-18 are
$this->load->library("Assets");
echo $this->assets->load("ie10mobile.css", "Content");
You have to load library and use it in controller file, not the view.
Sample controller function:
function index()
{
$this->load->library("assets");
$this->data['css'] = array(
$this->assets->load("ie10mobile.css", "Content"),
$this->assets->load("style.css", "Content"),
$this->assets->load("custom.css", "Content")
);
$this->load->view('index_view', $this->data);
}
Sample view file: index_view.php
foreach ($css as file) {
echo $file;
}

Categories