I know this has been posted a million times over but I cant find an example where the same item was being called for the same use but with different values. I am using lightbox and I need my <a> to pull in the size=full and my <img> to pull in the size=thumbnail. I am successfully doing this but my nested foreach statements are casing duplicates.
<?php
$dyno_images = rwmb_meta( 'gallery-images', 'type=image_advanced&size=thumbnail' );
$dyno_images_lrg = rwmb_meta( 'gallery-images', 'type=plupload_image&size=full' );
?>
<?php
foreach ( $dyno_images_lrg as $dyno_image_lrg ) {
foreach ( $dyno_images as $dyno_image ) {
echo '<figure class="gallery-item"><div class="gallery-icon landscape"><img src="'.$dyno_image['url'].'" aria-describedby="gallery-1-584" class="attachment-full"></div></figure>';
}
}
?>
I assume that each item in $dyno_images_lrg corresponds to an item in $dyno_images.
In that case you only want to loop once and pick out the corresponding item:
foreach ( $dyno_images_lrg as $key => $dyno_image_lrg ) {
$dyno_image = $dyno_images[$key];
//Snipped for brevity - rest of the code should remain the same
}
Related
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 );
I have made a PHP foreach loop for my array and I have a question:
I can show only the data less than 2 executed by the calculation? Is possible?
foreach (array_combine($fattorerov, $fattorenorm) as $valore => $valore2) {
$conteggio = $valore2." X ".$valore." = ".$valore * (0.70);
echo $conteggio."<br><br>";
}
Simply put if statment in your loop -:
If($conteggio<2){
echo $conteggio;
}
I'm trying to display results in a foreach loop.
I have an array like so:
$slides = array(
1 => $params->get('param_1'),
2 => $params->get('param_2'),
3 => $params->get('param_3')
);
If a parameter in the backend is set to yes, the value equals 1, and thus a result is displayed.
So what I'm trying to write is, foreach array value that is equal to 1
I'm aware that I can't use something like this:
foreach (slides == 1) {
// echo stuff here
}
How would I write this foreach loop? Any help or guidelines would be highly appreciated
foreach ( $slides as $slide ) {
if ( $slide != 1 ) continue;
// echo stuff here
}
I have a foreach loops for an array called $feedsMerged. I have this foreach loop near the top of a document and it's setting some global variables from the array.
I'd like to recall the foreach loop, or run it again on that array and this time build elements from the variables I set above.
Top of the document.
foreach ($feedsMerged as $posts) {
$post_id = $post['post_id'];
etc etc
}
Then bottom of page
foreach ($feedsMerged as $posts) {
echo '<div class="' . $post_id . '">stuff</div>';
}
Is this possible to do? The reason for using two foreach loops on the same array is I'd like to keep this all organized and I'll have content in between each foreach that can't be in this foreach
No problem at all. You can access an array as many times as you like using any supported method in a script.
i want to insert some data into a data table, for a wordpress plugin.
the data is taken with POST.
i have multiple results (taken with post), but $wpdb->insert only inserts me the last result (practically overwrites the data). why is that?
here is my code:
html:
echo '<label for="reduceri-post-category"><b>' . __("What categories should be the adverts", 'appplugin' ) . '</b></label><br /><br /><br />';
?>
<?php foreach ($the_data[categories] as $sat): ?>
<b> <?= $sat[name]; ?> <br /> </b>
<?php foreach ($sat[subcategories] as $cat):
?>
<input type="checkbox" name="reduceri-post-category" value="<?= $cat[sid] ?>" /> <?php echo $cat[name]; echo $cat[sid]; ?><br />
<? endforeach; ?>
<? endforeach; ?>
global $wpdb;
$thedata['reduceri-post-category'] = $_POST['reduceri-post-category'];
$table_name = $wpdb->prefix . "reduceri";
foreach ($thedata as $key => $value) {
if( $post->post_type == 'revision' ) return;
if ( $post->post_type == 'post'){
$wpdb->insert($table_name, array( 'time' => current_time('mysql'), 'post' => $post->ID, 'category' => $value));
}
}
what can i do in order to be able to insert ALL the results, not only the last one?? thanks a lot!
What is in reduceri-post-category?
You say that you have multiple values in post. How are these multiple values passed into your plugin? Does reduceri-post-category contain multiple values? Do you use separate keys for each of the values? For example, reduceri-post-category2/3/4?
You are using a foreach to iterate through $thedata. However I don't see anywhere in your code where you actually create an array in $thedata. So your foreach is only ever going to execute once, and it will execute based on what is inside $_POST['reduceri-post-category'];
What I think you want to do, tough to say, is one of these two scenarios.
Scenario 1 - multiple post keys hold data you are after
$thedata[foo1] = $_POST[foo1];
$thedata[foo2] = $_POST[foo2];
$thedata[foo3] = $_POST[foo3];
foreach ($thedata as $key => $value) { }
Or (pseudeocode) - a single post key holds all your category data. So you have to split it up and then execute on each one.
$thedata = explode("?", $_POST[reduceri-post-category]);
foreach ($thedata as $key => $value) { }