$this->loadModel('Siteconfig');
$data=$this->Siteconfig->find('all');
foreach($data as $data)
{
$email=$data['Siteconfig']['email'];
$companyname=$data['Siteconfig']['companyname'];
$cfa=$data['Siteconfig']['cfa'];
}
Above is Controller
How to get cfa data in view using CAKEPHP?
In your controller method, you can use this
$this->set('var_name_in_view', $cfa); //You will find the variable $cfa as $var_name_in_view in your view(You have to use $var_name_in_view in view)
You can also send multiple variables to your view at once
$this->set(compact('cfa', 'another_variable')); //You can acceess using $cfa and $another_variable
Compact makes an array keeping the variable name as key and variable value as the value for the array key.
Modify your script to
$this->loadModel('Siteconfig');
$data=$this->Siteconfig->find('all');
$cfa = '';
foreach($data as $datum)
{
$email=$datum['Siteconfig']['email'];
$companyname=$datum['Siteconfig']['companyname'];
$cfa=$datum['Siteconfig']['cfa'];
}
$this->set('view_cfa',$cfa);
This will fix it. Your foreach was reassigning $data.
Related
I'm fetching data from database, inside foreach loop i have to add one array index list_array
$list = Lists::where('name',$request->name)->get();
$Data=[];
foreach($list as $key => $list_row)
{
$list_row['list_array'][]=$list_row['data_1'];
$list_row['list_array'][]=$list_row['data_2'];
$Data[]=$list_row;
}
It should be of array type but when i declare it as array it is not working,
message: "Indirect modification of overloaded property
App\Models\Lists
Any solution to create list_array as an array index inside foreach loop. Thanks
You are not dealing with an array, you are dealing with an App/Models/Lists model. What you probably instead want to do is adding a custom attribute to the model. This happens one step before.
// your model
class Lists
{
protected $appends = ['list_array'];
public function getListArrayAttribute()
{
$data = [
// your array data from where ever
];
return $data;
}
}
You then can access (without adding it when you want to access the data) the new attribute from within the model, like so:
// where you access your data
$lists = Lists::where('name', 'whatever')->get();
foreach($lists as $list) {
dd($lists->list_array)
}
You can read more about the so called Accessors here:
https://laravel.com/docs/8.x/eloquent-mutators#defining-an-accessor (Laravel 8)
https://laravel.com/docs/9.x/eloquent-mutators#defining-an-accessor (Laravel 9)
I hope I got the intention of your question right. From your question it's not very clear what data you want to add and where it is coming from. There might be other and better ways too.
Edit:
Based on the comment from #user3653474 the Accessor function could look like this.
public function getListArrayAttribute()
{
$data = [
$this->data_1,
$this->data_2,
];
return $data;
}
data_1 and data_2 are the column names in the table of the same model.
I have a method:
public function winnerDetails()
{
$winners = DB::table('winners')->get();
//dd($winners);
foreach ($winners as $winner) {
$qrMainId = $winner->qrdetails_id;
}
dd($qrMainId);
}
dd($winners); returns to value of array but when i use foreach its returns one value. How can i get who value returns with foreach loop?
dd($winners) output:
and dd($qrMainId); return one value 44. But it should return another value of array 35; Thanks in advance.
To get array of ID's use
foreach ($winners as $winner) {
$qrMainId[]= $winner->qrdetails_id;
For just value use
$qrMainId='';
foreach ($winners as $winner) {
$qrMainId.= $winner->qrdetails_id;
If you want to accomplish this task in a Laravel way use the pluck method to map the array for the key that you want
<?php
public function winnerDetails()
{
$winnersId = DB::table('winners')->get()->pluck('qrdetails_id');
dd($winnersId);
}
I think you are not leveraging the power of Laravel here. Obviously, I don't know what you are trying to do but here are some pointers.
You should probably be making a model for the winners table now models in Laravel return Collections as does the DB facade you are using now. Now the Collection class contains alot of usefull helpers like pluck
At this point it becomes as easy as Winner::all()->pluck('id') and you got yourself an array of id's.
And if you would like to get them comma seperated or anything like that you can use the implode
And you would get Winner::all()->implode('id',',');
$qrMainId is a variable not an array.
It is modified in the foreach during every loop.
So your code has always the last element of the array.
Use an array to collect values like
$qrMainId[] = $winner->qrdetails_id;
or sql select directly the field you want.
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
}
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']}}
I consider myself as a php beginner, so it may be possible that this question is too easy for someone, but I got really confused on how to solve it. I am trying to loop something from the database in my views. So, in a quick way I solved it like this:
I've created a function in my model that does the loop and in the same time is creating the html and saves it in a variable. Then, I get that variable from my controller and I pass it in my view. But, it seems that this is not a good way to solve it, since if I want to change my html I need to enter my model function instead some of the view files.
Then, I've created another function in my model that looks like this:
function displayUsers() {
$sql = $this->pdo->prepare('select * from user');
$sql->execute();
while($row = $sql->fetch())
$results[] = $row;
return $results;
}
Now... I take the result in my controller, and send it in the view, but then... I don't know how to extract the results from my variable. I have done something like this:
while($output) {
foreach($output[$i] as $key => $value)
$data[$key] = $value;
echo $data['email'];
$i++;
}
But then, in the end it says to me undefined offset, which means I am referring to an array key that doesn't exist. Can anyone help me on how to solve this issue?
Proper MVC shouldn't have any output in the model or the controller.
Ideally you would have a model that just gets the raw data and returns it in the controller. The controller can then build up an array of values that we'll call data. For example:
Controller
$data['users'] = $this->MyModel->getusers(); // Getting the users from your model
$data['posts'] = $this->MyModel->getposts(); // Getting the posts from your model
$this->getTemplate('userdisplay', $data); // Get the template userdisplay and pass data
This gets the data from the model, and then assigns it to a key within the "data" variable. You can then pass the data variable into the template. You'll then have two variables to work with in the template, $users and $posts.
You'll need a "getTemplate" function that properly maps the data array to individual variables for use in the template, but all of the display should be located in the template.
To answer your specific question at the end, something like this should work in the template:
if (count($users) > 0) {
foreach ($users as $person) {
echo $person['email'];
}
}
You should be able to do this:
foreach($output as $row) {
echo $row['email'];
}