I am passing an array from controller to view in PHP CodeIgniter.
Here is my code in controller.
$gen is any array containing many values.
foreach ($gen as $value) {
$moviesbyid['similarmovie'] = $this->main_model->getsimilarmovies($value);
}
$this->load->view('home/check.php', $moviesbyid);
But the above code fill $moviesbyid['similarmovie'] array only for one value of $value.
I want that it contain all the values returned from getsimilarmovies($value) for every value of $value.
How can i do this?
Here is the method in model:
public function getsimilarmovies($gener)
{
$this->db->limit(4);
$this->db->like('Genre',$gener);
$query = $this->db->get('sources');
return $query->result();
}
You need to create new items in the array as it loops.
Your code just overwrite the same item every iteration.
foreach ($gen as $value) {
$moviesbyid['similarmovie'][]=$this->main_model-
>getsimilarmovies($value);
}
notice the []
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.
$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.
I have a function in my model that get all company details I am fetch this
I have to add a new key city_name in my return result() how can I add ?? I am very confused but I am not get any useable example.
function xxxx($search=array(),$page,$limit){
$this->db->select("*");
$this->db->from("xxx");
$this->db->join("xx","xx.xx=xxx.xx");
$this->db->limit($limit,$page);
$company_data = $this->db->get();
if($company_data->num_rows()){
return $company_data->result();
}
else{
return 0;
}
}
Change the following line:
return $company_data->result(); // It return Stc Class Object
to
return $company_data->result_array(); // Now it returns an array
Now you can use array_push() function, it inserts one or more elements to the end of an array.
Or simply use:
$company_data['index'] = value;
Keep your model same return $company_data->result().
In your controller, you need to iterate it for the number of results you get and add new key to your objects.(result() method returns Standard Class Object).
function yourControllerMethod()
{
$company_data = $this->your_model->xxx(search_array, $page, $limit);
if(!empty($company_data)
{
foreach($company_data as $cd)
{
$cd->city_name = "pune"; // or anything you want
}
}
var_dump($company_data);
}
Try this to add new key to your results.
Edit:
You asked that you don't have city_name key in your object.
Yes, you don't have. The above code adds a key in your object.
Just like array.
$temp = array();
$temp["id"] = 1;
In this code, initially the array is empty, the next statement add the id as key in the array.
In laravel the get() method over the DB returns an array of stdObjs,
for example this one:
$designers = DB::table('designers')->get();
Will return all designers from the designers table as an array of stdObj.
Now I want to manually create an array of stdObjs using something like this:
public function index()
{
$id = Auth::id();
$user = User::find($id);
foreach ($user->owned_item as $i) {
$result = new \stdClass();
$result->type => $i->Item_type->type,
$result->color => $i->Item_color->color
}
obviously that code will overwrite the $result obj at every cycle, the question is, how do I create an array of all the stdobjs I need?
You need to assign them to an array before the loop execution ends for each item.
$objs = [];
foreach (...) {
$obj = new \stdClass();
...
$objs[] = $obj;
}
After the loop the $objs variable contains an array containing the objects created inside the foreach loop.
EDIT: if you want to set specific array keys for each object, you can use
$objs[$key_here] = $obj;
when inserting the objects to the array. $key_here should be a string or an integer.