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
Related
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.
I'm trying to load photos from the database via a for each loop to display each photo in a formatted div. however i keep receiving a undefined variable results and i am unsure why
this is my view code
<?php echo form_open(base_url('index.php/smite/getimages/1/1'));?>
<?php foreach($imgres as $imgdata){?>
<img class="img-box" src="<?php echo base_url("assets/img/".$imgdata['name'].".".$imgdata['type']);?>">
<p style="font-size:140%"><?php echo $imgdata['description']?></p>
<?php }?>
<?php echo form_close();?>
this is the controller code
class Smite extends CI_Controller {
private $data = "";
private $session_data = array();
public function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
$this->load->library(array('form_validation', 'session'));
$this->load->model(array('users_model','comments_model','images_model'));
}
public function getimages($g1,$g2){
$this->data['imgres'] = $this->images_model->get_images($g1,$g2);
$this->load_page("smite");
}
furthermore in the controller is a function to load the page view and send it data
private function load_page($page)
{
$this->data['page'] = $page;
$this->load->view('template/head');
$this->load->view('template/nav',$this->data);
$this->load->view($page."_view",$this->data);
$this->load->view('template/footer');
$this->load->view('template/login_register_modal');
$this->load->view('template/scripts',$this->data);
}
the query runs fine and returns results when tested on the mysql server but im not sure why its not returning results to the view. these are the sever error messages
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: imgres
Filename: views/smite_view.php
Line Number: 28
followed by
A PHP Error was encountered
Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: views/smite_view.php
Line Number: 28
change your controller like this
public function getimages($g1,$g2){
$data['imgres'] = $this->images_model->get_images($g1,$g2);
$this->load->view("smite",$data);
}
Then In get_images fuction in images_model write the query to get images data from database
This is my previous question: How to convert a decimal $attribute['text']; into a fraction in opencart
I have a helper function that is declared in startup.php and defined in helper/dec2frac.php
I am trying to call the helper function from a category.tpl file with this code:
<?php if ($product['attribute_groups']) { ?>
<?php foreach ($product['attribute_groups'] as $attribute_group) { ?>
<?php foreach ($attribute_group['attribute'] as $attribute) { ?>
<?php /*var_dump($attribute);*/
if($attribute['name'] == "Adjuster Position")
{
//echo("<h1>HELLLO</h1>");
dec2frac($attribute['text']);
}
?>
...but I am getting this error message:
Fatal error: Call to undefined function dec2frac() in startup.php
helper/dec2frac.php
How can I call my helper function in the category.tpl file?
Do I need to reference the helper function in my category.php file?
I am trying to achieve same but in a different way. This following link will help, it will work with opencart v2.3x
Not able to find my custom object in Registry - OpenCart-v2.3.0.2
Try the above link (it is my posted question). Create a object and save it in registry and get it from registry and call the required function.
in your category.php file, get your object from registry. For eg:
$kt = $registry->get('ktLibrary'); //Object
$value = $kt->getSomeValue(); //function
object $kt will be available throughout the category.tpl file.
Trying to crate objects dynamically for a plug in system (work in progress)
heres my code using $this->module->load_module('test'); to use the method that creates the dynamic objects. Following code is the function that loads the class's and makes use of an auto loader, i have checked that its getting the correct file etc.
<?php
class BaseModule {
function __construct() {
}
function load_module($module){
echo 'Module = '.$module.'<br />';
$object_name = $module . "Controller";
$this->$$module = new $object_name();
}
}
Here is a test module that it would load when invoking $this->module->load_module('test'); and it creates the object outputting the test strings via echo statements. Heres the code for the test module that was constructed. Which should cause no problems as there is not really a solution but just an output string, but posted any way.
<?php
class testController {
function __construct() {
echo 'test controller from modules <br />';
}
}
However when running the page i am getting some errors can any one help out?
Notice: Undefined variable: test in
/Applications/MAMP/htdocs/tealtique/application/modules/BaseModule.php on line 11
Fatal error: Cannot access empty property in
/Applications/MAMP/htdocs/tealtique/application/modules/BaseModule.php on line 11
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