Variable passed but exception still thrown? - php

I create and pass a data array:
public function home()
{
$data['page'] = 'home';
$data['table'] = 'pageData';
$data['temp'] = 'temp_1';
$this->template($data);
}
public function template($data)
{
$this->load->model("model_get");
$data['results'] = $this->model_get->getData($data);
$this->load->view('template', $data);
}
This is the template view:
<?php
$this->load->view('header');
$this->load->view('nav', $data);
$data['results'] = $results;
$this->load->view($temp, $data);
$this->load->view('footer');
?>
It throws an exception for undefined variable at:
$this->load->view('nav', $data);
but still loads the view and completes all the if statements inside it and loads the view from the name stored in $temp.
Why does it throw the exception?

You haven't populated $data in that view to load a second view. Your code is too vaguely named to really understand what it's supposed to be doing but let's break it down.
public function home()
{
$data['page'] = 'home';
$data['table'] = 'pageData';
$data['temp'] = 'temp_1';
$this->template($data);
}
public function template($data)
{
$this->load->model("model_get");
$data['results'] = $this->model_get->getData($data);
//I assume getData takes a table name and returns all the data
//This only works because you're taking all the data passed from the first function
//in the function above.
$this->load->view('template', $data);
}
<?php
$this->load->view('header');
$this->load->view('nav', $data);
//At this point $data is empty. You have available $results, $page, $table and $temp
//because they were passed from the template function above.
$this->load->view('nav', $results);
//The line above is what it appears you need, rather than $data.
$data['results'] = $results;
//You've repopulated $data here but now all it contains is $results.
$this->load->view($temp, $data);
$this->load->view('footer');
?>
What you're misunderstanding is $data is not persistent, when you pass it to a view by loading a view with $data as the second parameter it breaks down into it's components, so $data itself is no longer available.

Related

Why doesn't the controller recognise the data passed on from the model?

I'm trying to get data from my DB using a model, to then be used in the view. I return the query results to my controller and it gives me the undefined variable notice.
I tried first tried performing a select(get) statement in the controller, I then defined the result array as row before defining the specific row to be used, to which I pass on to my view. That threw an error, then I tried the same thing but with a model and a return to the controller:
controller.php
public function Home()
{
$this->load->model('Main');
$this->Main->getresults();
$this->load->view('header', $data);
}
model.php
public function getresults() {
$query = $this->db->get('table');
foreach ($query->result_array() as $row) {
$data = array(
'column' => $row["column"]
);
}
return $data;
}
view.php
<?php echo $column; ?>
I expect the return of $data to the controller for it to be used in the view, yet it still throws a notice of an undefined variable.
In your controller you don't assign and send data to view.
Change your code there:
public function Home()
{
$this->load->model('Main');
$data = $this->Main->getresults();
$this->load->view('header', $data);
}

I can't get data and post it in my html

model
public function getPost(){
$this->db->select('post_title', 'post_auth', 'post_content');
$this->db->from('post');
$query = $this->db->get();
return $query->result_array();
}
controller
public function index(){
$this->load->model('swmodel');
$data['posts'] = $this->swmodel->getPost();
$this->load->view('dashBoard', $data);
}
How can I transfer data in my view?
Honestly I can't say you why, but for some reason in CodeIgniter 3 you have to store the result or return of your function in a variable and then that variable assign it within the array based value you pass to your view as $this->load->view('dashBoard', $data);.
You need to check does exist a view called dashBoard? and where is it?
So you can try with something like:
public function index() {
$this->load->model('posts_model');
// Get your posts, store them in $posts.
$posts = $this->posts_model->getPost();
// Assign your $posts to your $data.
$data['posts'] = $posts;
// Pass your $data when you load the view.
$this->load->view('dashBoard', $data);
}
And then in your view you can easily access to the passed data as $posts.
# dashBoard.php
<?php var_dump($posts); ?>

Trying to get property of non-object in view

I have a link ending with index.php/welcome/item/1
I have my controller welcome.php with the function item
public function item($id)
{
$data = array();
$this->load->helper('url');
$data['title'] = "item";
$this->load->view('header', $data);
$data['item']=$this->port_model->get_item($id);
$this->load->view('item',$data);
$this->load->view('footer');
}
and my model with the get_item function
public function get_item($id)
{
$this->db->select('*');
$this->db->from('portfolio');
$this->db->where('id', $id);
$query = $this->db->get();
return $item = $query->result();
}
In my item view i want to echo info out of the database. Why doesn't it do that now
<h1>item</h1>
<?php echo $item->naam; ?>
Error i get on the page:
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: views/item.php
Line Number: 2
The page gets loaded normal, no problems.
What am i doing wrong?
Since you're expecting one single result, your get_item function should
return $query->row();
Replace in your get_item function this :
result()
This function returns the query result as an array of objects, or an
empty array on failure.
return $item = $query->result();
Like this :
row()
This function returns a single result row. If your query has more than
one row, it returns only the first row. The result is returned as an
object.
return $item = $query->row();
Documentation
You need to call the library database ( $this->load->database(); ) and the model ($this->load->model('port_model');) :
Result:
<code>public function item($id)
{
$data = array();
$this->load->helper('url');
$data['title'] = "item";
$this->load->view('header', $data);
$this->load->database();
$this->load->model('port_model');
$data['item']=$this->port_model->get_item($id);
$this->load->view('item',$data);
$this->load->view('footer');
}</code>

How do I display active record in codeigniter with multiple controllers?

I am building my comment module and trying to get the active records to display on view. Seems simple however I have a lot going on and using a separate model and controller then what the view is being generated from.
So I have a profile page ( where I'm trying to get the results to display )
Controller:
public function profile()
{
$this->load->helper('url'); //Include this line
$this->load->helper('date');
$this->load->library('session');
$this->load->library('form_validation');
$session_id = $this->session->userdata('id'); //Ensure that this session is valid
//TRYING TO MAKE A VARIABLE WITHT THE $_GET VALUE
$user_get = $this->uri->segment(3); // Modify this line
// LOAD THE CORRECT USER
$this->load->model('account_model');
$user = $this->account_model->user($user_get); //(suggestion) you want to pass the id here to filter your record
$data['user'] = $user;
$data['session_id'] = $session_id;
if($user_get != $user['id'] || !$user_get)
{
$data['main_content'] = 'account/notfound';
$this->load->view('includes/templates/profile_template', $data);
}
else
{
if($user_get == $session_id) //Modify this line also
{
$data['profile_icon'] = 'edit';
}
else
{
$data['profile_icon'] = 'profile';
}
$sharpie = $this->sharpie($user);
$data['sharpie'] = $sharpie;
$data['main_content'] = 'account/profile';
$this->load->view('includes/templates/profile_template', $data);
}
}
Now I have a new controller for my comments I'm trying to display:
public function airwave() {
$this->load->helper('date');
$this->load->library('session');
$airwave_get = $this->uri->segment(3);
$this->load->model('community_model');
$airwave = $this->coummunity_model->airwave($airwave_get);
$data['airwave'] = $airwave;
$data['main_content'] = 'account/profile';
$this->load->view('includes/templates/main_page_template', $data);
}
with this model:
public function airwave($id=null)
{
if(is_null($id)) {
$session = $this->session->userdata('is_logged_in');
$commenter_id = $this->session->userdata('id');
}else{
$commenter_id = intval($id);
}
$query = $this->db->query("SELECT * FROM airwaves_comments WHERE from_id=$commenter_id");
if($query->num_rows()==1)
{
$data = $query->result_array();
return $data[0];
//above returns the single row you need to the controller as an array.
//to the $data['user'] variable.
}
}
and trying to display it on this view ( which is generated by the profile function )
<div class="profile_airwave_comment_text">
<?php echo $airwave['comment'];?>
</div>
I just can't seem to get it to pass the array variable I've created properly to that view?
thanks in advance.
The first glaring thing I see wrong is in you model's airwave function:
if($query->num_rows()==1)
{
$data = $query->result_array();
return $data[0];
You are assuming that there will only be one result in your query, so if there are more or less than one, your $data array will bever be set.
Also, even then you are only returning the first element in the array. I am not seeing anywhere in your question that you want to return only one.
Finally, in your view, if you now return the full array, and not only one element, you will have to do a foreach statement.
EDIT: please see suggested solution.
In your model, don't worry about the check for the amount of data in the result array. This will come later. Simply do:
return $query->result_array();
Now, keep your controller as is, but adapt your view:
<div class="profile_airwave_comment_text">
<?php
if (isset($airwave) && is_array($airwave) && !empty($airwave))
{
foreach ($airwave as $wave)
{
echo $wave['comment'];
}
}
else
{
echo 'No comments found...';
}
?>
</div>
This should work, and if not, at least let you know that no comments were fond, and thus check your database.
HTH!
If your using HMVC, simply call the module from inside the profile view, ie:
echo Modules::run('comments/profileComments', $user->id);
Else:
You would need to create a new method in the loader class, to load in the controller seperate from routing.
If your really after a quick fix until you come up with a better alternative, you could(depending if both controllers live in the same directory) just include the 'comments' controller in your 'profile' controller and try something like this:
{Cons: CI Pagination wont work!}
application/controllers/profile.php
include 'Comments' . EXT;
class Profile extends CI_Controller{
public function __construct ()
{
parent::__construct () ;
}
public function index( $user_id ){
$commentsPartialView = call_user_func_array(array(new Comments(), "getProfileComments"), array($user_id));
return $this->load->view($this->template, array(
'view' => 'profiles/_index',
'comments' => $commentsPartialView
));
}
}
-
application/controllers/Comments.php
class Comments extends CI_Controller{
public function __construct ()
{
parent::__construct () ;
}
public function getProfileComments( $user_id ){
$user_comments = ''; //pull from DB
//set third param to TRUE for partial view, without main template
return $this->load->view('comments/show', array(
'user_comments' => $user_comments
), TRUE);
}
}

Pass all array elements to view from controller in codeigniter

I want to pass an array to a view from a controller. I tried to use the code below. I know it is wrong, but cannot think of what to use. find() function gets all rows from table, and then i want to pass those rows as array to the view. How can i do so?
<?php
class Blog extends CI_Controller{
public function __construct(){
parent::__construct();
$this->load->model('blog_model');
}
public function index(){
$data = $this->blog_model->find(); //which gets all entries from table
$this->load->view('template/header');
$this->load->view('template/content', $data);
$this->load->view('template/footer');
}
public function create(){
$this->blog_model->create();
}
public function delete(){
}
}
?>
$data = $this->blog_model->find(); //which gets all entries from table
$this->load->view('template/header');
$this->load->view('template/content', $data);
$this->load->view('template/footer');
Should be:
$data = array('myvar' => $this->blog_model->find());
$this->load->view('template/header');
$this->load->view('template/content', $data);
$this->load->view('template/footer');
Then access it in your view with:
$myvar
Please see this for a thorough explanation.

Categories