Model:
Grabs data from mySQL with the value: array['10','5','2'] and puts it in the session data
Controller:
Puts that session data into an array to pass to the view:
$fubar = array(
'ex1' => $this->session->userdata('ex1')
);
$this->load->view('view', $fubar);
View:
echo $ex1;
The view will spit out the array: array['10','5','2']
How do I parse that array to get the individual values (10, 5, or 2)?
For example, echo $ex1['1']; will spit out the first 1 characters: ar but I want it to return the value 5
I think perhaps I may not be storing the array properly in mySQL, but I'm not sure.
$fubar['ex1'] = $this->session->userdata('ex1');
$this->load->view('view', $fubar);
Now Try
$ex1[0];
$ex1[1];
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
When you pass an associate array of data to views with Laravel, how does that associative array get converted into variables? I tried to look at the source code but I can't quite see how it's done.
// View
view('greetings', ['name' => 'Victoria', 'title' => 'Test']);
// Using the associative array data in the templates
<?php echo $name; ?>
<?php echo $title; ?>
https://laravel.com/docs/5.4/views#passing-data-to-views
Is PHP's extract function used?
Yes, it uses the extract function.
You can find this in the source code here Illuminate\View\Engines\PhpEngine.php.
After passing data to view you can display the above data as follow.
//this one will display the name
{{$greetings['name']}}
//this one will display the title
{{$greetings['title']}}
I have created a custom field named "sec1array" in my categories so that i can add an array, for example 1,2,3,4
I want to retrieve that array and output it in the loop, so I created this code.
$seconearray = array($cat_data['sec1array']);
$args = array(
'post__in' => $seconearray
);
However, it only seems to be outputting the first post in the array. Is it something to do with the way the comma is outputting?
If I print $seconearray it outputs correctly, example 1,2,3,4
What you are doing is storing a string value of "1,2,3,4" in your database which when you are trying to construct an array from it like array("1,2,3,4") you end up just assigning a single value to that new array. This is why it only contains a single value.
You need to store your value in a serializable format so it can be converted back to an array after you save it to the database. There are many ways to do this, I'm sure others will give more examples:
JSON encode it
json_encode(array(1,2,3,4)); // store this in your db
json_decode($cat_data['sec1array']); // outputs an array
Or, you can use PHP serialize
serialize(array(1,2,3,4)); // store this in your db
unserialize($cat_data['sec1array']); // outputs an array
If you want to keep your string, you can explode it:
explode(',', $cat_data['sec1array']); // outputs your array of 1,2,3,4.
Using any of these methods will work. Finally you'll end up with an example like:
$seconearray = explode(',', $cat_data['sec1array']);
$args = array(
'post__in' => $seconearray
);
I'm using version 1.1.14 of yii.
My VIEW file has
<?php echo $form->dropDownList($model,'estado', CHtml::listData(Estado::model()->findAll(), 'id', 'estado')); ?>
I have a model called Estado which was generated from a table with only 2 fields ID as PK and estado where I have my data. Which has 3 rows Active, Inactive, Prospecting.
So far the code only shows the last row of that table, ignoring the first 2.
What am I doing wrong?
for the dropdown list you can pass a normal array :
$data = array(
'number1',
'number2',
'number3',
);
or an array with key => value
$data = array(
7 => 'number7',
2 =>'number2',
4 =>'number4',
);
Chtml::listData() will only help you make that array avalable for the function
however if you need to make a combination of models ( or arrays) you have to do that manually using array concatenation functions such as CMap::mergeArray()
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
}