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
Related
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
I want to pass multiple ID's to my controller.First for Categories and second for Food items as show in function show
My Controller is:
public function index()
{
$Foods=Food::all();
$Category=Categories::all();
return view('index.welcome', compact('Foods','Category'));
}
public function show($Food_id,$Category_id)
{
$food = Food::with('restaurant','categories')->findOrFail($Food_id);
$category = Categories::with('food')->findOrFail($Category_id);
return view('index.show', compact('food','category'));
}
My Routes are:
Route::get('index','DetailsController#index');
Route::get('index/{Food_id?}', 'DetailsController#show');
But it returns me error "Missing argument 2 for App\Http\Controllers\Detailscontroller::show()".Where is the problem in this?
You are calling a method which has 2 arguments, but in the route file you're only specifying one parameter.
Maybe your route should look something like this:
Route::get('index/{Food_id?}/{Category_id}', 'DetailsController#show');
Then you would be passing both variables.
Or modify your method declaration and remove the $Category_id parameter, as you are not passing it to the method, like this:
public function show($Food_id)
I have mysterious issue with kohana framework.
I create session variable in controller function:
public function action_authorise()
{
session_start();
$_SESSION["user"] = "superAdmin";
}
Later in the same controller's another function I try to access this season:
public function action_getSession()
{
$this->template->test = $_SESSION["user"];
$this->template->content = View::factory('admin/main');
}
The problem is that when I call $test variable in admin/main view it returns empty string, but if I call implicitily $_SESSION["user"] in admin/main view, it returns "superAdmin" as it should.
Can anyone see mistake while calling session variable in controller? Thanks
The problem here is that you're passing the variable test to the view template and it needs to be passed to the view admin/main. You can do this a couple of ways, pick whichever one you like best:
// Create the view object
$partial_view = View::factory('admin/main');
// Assign the session value to the partial view's scope as `test`
$partial_view->test = $_SESSION["user"];
// Assign the partial view to the main template's scope as `content`
$this->template->content = $partial_view;
Shortcut syntax:
$this->template->content = View::factory('admin/main', array(
'test' => $_SESSION['user'],
));
You passing test variable to template view, but trying to access it it admin/main view. There is no test variable in admin/main view. These are different views. Each one has its own variables.
You should set test to admin/main view like:
public function action_getSession()
{
$this->template->content = View::factory('admin/main')
->set('test', $_SESSION["user"]);
}
Also there is very usefull Session class in Kohana. It takes care of session business within framework.
Take a look at user guide.
I have made a helpers file in my /app folder which contains the following:
$constants = DB::table('constants')->get();
foreach ($constants as $constant) {
$C[$constant->type] = $constant->value;
}
echo $C['business_name'];
This works, but if I try
echo $C['business_name'];
In one of my views I get an error of $C undefined. I have added the helpers file to my start/global file and I know it works...
What steps should I take to use this variable in my views?
You need to pass data directly into the view via the second parameter of View::make or alternatively View::make('someBlade')->with(data);
So in your case it might be something like:
View::make('someBlade', $C);
If you really, really want globals, you can do this for views:
View::share('c', $C);
http://laravel.com/docs/4.2/responses
I think you need to create a function in helper and call that function in view.
it will automatically display value of this variable but you need change "echo" replacing with "return" in function last line.
function xyz()
{
$constants = DB::table('constants')->get();
foreach ($constants as $constant) {
$C[$constant->type] = $constant->value;
}
return $C['business_name'];
}
Call this function in your view like this. {{xyz()}}
if you are returning array {? $abc=xyz(); ?} make blade filter not echoing value pass this function to array variable and show like this {{$abc['business_name']}}
When loading one view in to another with jquery i do that.
In file view 5
load("<?php echo site_url("controller/name")?>");
I'm loading the name function that contains the view.But how to get the variables from that name function that is loading a view in to that view ?
function name(){
$a['aa'] = 1;
$this->load->view("file",$a);
}
And i wan to load the $aa variable where i'm loading the function with jquery , this is the loaded function - name, the default view is different and with different function in the controller.
Why don't you pass the $aa through the controller like this?
function name($aa = 1) {
$this->load->view('file', $aa);
}
Then you can do load('<?php echo site_url('controller/name/1') ?>');
You might want to sanitize $aa as well.