pass data from controller to view inside another view in codeigniter - php

I fetched data from model in controller . i want to display this data in view inside another view. its showing blank page.
here is my code..
controller -
public function Listblog()
{
$listblog=$this->Login->listblog();
$listblogwithpage=$this->load->view('list_blog',$listblog);
$this->load->view('Welcome_message',$listblogwithpage);
}
model -
public function listblog()
{
$query=$this->db->get('new_employee');
return $query->result();
}

To assign a view to a variable the 3rd param must be true:
$listblogwithpage=$this->load->view('list_blog',$listblog, true);
Further the 2nd param must be an array. E.g. $data['listblog'] = 123;
$var = $this->load->view('somepage', $data, true);
This applies to any usage of view.

If you want to pass data from a controller to the first view and then have the second view pass data to the second view, you should do the following, always remembering that CI expects data passed to a view to be in form of an array. Take this and feel free to adapt it to suit your needs
In controller:
// populate an array and pass it to the first view
$first_view_data = array(
'listblog' => $listblog_query_result,
);
$this->load->view('firstview', $first_view_data);
In the first view, populate a new array with whatever data you need and call the second view from within the first one, passing the second data array:
$second_view_data = array(
'second_data_var' => $variable,
'other_data_var' => $other_var,
);
$this->load->view('second_view', $second_view_data);
CI is intelligent enough to let you call a view from within a view and pass data from each to the next in this way. Just remember, it has to be an array.
Using the data:
In the first view you'd call $listblog
In the second view, you would access $second_data_var and $other_data_var
$listblog $second_data_var and $other_data_var each could be single variables, arrays, objects and mostly anything as long as they are passed to the view as elements of an array

try this way.
//Controller
function Listblog() {
$data = array();
$data['listblog']=$this->Login->listblog();
$this->load->view(''list_blog',$listblog');
}
in view page you have to call that data array $listblog

Related

Codeigniter view returned as a string with data

I'm using Codeigniter 3 and im trying to pull in a view to a variable, and pass data to the view for inclusion. But the view is not recognising the data being passed and is telling me the variable doesn't exist.
This is how I'm calling the view:
$view = $this->load->view('notifications/' . $report_type, $data ,true);
And then in the view I'm trying to loop through $data and display as appropriate, like so:
foreach($data as $item){
// echo stuffs
}
I know $data definately contains data as I've var_dump'ed it.
Can I do it like this?
You need to pass an associative array to the loader.
$view = $this->load->view('notifications/' . $report_type, array('data' =>$data) ,true);
Now the variable will be visible at the view.
if model Page_m and method western return a specific value which you need,
$this->data['western'] =$this->Page_m->western();
$this->load->view('notifications/' . $report_type,$this->data);
in your view
foreach($western as $item){
// echo stuffs
}

very confused with passing data array from controller to view in laravel or calling it from the view

ok I have my route to my controller (for future crud maybe angular use)
public function Dashboard_Clicks()
{
$DBClicks = DB::table('TotalClicks')->select('Total_Clicks')->get();
return view::('dashboard.pages')->with('$DBClicks', Total_Clicks);
do I use view composer? or another clean simple way to call this in?
}
this has one result a number 45454
I want to be able to get this result in my view like so.
<h1 class="clicks"><strong>{{ $DBClicks->Total_Clicks }} </strong> </h1>
You can try this: (Laravel 5)
return view('dashboard.pages', ['Total_Clicks' => $DBClicks]);
As far as I can recall there are two ways of passing data from controller to views first is compacting the variable in your controller and it will available as it is in your view. An example for the same is following:
$variable = 'somedata';
$array = ['some' => 'data'];
return view('viewName', compact('variable', 'array'));
//now in your view you can access {{$variable}} and #foreach($array as $something)
Second is with :
return view('viewName')->with(['first_var' => $variable, 'first_array' => $array])
//now in your view you can access {{$first_var}} and #foreach($first_array as $array)

SImple Issue on loading data in CodeIgniter

Hi Here is my Loader and Index Function inside my Controller
While calling the index() function I am assigning the $menu['menu'] and $menu['menu'] at the same time i am the value for $data and sending it to the loader function.
In the Loader function
I am calling the header (which has css,js files)
I am calling the view index and sending the value $data into it
I am calling the footer
But in the index view even i didn't send the value $menu, i am able to print the $menu and $title but i can't able to print the $data.
What is the mistake i am doing. How can i get the value of $data inside the index view
Here is my Code :
public function loader($url,$menu,$data)
{
$this->load->view('assets/header',$menu);
$this->load->view($url,$menu,$data);
$this->load->view('assets/footer');
}
public function index()
{
$menu['menu']="home";
$menu['title']="Home Page";
$data='somedata';
$this->loader('index',$menu,$data);
}
When you pass value at view you should pass it as array and the array key will be received as variable at view.In your case you need to replace the line $data='somedata' with;.
$menu['data']='somedata';
You will receive it as $data inside view
You also need to rewrite the line $this->load->view($url,$menu,$data);
like this
$this->load->view($url,$menu);
3rd parameter of load->view function is either true or false;
you can see documentaion

CakePHP: How do I render multiple elements with parameters from a controller?

I'm working with CakePHP 1.3. I'm making an AJAX call where I want the server to return some HTML that corresponds to several of rows of results that I can throw inside a <div>. I have the template for a single row stored as an element called 'library_track'. The element requires variables 'id', 'artist' and 'name' to be passed to it. I would like my function getResults() to return a block of HTML that is composed of HTML from several elements.
Can anyone show me what this code would look like please?
Just as usual, you'll probably have to loop through the results in your view:
Controller:
function getResults() {
// here be dragons
$this->set(compact('results'));
}
View /foo/getresults.ctp:
foreach ($results as $result) {
echo $this->element('library_track', array('id' => $result['Result']['id'], ...));
}

CodeIgniter get_where

I’m attempting to use get_where to grab a list of all database records where the owner is equal to the logged in user.
This is my function in my controller;
function files()
{
$owner = $this->auth->get_user();
$this->db->get_where('files', array('owner =' => '$owner'))->result();
}
And in my view I have the following;
<?php foreach($query->result() as $row): ?>
<span><?=$row->name?></span>
<?php endforeach; ?>
When I try accessing the view, I get the error :
Fatal error: Call to a member function result() on a non-object in /views/account/files.php on line 1.
Wondered if anyone had any ideas of what might be up with this?
Thanks
CodeIgniter is a framework based on MVC principles. As a result, you would usually separate application logic, data abstraction and "output" into their respective areas for CodeIgniter use. In this case: controllers, models and views.
Just for reference, you should usually have you "data" code as a model function, in this case the get_where functionality. I highly suggest you read through the provided User Guide to get to grips with CodeIgniter, it should hold your hand through most steps. See: Table of Contents (top right).
TL;DR
To solve your problem you need to make sure that you pass controller variables through to your view:
function files()
{
$owner = $this->auth->get_user();
$data['files'] = $this->db->get_where('files', array('owner =' => '$owner'))->result();
$this->load->view('name_of_my_view', $data);
}
And then make sure to use the correct variable in your view:
<?php foreach($files as $row): ?>
<span><?=$row['name']; ?></span>
<?php endforeach; ?>
<?php foreach($query->result() as $row): ?>
<span><?=$row->name?></span>
<?php endforeach; ?>
Remove the result function like so.
<?php foreach($query as $row): ?>
<span><?=$row->name?></span>
<?php endforeach; ?>
Btw. It's a much better idea to test the query for a result before you return it.
function files()
{
$owner = $this->auth->get_user();
$query = $this->db->get_where('files', array('owner =' => $owner))->result();
if ($query->num_rows() > 0)
{
return $query->result();
}
return FALSE;
}
public function get_records(){
return $this->db->get_where('table_name', array('column_name' => value))->result();
}
This is how you can return data from database using get_where() method.
All querying should be performed in the Model.
Processing logic in the View should be kept to an absolute minimum. If you need to use some basic looping or conditionals, okay, but nearly all data preparation should be done before the View.
By single quoting your $owner variable, you convert it to a literal string -- in other words, it is rendered as a dollar sign followed by five letters which is certainly not what you want.
The default comparison of codeigniter's where methods is =, so you don't need to declare the equals sign.
I don't know which Auth library you are using, so I'll go out on a limb and assume that get_user() returns an object -- of which you wish to access the id of the current user. This will require ->id chained to the end of the method call to access the id property.
Now, let's re-script your MVC architecture.
The story starts in the controller. You aren't passing any data in, so its duties are:
Load the model (if it isn't already loaded)
Call the model method and pass the owner id as a parameter.
Load the view and pass the model's returned result set as a parameter.
*Notice that there is no querying and no displaying of content.
Controller: (no single-use variables)
public function files() {
$this->load->model('Files_model');
$this->load->view(
'user_files',
['files' => $this->Files_model->Files($this->auth->get_user()->id)]
);
}
Alternatively, you can write your controller with single-use variables if you prefer the declarative benefits / readability.
public function files() {
$this->load->model('Files_model');
$userId = $this->auth->get_user()->id;
$data['files'] = $this->Files_model->Files($userId);
$this->load->view('user_files', $data);
}
Model: (parameters are passed-in, result sets are returned)
public function Files($userId) {
return $this->db->get_where('files', ['owner' => $userId])->result();
}
In the above snippet, the generated query will be:
SELECT * FROM files WHERE owner = $userId
The result set (assuming the query suits the db table schema) will be an empty array if no qualifying results or an indexed array of objects. Either way, the return value will be an array.
In the final step, the view will receive the populated result set as $files (the variable is named by the associative first-level key that was declared in the view loading method).
View:
<?php
foreach ($files as $file) {
echo "<span>{$file->name}</span>";
}
The { and } are not essential, I just prefer it for readability in my IDE.
To sum it all up, the data flows like this:
Controller -> Model -> Controller -> View
Only the model does database interactions.
Only the view prints to screen.

Categories