I just found a script to programatically delete a field collection to a specific node :
<?php
$node = node_load(1);
$field_collection_item_value = $node->field_page_collection1[LANGUAGE_NONE][0]['value']; // Take field collection item value.
entity_delete_multiple('field_collection_item', array($field_collection_item_value)); // Delete field collection item.
?>
Unfortunately as i see it , it only delete first field collection, I need to select which one i want to delete.
Here is my structure :
Multiple field collection who have : a reference to another node and two selects
I have the reference nid in the url so I can use it, but I don't have any idea how to select the right field collection with that.
Thanks
Try to use this:
$node = node_load(1);
$searhed_nid = '2';
$field_page_collection1 = field_get_items('node', $node, 'field_page_collection1');
foreach ($field_page_collection1 as $item) {
$field_collection = entity_load_single('field_collection_item', $item['value']);
$fc_item_wrapper = entity_metadata_wrapper('field_collection_item', $field_collection);
// lets take name the field with ref field_ref_nid.
$field_val = $fc_item_wrapper->field_ref_nid->raw();
if ($field_val == $searhed_nid) {
$field_collection->delete();
}
}
Related
I have a database table with the following structure.
id
setting_name
setting_value
1
website_name
New Website
2
contact_email
info#example.com
3
contact_address
London, UK
Now I am using laravel to return the values of this table,
$syetem_settings = System::all()
This returns it as a collection and there is no way I can use the individual values
$syetem_settings = System::all()->toArray()
This also returns it as an array and foreach loop isn't helping me.
What I want to do
I want to use this variables in the view blade. e.g
{{system_settings->website_name}} // to return website name;
{{system_settings->contact_email}} // to return website email;
Please what is the right code for this? I'm using php 8 and laravel 9
If you really must have this database design, then you would need to get each setting by name from the database as single models, like this:
$websiteName = System::where('setting_name', 'website_name')->first();
$contactEmail = System::where('setting_name', 'contact_email')->first();
Then just output it like this:
{{ $websiteName }}
{{ $contactEmail }}
But if you want to have all the settings variables in one single object, then you would need all the setting_name values as actual columns. So you get ONE single model with all the values instead of one model per value.
This might not be what you want, for other reasons. So a workaround for you, with your current database design, could be something like this:
// Get all rows
$systemSettings = System::all();
// Create empty object
$settingsObject = new stdClass;
// Loop through all the rows (models)
foreach($systemSettings as $systemSetting) {
// Get the settings_name to use as attribute name
$attribute = $systemSetting->setting_name;
// Get the settings_value to use as value for the attribute
$value = $systemSetting->setting_value;
// Add attribute and value to the empty object
$settingsObject->$attribute = $value;
}
This should give you an object called $settingsObject that has all the rows as attribute->value. And then you could output your stuff like this:
{{ $settingsObject->website_name }}
{{ $settingsObject->contact_email }}
I think you're trying to use Entity-Value architecture for your table.
It could be a good idea to pluck each column data separately and then combine them as a single array:
$keys = System::pluck('setting_name')->toArray();
$values = System::pluck('setting_value')->toArray();
$arr = array_combine($keys, $values);
then you can use foreach to itrate through the combined array:
foreach($arr as $k => $v) {
...
}
Searched, Googled already couldn't found one with same case,
Basically i have a set of multiple categories in one form, i want to update Number of questions in each category on one form submit.
Following is the form:
Number of categories can be dynamic, each Question TextBox contains its name = "question" merged with category ID and make it as "question12 , question13" etc.
I know about update_batch() , but how do i get values and put them in array as they can be of unknown number.
How do i update all categories at once in CodeIgniter
$post = $this->input->post();
foreach($post as $key=>$value){
if(strpos($key, "question") == 0){
$category_id = substr($key, 8);
//Use $category_id and $value in this loop to build update statement
}
}
I have resolved this using foreach in Controller
foreach ($sample_settings as $sample) {
$array['category_id'] = $sample['category_id'];
$array['no_of_questions'] = $this->input->post('question'.$sample['category_id']);
$batch_array[] = $array;
}
I have a rather large table that has 125 data inputs, each of which has to be saved in a separate column.
I have named each HTML input as 1,2,3 etc... and the same within the table, to hopefully help things.
At the moment, I have the following code:
$observation = new Observation();
$observation->site_id = Input::get('site_id');
$observation->result = Input::get('1');
$observation->result_id = '1';
$observation->save();
Is there a way I could use a loop to iterate through the 125 data inputs (Input::get('X') and result_id = 'X') and then save them all?
for($i=1; $i<=125; $i++) {
$data[$i] = [
'site_id'=>Input::get('site_id'),
'result'=>Input::get($i), 'result_id'=>$i
];
}
Using Query builder:
DB::table('table')->insert($data); // Change this with your table name.
and if you want to use Eloquent:
Model::insert($data);
You can use something like this pattern in your controller to handle creating new Observations and editing existing Observations without worrying about the specific attribute names:
// get all non-empty inputs except 'token'
$inputs = array_filter(Input::except('_token'), 'strlen');
// assuming 'id' is your primary key and sent as an input in your form
if (Input::has('id'))
{
// An edit
$id = Input::get('id');
$observation = Observation::find($id);
foreach ($inputs as $key => $value)
{
$observation->$key = $value;
}
}
else
{
// A create
$observation = new Observation($inputs);
}
This solution doesn't force you to use sequential column names or html input names like 1..125 and will allow you to use more meaningful column names if you prefer. However, it does assume that your column names (and therefore object attributes) are the same as the html input names.
Related, but you might also like to know that if you use the HTML helpers in the Blade view template to construct your form, and open the form with Form::model, it will fill even fill in the values of the inputs using the object that you pass to it.
For example, in the view:
{{ Form::model($observation) }}
I have a taxonomy vocabulary kategorie like this:
Kat01
Child01
Child02
Kat02
Child01
Child02
The vocabulary has a custom field with IDs like this: 957214d2-ce39-423d-aeb1-32f2e8b972f6, so every term and parent term has an ID (which is NOT the tid!).
I have a content type with a referenced taxonomy field kategorie. Also there is an array field called kategorieTempId, and the values are the same like in the kategorie-taxonomy. The number of IDs in that array represent the hierarchical order of the terms and parent terms. So, if the node should be in kategorie -Kat01 -> Child01, there would be two IDs in kategorieTempId, one for the parent, one for the child term.
Now I need to get the ID of the child term, than get the taxonomy term from the vocabulary and write it into the referenced term field.
What I did so far:
Set up a rule, which is fired before saving a node, using the action Fetch entity by property to get the taxonomy terms from the vocabulary into an array. Then I add an action Execute custom PHP code to find the child tid and write it into a field called katReturn.
Here is the codeI use (I'm a total php-beginner, and I don't know, if this is an elegant way to do it, but it works):
$anzahl = 0; //counter for hierarchy
foreach ($kat_id_fetched as $kat_term) { // fill two arrays
$tid[$anzahl] = $kat_term->tid; // array with termID
$parentTerm_id[$anzahl] = db_query("SELECT {parent} FROM {taxonomy_term_hierarchy} WHERE tid = $kat_term->tid")->fetchField(); // array with parent-termID
++$anzahl;
};
foreach ($tid as $item) { // check if tid is parent
$anzahl = 0;
if (!in_array($item, $parentTerm_id)) {
$kat_child = $item; // if not in parent-array, save tid
$node->field_kat_return['und'][0]['value'] = $item;
break;
};
++$anzahl;
};
So now I know the tid of the child term and I want to write it into the reference taxonomy field, but I don't get it. The code I try is:
foreach ($kat_id_fetched as $kat) {
if ($kat->tid == $returned_kat) {
$node->field_kategorie[] = $kat;
break;
};
};
I hope, I made clear what I want. As I said before, my php-skills are limited and I appreciate any help, which points me to the right direction. Thanx.
The solution is simpler than I thought. Thanks to #Clive
I can save the term-id, which is stored in $item, directly without going through a third foreach-loop.
$node->field_kat_return['und'][0]['tid'] = $item;
I need help to traverse all options in a multioption.
I use the Product-class with a new multioption-attribute called "product_properties". I need a function to check if the optionID the user chose on the front-end matches an option in the list, and return true if a match is found.
This way I can check if e.g. the user chose "Red" as the "Color" on a product.
In pseudo-code this is what I need:
Parameters: postedOptionID, currentObjectID
Fetch attribute "product_properties" (multioption) on object .
For each option for "Color" in "product_properties"
2.1 If postedOptionID == optionID
2.1.1 return true
Thanks
I finally found a way :)
$product_properties_name is the name of a class-attribute that's an 'ezmultioption'-datatype. In my case it's called 'product_properties' and is an attribute on the 'Product'-class.
First get all of the object's attribute:
$contentObjectAttributes = $contentObject->version($contentObject->attribute( 'current_version' ) )->contentObjectAttributes();
and then loop each and find 'product_properties':
// Loop all attributes of the object's class
foreach(array_keys($contentObjectAttributes) as $key)
{
$contentObjectAttribute = $contentObjectAttributes[$key];
$contentClassAttribute = $contentObjectAttribute->contentClassAttribute();
$attributeIdentifier = $contentClassAttribute->attribute("identifier");
// Get 'product_properties'-attribute
if ($attributeIdentifier == $product_properties_name)
{
// Get the multioption
$multioption_list = $contentObjectAttribute->content();
// Loop all multioption lists (Color, Make, Brand etc.)
foreach($multioption_list->attribute('multioption_list') as $index => $option)
{
// Loop through this multioption and get all options (if 'Color', get 'Blue', 'Red', 'Green' etc.)
foreach($option['optionlist'] as $option)
{
$optionValue = trim($option['value']);
// if there's a match on $optionValue, do something interesting...
}
}
}
}