So I went over this thread to figure out how to send data from controller to a view here.
However I am trying to pass multiple values with identical titles to the view and I cannot seem to figure out how to do this and still access them later.
I have a for loop that grabs all the films a user has in their database, this information is then stored into a array called $data
$data = array();
foreach ($films as $film)
{
$title = $film->title;
$description = $film->description;
$url = $film->fileUrl;
$data = array_add($data, 'title', $title);
$data = array_add($data, 'description', $description);
$data = array_add($data, 'url', $url);
}
$this->layout->content = View::make('profileFilms')->with($data);
This works but I cannot figure out how to access this information, I tried making each "film" its own array with all three data values and then adding that to the larger array and then passing that array to the view, which again worked but I cannot figure out how to access the data. if i use say {{$title}} it will grab one title, but I have no way of getting both.
How can I pass N number of films each with its title, description and url to a view so I can display them, and how can I display them?
You are adding the values directly to the array once and that is it, because Laravels array_add only overwrites non existing keys.
You should do something like that (or use Eloquent):
<?php
$data = array();
foreach ($films as $film){
$data['films'][] = array(
'title' => $film->title,
'description' => $film->description,
'url' => $film->fileUrl
);
}
$this->layout->content = View::make('profileFilms')->with($data);
Then you can access your films (Blade style):
#foreach($films as $film)
{{$film['title']}}
#endforeach
If you want to use it like $film->title you can use the (object) typecast while assigning the array.
Of course, you could also pass $films directly like
$data = array(
'films' => $films
);
Then you can access your films like you would in the controller. You can alsways use print_r and var_dump to see, what the variable really contains.
$data = array();
foreach ($films as $film)
{
$row = array();
$row = array_add($data, 'title', $film->title);
$row = array_add($data, 'description', $film->description);
$row = array_add($data, 'url', $film->fileUrl);
// will send to view
$data[] = $row;
}
$this->layout->content = View::make('profileFilms')->with($data);
Related
I've an existing form which is passing the input data to the model in an array format. $postdata has all the data from the view and sending to model.
Controller:
$inquiry_id = $this->input->post('inquiry_id');
$postdata = $this->input->post();
$this->load->model('Design_model');
$this->Design_model->insertdata($postdata,$inquiry_id);
Model:
function insertdata($data = array(), $inquiry_id){
$sql = $this->db->query("select * from design where inquiry_id='".$inquiry_id."'");
if($sql->num_rows() == 0){
$sql_query = $this->db->insert('design', $data);
}
else{
$this->db->where('inquiry_id', $inquiry_id);
$this->db->update('design', $data);
}
}
Above is working fine. Now, I'd like to add few fields in the view and save in a different database table. Need to exclude the new field values from $postdata array getting saved. Need to find the best approach to do this. I can start with some name for all the new fields, so that we can add any filter if available to exclude from the $postdata.
You can use elements() function from Array helper.
$array = array(
'id' => 101,
'title' => 'example',
'desc' => 'something',
'unwanted' => 'bla bla'
);
$filtered_array = elements(array('id','title','desc'),$array); //you can use this directly to the post data
$this->Design_model->insertdata($filtered_array,$inquiry_id);
You can use array_merge() or array_push() functions to add new fields to the array.
Let's say you have following data
$postdata = array("name"=>"xyz",
"email"=>"xyz#gmail.com",
"age"=>"40",
"gender"=>"Male",
"occupation"=>"Engineer"
);
Of which first 3 records are from old fields and last 2 are from new fields as you saying.
You need to find last index of first set i.e. '3' Now you can do this.
$firstDb = array_splice($postdata,0,3); //here 3 is index we are using to get first 3 records from $postdata
$secondDb = array_slice($postdata,0,3); //here 3 is index we are using to get records from position 3 from $postdata
Output:
$firstDb = array("name"=>"xyz","email"=>"xyz#gmail.com","age"=>"40");
$secondDb = array("gender"=>"Male","occupation"=>"Engineer");
Now you can insert you records as you wish to. Happy coding
I have an array of objects which have been posted from a Vue Axios function, which I wish to loop over and save into a database. They are answers to a question.
I have passed in $data which is the array of answer objects (each has a content, correct and mark property), and the $id of the question they belong to. When I return $data, it shows me the array of objects with all the correct properties. When I return $data[0], I can access the first object. But when I try and foreach as below, it complains that $content doesn't exist. Running count() on $data also errors. What is wrong here?
Route::post('answers/{id}', function (Request $data, $id) {
foreach ($data as $value) {
$post[] = [
'user_id' => 1,
'question_id' => $id,
'content' => $value->content,
'correct' => $value->correct,
'mark' => $value->mark
]);
}
Answer::save($post);
});
You are trying to iterate over the hole $request object, which is an instance of the Request class. To access the received values first get them:
// To get all the data
$data = $request->all();
// or..
// To get just a specific value
$data = $request->get('key');
// or..
// only a list of allowed elements
$data = $request->only('here', 'goes', 'your', 'keys');
So, in case your frontend are sending an array of items under the key items. Just get them like mentioned above:
$items = $request->get('items');
Then you can use the foreach():
$items = $request->get('items');
foreach($items as $item)
{
// your operations
}
You can read more about Retrieving Input, in the documentation.
I have an array of the names of my POST variables to use when I update a row in my database.
$jobs = array( "proposal_id",
"will_provide",
"general_scope",
"per_bid",
"job_type");
Using this style my table is called jobs and each value in the array is a column id.
I want to edit this array so each item (column id) contains a single _POST Value
Then I have a function that uses the variables to create generic queries.
function save_data($jobs) {
foreach ($jobs as $job)
{
$job[$job[$i]] = _$Post[$job];
or
Table_name[column] = cell value;
...
...
...
I would like to be able to save $values into the post variables associated to it. Something like
For example if I was going to manually create this array it would look like
$jobs = array('proposal_id' => '12345678','title_of_project' => 'aTitle','creator' => 'aUser','last_modified' => '0000-00-00','date_created' => '0000-00-00','price' =>'1000');
This should be what you're looking for:
$jobs = array( "proposal_id",
"will_provide",
"general_scope",
"per_bid",
"job_type");
$jobValues = array();
foreach($jobs as $job) {
$jobValues[] = isset($_POST[$job]) ? $_POST[$job] : null;
}
$jobs = array_combine($jobs, $jobValues);
i want to insert two different values into my database.
The field names are same but i want two different values to be saved there.
For Now i have a form which creates Organization. in that i have two fields sales and tech. So i insert Name,last_name,email for sales and tech as well. Now whatever values i get from there i save it to users table and save their id to my organization table.
First i want insert sales person information and then tech person
information and get their id and save it in organization
This is my code:
$this->data['company'] = $this->company_m->get_new();
$this->data['user'] = $this->secure_m->get_new();
$rules = $this->company_m->rules_admin;
$this->form_validation->set_rules($rules);
if ($this->form_validation->run() == TRUE)
{
$data =$this->secure_m->array_from_post(array('first_name','last_name','email'));
$sales_id = $this->secure_m->save($data,$id);
$data =$this->secure_m->array_from_post(array('first_name','last_name','email'));
$tech_id = $this->secure_m->save($data,$id);
$data = $this->company_m->array_from_post(array('org_name','dba','addr1','addr2','city','state','country','pin','sales_id','tech_id','tax_number','comment','url'));
$data['sales_id']= $sales_id;
$data['tech_id']= $tech_id;
$this->company_m->save($data, $id);
redirect('admin/company');
}
// Load the view
$this->data['subview'] = 'admin/company/add';
$this->load->view('admin/_layout_main', $this->
Array from POST code
public function array_from_post($fields){
$data = array();
foreach ($fields as $field) {
$data[$field] = $this->input->post($field);
}
return $data;
}
You're trying to get values from wrong keys. I.e. you have first_name_sales and first_name_tech, but always read from first_name.
Try
$data_tech = $this->secure_m->array_from_post(array('first_name_tech','last_name_tech','email_tech','tech'));
// ...
$data_sales = $this->secure_m->array_from_post(array('first_name_sales','last_name_sales','email_sales','sales'));
// ...
Also you should set_value from corresponding objects instead of same $user. Pass like $user_tech and $user_sales to view. Smth like
$this->load->view('admin/_layout_main', ['user_tech' => $data_tech, 'user_sales' => $data_sales]);
$values = array(
'first_name' => $this->input->post('first_name'),
'last_name' => $this->input->post('last_name'),
'email' => $this->input->post('email'),
'user_type' => $this->input->post('user_type'),
);
$data = $this->model->inset('table_name', $values);
And also use this array to many tables
I have a multidimensional array $data on the controller side. I populate $data[$group] with any group value, between G1 - G100. I then pass the array to a view via a controller:
$this->load->view('example', $data);
On the view-side I can then access the variable, for instance $G1, $G2. The problem is that I dont know before-hand what will be passed. I can try to access my variable like this in the view:
if (isset($G1)) echo $G1;
if (isset($G2)) echo $G2;
if (isset($G3)) echo $G3;
But this becomes highly unpractical when the group variable in $data[$group] on the controller-side can have many different values.
Is there any way to check from the view beforehand what is being sent?
I don't think it is possible to know what will be passed, but you can put $data itself into an array and pass this array to the view, and in the view go through $data with a foreach:
//controller
$newdata = array(
//maybe other data
'data' => $data
);
$this->load->view('someview', $newdata);
//view
foreach($data as $key => $value){
//do whatever you like
}