Create new field with value separet with comma - php

I am selecting multiple values ​​that I export from google sheet.
I need to make them independent of filtering them in ACF so that they can be filtered separately.
I show code that I'm trying to use in functions.php but I do not know where I'm wrong:
$value=get_field('location'); //location is my custom field that I am exporting from google sheet
$arrFields= explode(", ",$value);
if( $value)
{
foreach($arrFields as $v){ echo $v.'<br>'; };
}
Meta Keys Select will be automatically modified? this is what the filter takes
thanks!!

You are not checking whether it is array or not
$value=get_field('location');
$arrFields= explode(", ",$value);
if( is_array($arrFields))
{
foreach($arrFields as $k=>$v)
{
echo $k.'- '.$v.'<br>';
}
}

You can use implode instead of foreach
http://php.net/manual/en/function.implode.php

Related

How to create Json objects from foreach $_POST loop

i have made some random input names, because its a drag and drop page builder, so i can't guess, how much elements user will use, so i have created a random input names,
for that am using php foreach loop for $_POST requests. i have tried to make it encoded into json and then later save it into database. but it looks like something is wrong in my json.
Here are my html demo codes :
<input style="display:none;" name="DATA-BLOCK-A(some random string)">
<input style="display:none;" name="DATA-BLOCK-B(some random string)">
PS: A is for selecting element A, and B is for element B.
here is my PHP code :
if (isset($_POST)) {
//$arr = array();
foreach($_POST as $key => $value)
{
$arr = array($key => $value);
$encode = json_encode($arr);
echo $encode;
}
}
and Here is the result :
{"sortlist":"block[]=D5e3385b75a75d&block[]=K5e3385b85a75e&block[]=C5e3385b95a75f&block[]=F5e3385ba5a760"}{"save_cont_flag":"0"}{"DATA-block-D5e3385b75a75d":"0#TRANSP
<\/p>"}{"DATA-block-K5e3385b85a75e":"0#TRANSP20"}{"DATA-block-C5e3385b95a75f":"01#TRANSP0images\/250place.jpg\u00b8"}{"text-1573532276681":""}{"textarea-1573532278320":""}{"DATA-block-F5e3385ba5a760":"121212unundefined"}{"page_name":"123"}{"aff_link":""}{"pause_link":""}{"seo_title":""}{"fbook":""}{"seo_desc":""}{"seo_keywords":""}{"back_color":"#EEEEEE"}{"body_color":"#FFFFFF"}{"back_image":""}{"ty_font_color":"#000000"}{"ty_override":""}{"ty_name":"12314"}{"ty_stm":""}{"modal_para_width":"0"}{"catcha_url":""}{"catcha_un":"Yes"}{"catcha_message":""}{"code_head":""}{"code_body":""}{"modal_share_width":"0"}{"modal_cta_width":"0"}{"modal_video_width":"0"}{"modal_mp_width":"0"}{"modal_stm_width":"0"}{"modal_image_width":"0"}{"modal_bonus_width":"1"}{"ty_headline":""}{"modal_spacer_width":"0"}{"att_bar_status":"0"}{"att_delay_in":"0"}{"att_bar_color":"#bbbbbb"}{"att_gradient":"0"}{"att_text_color":"#000000"}{"att_text_font":"Open Sans:400"}{"att_text_size":"14"}{"att_bar_message":"Add Your Attention Bar Text Here"}{"att_link_color":"#000000"}{"att_link_label":"Add Link Text Here"}{"att_link_url":"http:\/\/commissiongorilla.com"}{"count_font":"Open Sans:800"}{"count_size":"55"}{"count_status":"0"}{"count_type":"0"}{"count_end":"01\/31\/2020 6:41 AM"}{"count_zone":"0.0"}{"count_eg_days":"0"}{"count_eg_hours":"0"}{"count_eg_mins":"0"}{"count_digit_color":"#bbbbbb"}{"count_label_color":"#bbbbbb"}{"count_background":"0"}{"count_language":"1"}{"count_exp":"0"}{"count_url":"http:\/\/commissiongorilla.com"}{"count_add_days":"0"}{"count_add_hours":"0"}{"count_add_mins":"0"}{"modal_countdown_width":"0"}{"modal_review_width":"0"}
and also how seperate all A BLOCKS and B BLOCKS?
Thanks.!
You don't need to use a loop.
Just used json_encode :
$json = json_encode($_POST);
If you need to get key contain DATA-block-, you can write :
foreach ($_POST as $key => $value) {
if (strpos($key, 'DATA-block-') !== false) {
// Here `DATA-block-{}`
}
}
If you change the input names to DATA-BLOCK-A[] and DATA-BLOCK-B[], $_POST['DATA-BLOCK-A'] will contain an array of all a blocks and $_POST['DATA-BLOCK-B'] will contain an array of all b blocks.
This also eliminates the need for generating random strings.

output values from array into variables in php

I have 2 pages in php. The 1st page includes a form which transimts data to the 2nd page. The form uses method=post. Data transmitted successfully in the 2nd page. I have the following code, which gets data and printing them using the code:
php?
var_dump($_POST);
foreach ($_POST as $key => $value) {
echo $value;
}
?>
All I want is to extract data from array and place them into variables, because I want to use these varaibles later in some if startments and mysql queries. Any idea how can i do this?
First, these really are basic PHP skills (or programming skills for that matter). Try to follow some tutorials or courses before attempting to write code in the "real world".
As long as you know the key for the value you want to store, this is how you do it:
$yourVariableName = $yourArray['yourKey']; // or just a number if the key is an int
You don't need for loops to do this.
EDIT
$kentroName = $_POST['kentro_name'];
$kentroSurName = $POST['kentro_surname'];
// And then the following six.
<?php
var_dump($_POST);
$array = array();
foreach ($_POST as $key => $value) {
echo $value;
$array[$key] = $value;
}
print_r($array);
?>

Advanced Custom Fields (ACF) - loop through repeater checkout field and output single instance of each value

I have a WordPress page that loops through a list of products. The products are created using ACF repeater field. Each product has a one or more attributes (e.g. light, dark, metallic, colourful) applied using the checkbox field.
The aim is to query the checkbox repeater fields for all products on the page, see what attributes are assigned to each product on an individual basis, and output a single list for all products (stripping out duplicates).
For example, if there were ten products and between them they were tagged with four unique attributes, only the four unique attributes would be output.
I've tried creating an empty array and then looping through that. The current loop outputs duplicate values (eg light dark light dark light dark instead of light dark)
<?php if (have_rows('product')): while (have_rows('product')) : the_row();
$attributes = get_sub_field_object('product_attributes');
$values = $attributes['value'];
if($values) {
$filter_attributes = array();
foreach ($values as $value) {
if (in_array($attributes, $filter_attributes)) {
continue;
}
$filter_attributes[] = $attributes;
echo $value . " ";
}
} endwhile; endif; ?>
There are quite a few issues with your code so we really need to start again. Based on what you've provided and without knowing exactly how your ACF repeater is set up, I think the following should work for you.
What it appears that you want to do is:
Loop through all products in your repeater
Get all values from your product_attributes
Get the unique values across all products
Display the unique values on the same line, separated by spaces
The main problem you are having is getting the unique array. There are a number of ways to do this, you chose the most complicated :)
1. Use in_array to check previous values
This is the way you are trying to do it at the moment, but you are having problems with the logic. Therefore I'd suggest option 2, but for completeness this is how you should do it:
$filter_attributes = array();
if (have_rows('product')): while (have_rows('product')) : the_row();
$attributes = get_sub_field_object('product_attributes');
$values = $attributes['value'];
if($values) {
foreach ($values as $value)
if (!in_array($value, $filter_attributes)) {
$filter_attributes[] = $value;
}
}
} endwhile; endif;
/* display the values on the same line, separated by spaces */
echo implode(" ", $filter_attributes );
2. Use array_unique
The code for this is much simpler than the previous option. You save all values into an array and then at the end use array_unique (PHP.net array_unique). This eliminates the need for checking the existing values every time. e.g.
$all_attributes = array();
$filter_attributes = array();
if (have_rows('product')): while (have_rows('product')) : the_row();
$attributes = get_sub_field_object('product_attributes');
$values = $attributes['value'];
if($values) {
foreach ($values as $value)
$all_attributes[] = $value; /* Save ALL values */
}
} endwhile; endif;
/* use array_unique to remove duplicates from the array */
$filter_attributes = array_unique ($all_attributes);
/* display the values on the same line, separated by spaces */
echo implode(" ", $filter_attributes );
3. Use the array key
If you use the value for the array key, then that will ensure each values will be unique because duplicate keys are not allowed in the array. It's slightly hack-y way, but its quick & easy :)
$filter_attributes = array();
if (have_rows('product')): while (have_rows('product')) : the_row();
$attributes = get_sub_field_object('product_attributes');
$values = $attributes['value'];
if($values) {
foreach ($values as $value)
/* if $all_attributes[$value] already exists, this will overwrite it
ensuring the array only has unique values */
$all_attributes[$value] = $value;
}
} endwhile; endif;
/* display the values on the same line, separated by spaces */
echo implode(" ", $filter_attributes );

cakephp foreach loop only display the first letter from title

i want to display data from database and also i have created function in model file which is showing data from database but all values are shown in the array format.
problem is that when i print echo $values['title']; in foreach loop it is showing only first letter from title array??
model code
function reviewcitypage()
{
$cacheKey = 'city_page';
GigaCache::set(array('duration'=>"+1 minutes",'path'=>CACHE));
$cachedCategoryData = GigaCache::read($cacheKey);
if($cachedCategoryData && !cr('DynamicPage.field'))
{
$recentactivity = $cachedCategoryData;
}else
{
$recentactivity= $this->find("list",array("conditions"=>array("status"=>1),'fields'=>array('title','body','rating'),'recursive'=>-1,'limit'=>10));
//dont't set cache if dynamic field
if(!cr('DynamicPage.field'))
{
GigaCache::set(array('duration'=>"+1 minutes",'path'=>CACHE));
GigaCache::write($cacheKey,$recentactivity);
}
}
return $recentactivity;
}
view file
$ReviewObj = cri('Review');
$recentactivity = $ReviewObj->reviewcitypage();
foreach ($recentactivity as $name => $value){
foreach($value as $values)
{
echo $values['title'];
}
}
**problem is solved now thanks for support **
i have changed the code in model file and it is woking now
$recentactivity= $this-
>find("all",array("conditions"=>array("status"=>1),'recursive'=>-1,
'limit'=>10));
Your find() query is preparing the data as a 'list'. in cake lists are always key => value pair arrays. so in your view when you use the second foreach loop you are saying foreach character in a string...do.....
in your example $value can only be a string. foreaching it can only make $values a single char.
Let me know if you still unsure what i mean. not the best at explaining what i mean
http://book.cakephp.org/2.0/en/models/retrieving-your-data.html#find-list
Because you are after 3 fields I suggest using either first or all in place of list as the first argument in the find() method.

Get post values when the key is unknown in CodeIgniter

CodeIgniter allows access to POSTed data via:
$this->input->post('input_name');
where 'input_name' is the name of a form field. This works well for a static form where each input name in known ahead of time.
In my case, I am loading a collection of key/value pairs from the database. The form contains a text input for each key/value pair.
I am wondering, is there a way to get an array of posted data via the CodeIgniter api?
Thanks!
According to the documentation, no. I would suggest just using array_keys($_POST) to get the keys.
foreach($this->input->post() as $key => $val) { echo "<p>Key: ".$key. " Value:" . $val . "</p>\n"; }
that can be used to
Surely if you have an array of keys from the database you can use that, like :
foreach ($arrayFromDb as $key => $value) {
$newValue = $this->input->post($key);
}
Then you have the advantage that people if people submit additional fields (e.g. by modifying the form and posting it themselves) those fields will be ignored
$array_db_columns = $this->db->query('SHOW COLUMNS FROM ci_props');
$array_db_columns = $array_db_columns->result_array();
$array_save_values = array();
foreach ( $array_db_columns as $value )
{
$array_save_values[$value['Field']] = $this->input->post($value['Field']);
}
insert :
$this->db->insert('props', $array_save_values);
update :
$this->db->where('id',$id); $this->db->update('props',$array_save_values);

Categories