I can't passing the variables in view page
this is my controller code
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Management extends CI_Controller {
public function __construct()
{
parent::__construct();
if($this->session->userdata('level') != 1){
redirect('');
}
}
public function hotels()
{
$this->load->model('model_hotels');
$ss['most_view_hotels'] = '23';
$this->load->view("management/header_mng_view");
$this->load->view('management/hotels_mng_view' , $ss , true);
$this->load->view("management/footer_mng_view");
}
}
this is the error
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: most_view_hotels
Filename: management/hotels_mng_view.php
Line Number: 22
hotels_mng_view.php
<?php
echo $most_view_hotels;
foreach($most_view_hotels as $value):
?>
<div class="row">
<div class="cell en">adsf</div>
<div class="cell en">1212</div>
<div class="cell en">12</div>
<div class="cell en">32</div>
</div>
<?php endforeach;?>
In your controller:
$ss['most_view_hotels'] = '23';
$this->load->view("management/header_mng_view");
$this->load->view('management/hotels_mng_view',$ss);
$this->load->view("management/footer_mng_view");
In your view:
<?php echo $most_view_hotels?>
In you're template you would refer to the item as...
$most_view_hotels
So we need to verify this is what you are doing in the view management/hotels_mng_view.php
As said on Codeigniter manual:
There is a third optional parameter lets you change the behavior of the function
so that it returns data as a string rather than sending it to your browser.
This can be useful if you want to process the data in some way. If you set
the parameter to true (boolean) it will return data.
The default behavior is false, which sends it to your browser.
What this means is that your line
$this->load->view('management/hotels_mng_view' , $ss , true);
actually returns result of that view as string to php instead of sending it to browser.
The answer to your actual question ( undefined variable ), relies in in management/hotels_mng_view.php, possibly a typo.
I think the problem is with third parameter, ie true.
As codeigniter documentation says:
There is a third optional parameter lets .....
Remember to assign it to a variable if you want the data returned
And in your code you are not assigning, returned data to any code.
I suggest you to try something like this:
public function hotels()
{
$this->load->model('model_hotels');
$data = array();
$ss['most_view_hotels'] = '23';
$data['header'] = $this->load->view("management/header_mng_view",,true);
$data['body'] = $this->load->view('management/hotels_mng_view' , $ss , true);
$data['footer'] = $this->load->view("management/footer_mng_view");
$this->load->view("template",$data);
}
/*template view file*/
<?php
echo $header;
echo $body;
echo $footer;
?>
Hope this helps.
Related
Please read the code below
Insertmodel.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Insertmodel extends CI_Model {
public function insertdata($data)
{
$this->db->insert('page',$data);
}
public function getpagedata()
{
//$this->db->select('pname,purl,pub');
//$this->db->from('page');
//$this->load->database();
$getquery = $this->db->get('page');
return $getquery->result();
}
}
Pagedatainsert.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Pagedatainsert extends CI_Controller {
public function insertdata()
{
$this->load->helper('date');
$this->load->model('insertmodel','tblselect');
$data = array(
'pname'=>$this->input->post('page-name'),
'purl'=>$this->input->post('page-url'),
'pcon'=>$this->input->post('page-content'),
'pub'=>date('Y-m-d H:i:s')
);
$this->tblselect->insertdata($data);
}
public function gpdatacon()
{
$this->load->model('insertmodel');
$data['query'] = $this->insertmodel->getpagedata();
$this->load->view('backend/pages/pages',$data);
}
}
pages.php (which is under directory backend/pages)
<?php foreach($data as $row){?>
<tr class="odd gradeX">
<td><?= $row->pname; ?></td>
<td><?= $row->purl; ?></td>
<td><?= $row->pub; ?></td>
</tr>
<?php } ?>
Here an error is occurred which says
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: data
Filename: pages/pages.php
Line Number: 73
Backtrace:
File: D:\Ampps\www\customcms\application\views\backend\pages\pages.php
Line: 73
Function: _error_handler
File: D:\Ampps\www\customcms\application\controllers\Backmanage.php
Line: 26
Function: view
File: D:\Ampps\www\customcms\index.php
Line: 316
Function: require_once
My database name is customcms, table name is page.
Made lots of searching on google already but not found so much. Besides, I found a question like this on stackoverflow but that was not so much helpful in my case. I am working for entire day on this problem but it is not getting resolved.
Change it:
$this->load->view('backend/pages/pages',$data);
to
$this->load->view('backend/pages/pages',array('data' => $data));
or change it:
<?php foreach($data as $row){?>
to
<?php foreach($query as $row){?>
Explanation: The second parameter of view() function is an array like
view('viewname', array('data' => $data))
and on view you can access it by using $data. If you pass $data then you have to use by its index i.e. $query
change $data to $query cause you store all data in $data['query'] so you get all data in $qyery variable
<?php foreach($data as $row){?>
.............
<?php } ?>
to
<?php foreach($query as $row){?>
............
<?php } ?>
if not work then print_r use and check that value exit
public function gpdatacon()
{
$this->load->model('insertmodel');
$data['query'] = $this->insertmodel->getpagedata();
print_r( $data['query']);
die()
$this->load->view('backend/pages/pages',$data);
}
for more information
https://www.codeigniter.com/user_guide/general/views.html
I tried it myself that was the issue with the URL I was calling. I made two pages similar in looks. and accidentally calling the wrong URL that was obvious not working.
I have this todo list project.
My model
class Model_lists extends CI_Model {
function Model_list(){
parent::__construct();
}
function list_get($id){
$this->load->database();
$query = $this->db->get_where('lists', array('id'=>$id));
return $query->row_array();
}
My Controller
public function view_list($id){
$this->load->model('model_lists');
$data["query"] = $this->model_lists->list_get($id);
$this->load->view('lists/view_list',$data);
}
List view
<?php
echo $query['list_by'];
?>
However, when I access the view I get 2 PHP errors
1) Message: Missing argument 1 for Lists::view_list()
2) Message: Undefined variable: id
This is how I call the list:
<a class="view_list" href="<?php echo site_url("lists/view_list?lid={$row->list_id}");?>"><i class="icon-eye"> </i></a>
Your site url is not correct. The error is because no id is being passed and in CI (unless you have set to use query arrays) then the url is of the format www.example.com/controller/method/argument
So try:
href="<?php echo site_url('lists/view_list/'.$row->list_id); ?>"
In the docs:
http://www.codeigniter.com/user_guide/helpers/url_helper.html#site_url
I've two tables on my database, monitor [pk = idMonitor] and monitor_data [pk = idMonitor_data].
Please click you can see the tables fields here. As you can see i put the array data in table monitor_data.
I want to Update the condition for every idinventory where monitor_data.idMonitor = $id.
But first i want to display the current data of 'monitordate','idinventory', and 'condition' from database to my view.
My controller
public function edit($id=0) {
$dataa = $this->monitor_m->get_record(array('monitor_data.idMonitor'=>$id),true);
$this->data->monitordate = $dataa->monitordate;
$this->data->condition = $dataa->condition; <-line 20
$this->data->detail = $this->monitor_m->get_record(array('monitor_data.idMonitor'=>$id),true);
$this->template->set_title('SMIB | Monitoring')
->render('monitor_edit',$this->data);
}
My View (monitor_edit)
<?php echo form_open(site_url("monitor/ubah"),'data-ajax="false"'); ?>
<?php foreach ($detail as $items): ?>
<h4><?php echo '[ '.$items['idinventory'].' ] '?> </h4>
<?php echo form_label ('Condition : ');
echo form_dropdown('condition', array('good'=>'Good','broke'=>'Broken','lost'=>'Lost'),#$items['condition']);
?>
<?php endforeach; ?>
<?php echo form_close(); ?>
My Model
class Monitor_m extends MY_Model {
public function __construct(){
parent::__construct();
parent::set_table('monitor','idMonitor');
}
public function get_record($id = 0,$get_user = FALSE) {
$this->db->where($id);
if ($get_user){
$this->db->join('monitor_data','monitor_data.idMonitor = monitor.idMonitor');
$this->db->join('inventory','inventory.idinventory = monitor_data.idinventory');
$this->db->join('user','user.id_user = monitor.id_user');
}
$data = parent::get_array();
return $this->improve_data($data);
}
Here is my problem : its work fine for monitordate code in my controller, BUT i keep getting an error for condition code
Maybe because i use 'monitor_data.idMonitor' as my parameter $id not idinventory. how can i use 2 parameters for example like where idMonitor=$id and idinventory=$idiventory.
Do i explain it right ?
Severity: Notice Message: Trying to get property of non-object
Filename: controllers/monitor.php Line Number: 20
Please Please help me, i dont know what is wrong with my controller :( i've searching the solution but none of those work. :(
It's weird if you can get monitordate but can't get condition.
Can you edit your controller to be like this?
public function get_record($id = 0,$get_user = FALSE) {
$this->db->where($id);
if ($get_user){
$this->db->join('monitor_data','monitor_data.idMonitor = monitor.idMonitor');
$this->db->join('inventory','inventory.idinventory = monitor_data.idinventory');
$this->db->join('user','user.id_user = monitor.id_user');
}
// $data = parent::get_array();
$data = $this->db->result_array();
print_r($data);
echo $this->db->last_query();
exit;
return $this->improve_data($data);
}
I am writing a program that basically does stuff with a database, I am having trouble with some of the functionality though, this is what it is supposed to do
public function clist() {
$this->load->model('list_model');
$fields = $this->list_model->listcli();
if ($fields === $this->list_model->listcli()) {
$fieldl = $fields;
$this->load->view('clientlist');
$this->clientlist->display_clients($fieldl);
}
}
This loads the model which looks like this:
public function listcli()
{
$this->db->list_fields('clients');
}
}
Then runs the model function listcli so that it will list all the fields in the clients database and puts the value into $fields I then call it fieldl and load a view that will show the data, the view looks like this:
<html>
<body>
<p> sup </p>
<?php
function display_clients($fieldl)
{
?>
<html>
<body>
<p> sup2 </p>
<ul>
<?php
foreach ($fieldl as $l) {
?>
<li>
<?php echo $l;?>
</li>
<?php
}
}
Then calls the function inside the view and passed the data from $fieldl into it.
but I am getting the error " Call to a member function display_clients() on a non-object in /codeigniter/src/application/controllers/clientlist.php on line 40"
line 40 is
$this->clientlist->display_clients($fieldl);
Can you help? Please and thank you.
(I know this kind of question has been asked before but they are always very specific to the code at hand so doesn't help me, I am really new to CodeIgniter so if you can keep any answer relatively simple I will be grateful).
I needed to use the second parameter possible to pass my data over into the view, not my own makeshift solution for it, CI accepts an object as a second parameter for the CI_Loader::view()
Why can't you prepare the list in the controller and pass it to view via $data array?
$my_list = '<ul>';
foreach ($fieldl as $l) {
$my_list .= '<li>'.$l.'</li>';
}
$my_list .= '</ul>';
$data['my_list'] = $my_list;
then you can access it in view as $my_list. Just echo it where you need it.
EDIT:
AS per your own answer, yes - exactly. Using example above you would pass the $data to view as the second parameter:
$this->load->view('my_view',$data);
Then in the View, you access $data['my_list'] as $my_list, $data['foo'] as $foo etc.
I have the foloowing controller (supernavigationloggedin):
<?php
class Supernavigationloggedin extends CI_Controller {
function index(){
#get current session id
$currentSessionID = $this->session->userdata('session_id');
#get all the account row for the given sessionID
$data['info'] = $this->db->get_where('Client', array('session_id'=>$currentSessionID))->row();
#views
$this->load->view('supernavigationloggedin',$data);
}
}
?>
and the following view named(supernavigationloggedin):
<div id="superNavigation">
<h5><strong>Welcome</strong>, <?php $info->fname; ?> Account Settings</h5>
<div class="clearL"> </div>
</div
>
It keeps throwing an error on line:<h5><strong>Welcome</strong>, <?php echo $info['fname']; ?> <a href="#">Account that Message: >>> Trying to get property of non-object
I've tried : <?php echo $info['fname']; ?> <?php echo $info->fname; ?> but neither seem to work.
It's because $info is empty and no database request are made. You have to do this this way if you want your query to return an object:
$data['info'] = $this->db->get_where('Client', array('session_id'=>$currentSessionID))->row();
Or this way, if you prefer to get it into an array:
$data['info'] = $this->db->get_where('Client', array('session_id'=>$currentSessionID))->row_array();
This way it should work. row() or row_array() is necessary to execute your query.