I have the following loop...
for ($i = 1; $i <= 10; $i++) {
echo '<span class="srch-val-'.$i.'">'.apply_filters(" $value\n", $value)."</span>";
}
within...
while ( $query->have_posts() ) : $query->the_post();
if ( $keys = get_post_custom_keys() ) {
echo "<div class='clearfix card-prod ".($i==0?'first':'')."'><span class='card-title'>";
echo the_title();
echo "</span>";
foreach ( (array) $keys as $key ) {
$keyt = trim($key);
if ( '_' == $keyt{0} || 'pricing' == $keyt || 'vehicleType' == $keyt || 'coverageRegion' == $keyt || 'locationType' == $keyt )
continue;
$values = array_map('trim', get_post_custom_values($key));
$value = implode($values,', ');
for ($i = 1; $i <= 10; $i++) {
echo '<span class="srch-val-'.$i.'">'.apply_filters(" $value\n", $value)."</span>";
}
}
echo "\n"; echo '<img src="wp-content/themes/cafc/images/top-choice.jpg" alt="Top Choice" class="topchoice">';echo '<img src="wp-content/themes/cafc/images/cards/dummy.png" />'; echo the_excerpt()."</div>";}
$i++;
endwhile;
When I execute my code however, say my while() loop returns 4 values, my for() loop then outputs 10 of the same thing, in my browser its shown as...
All I want to do is for each <span class="srch-val'> is to add a number after each 'srch-val' class, so srch-val-1, srch-val-2 etc...
You have an extra for-loop inside your foreach-loop. Remove this loop and just do the echo directly inside the foreach-loop and increment $i each time you actually use it.
Like this:
$i = 1;
while ( $query->have_posts() )
{
$query->the_post();
if ( $keys = get_post_custom_keys() )
{
echo "<div class='clearfix card-prod ".($i==0?'first':'')."'><span class='card-title'>";
echo the_title();
echo "</span>";
foreach ( (array) $keys as $key )
{
$keyt = trim($key);
if ( '_' == $keyt{0} || 'pricing' == $keyt || 'vehicleType' == $keyt || 'coverageRegion' == $keyt || 'locationType' == $keyt )
continue;
$values = array_map('trim', get_post_custom_values($key));
$value = implode($values,', ');
echo '<span class="srch-val-'.$i.'">'.apply_filters(" $value\n", $value)."</span>";
$i++; // move the incrementer here so that you only increment when you actually use it.
}
echo "\n"; echo '<img src="wp-content/themes/cafc/images/top-choice.jpg" alt="Top Choice" class="topchoice">';echo '<img src="wp-content/themes/cafc/images/cards/dummy.png" />'; echo the_excerpt()."</div>";
}
}
You create a variable at the beginning of your foreach-loop (say $counter == 1;)
Than your For-loop should be:
for($i = $counter; $i <= $counter+10; $i++){
//do your span-class thing here
}
And at the end of the foreach loop do: $counter += 10;
Related
I currently have this code:
foreach ($title_dump as $i => $title) {
$prev_i = $i === 0 ? $i : $i - 1;
if ($i > 0 and $title_dump[$prev_i] === $event->title) {
echo '<div class="hide-this-element"></div>';
} elseif ($i > 0 and $title_dump[$prev_i] != $event->title) {
if ($i === $title_dump[$i]) {
$show_event = '<div class="uk-text-left debugging">Stufe ' . $filtered_numbers . '</div>';
echo $show_event;
}
} elseif ($i === 0) {
echo '<div class="uk-text-left debugging">Stufe ' . $filtered_numbers . '</div>';
}
}
inside this I try to figure out how to prevent that $show_event is shown if the first if statement matches. With this I mean if if ($i > 0 and $title_dump[$prev_i] === $event->title) { matches I need to hide everything inside if ($i === $title_dump[$i]) { Is there a way to do this?
If you loop your $title_dump and notice that there is a element, you do not want to print, you can use continue; statement to go to the next item in the foreach.
foreach ($title_dump as $i => $title) {
$prev_i = $i === 0 ? $i : $i - 1;
if ($i > 0 and $title_dump[$prev_i] === $event->title) {
//Do not print anything
//will exit the loop for this item, and go the next item
continue;
} elseif ($i > 0 and $title_dump[$prev_i] != $event->title) {
if ($i === $title_dump[$i]) {
$show_event = '<div class="uk-text-left debugging">Stufe ' . $filtered_numbers . '</div>';
echo $show_event;
}
} elseif ($i === 0) {
echo '<div class="uk-text-left debugging">Stufe ' . $filtered_numbers . '</div>';
}
}
I have a repeater from ACF and what I want to is loop and count the items and based on the counted items output a particular css class.
Can I get help so the output of css classes depends on the counted items. For example if there was six items the class would be col-6 etc...
<?php
if( have_rows('rainbow') ):
$counter = 0;
while( have_rows('rainbow') ): the_row();
// vars
$name = get_sub_field('name');
$age = get_sub_field('age');
$cssClass = 'col';
for($i = 0; $i < $counter; $i++) {
if($counter === 6) {
$cssClass = 'col-lg-4';
} elseif ($counter == 4) {
$cssClass = 'col-xl-6';
}
else {
$cssClass = 'col';
}
}
echo '<div class=\'' . $cssClass. '\'>';
echo "<h4>" . $name . "</h4>";
echo "<p>" . $age . "</p>";
echo $counter;
echo '</div>';
$counter++;
endwhile;
endif;
?>
Don't know why are you using for loop here, $i < $counter this condition cant be true because $i and $counter both of them started from 0. so 0 < 0 == FALSE
You just need to remove for loop inside your while loop.
Or, if you are using for loop somewhere else in your code, then you can just move your conditions outside the for loop.
Second Solution:
Second, if you start $counter from 1 then you can achieve your desired result as given example:
<?php
$array = array(1,2,3,4,5,6);
$counter = 1;
foreach ($array as $key => $value) {
$cssClass = 'col';
for($i = 0; $i < $counter; $i++) {
if($counter === 6) {
$cssClass = 'col-lg-4';
} elseif ($counter == 4) {
$cssClass = 'col-xl-6';
}
else {
$cssClass = 'col';
}
}
echo $cssClass."<br/>";
$counter++;
}
?>
Result:
col
col
col
col-xl-6
col
col-lg-4
<?php
$counter = 1;
while( have_rows('rainbow') ): the_row();
// vars
$name = get_sub_field('name');
$age = get_sub_field('age');
if ( $counter == 4 ) {
$cssClass = 'col-xl-6';
} elseif ( $counter == 6 ) {
$cssClass = 'col-lg-4';
} else {
$cssClass = 'col';
}
echo '<div class="' . $cssClass . '">';
echo "<h4>" . $name . "</h4>";
echo "<p>" . $age . "</p>";
echo $counter;
echo '</div>';
$counter++;
endwhile;
try this
I'm trying to reverse the order of images that are displayed. I'm a PHP noob and I'm not sure how to do it. I guess I need to reverse the order of foreach that is displayed, but I'm not entirely sure how to do that.
<div class="yacht-view-right">
<?php
if (count($tpl['gallery_arr']) > 0)
{
$is_open = false;
foreach ($tpl['gallery_arr'] as $k => $v)
{
if ($k == 0)
{
$size = getimagesize(BASE_PATH . $v['medium_path']);
?>
<p><?php print_r(array_keys($v));
print_r(array_VALUES($v));
echo (count($tpl['gallery_arr']))
?></p>
<div class="yacht-view-pic" id="yacht-view-pic" style="width:<?php echo $size[0]; ?>px; height: <?php echo $size[1]; ?>px;">
<img id="yacht-view-medium-pic" src="<?php echo BASE_PATH . $v['medium_path']; ?>" alt="<?php echo htmlspecialchars(stripslashes($v['title'])); ?>"/> </a>
</div>
<?php
}h
$is_open = true;
?>
<div class="yacht-view-img">
<a href="<?php echo BASE_PATH . $v['large_path']; ?>" data-lightbox="yachts">
<img src="<?php echo BASE_PATH . $v['small_path']; ?>" alt="<?php echo htmlspecialchars(stripslashes($v['title'])); ?>" />
</a>
</div>
<?php
/*if ($k > 0 && ($k + 1) % 4 === 0)
{
$is_open = false;
?><div class="clear_left"></div><?php
}*/
}
if ($is_open)
{
?>
<div class="clear_left"></div>
<?php
}
} else {
}
?>
You can simply use array_reverse() before starting your foreach iteration:
$is_open = false;
$tpl['gallery_arr'] = array_reverse( $tpl['gallery_arr'], true );
foreach ($tpl['gallery_arr'] as $k => $v)
You could simply reverse the order of the array using array_reverse before iterating through it with foreach:
foreach ( array_reverse( $tpl['gallery_arr'] ) as $k => $v)
or you could iterate in reverse using a counter (which may have slightly better performance, if the array is large):
$gallery = $tpl['gallery_arr'];
for($i = $first = count($gallery) - 1; $i >= 0; $i-- ) {
$v = $gallery[$i];
if ( $k == $first ) {
...
}
So it looks like you could use the array_reverse() function
$tpl['gallery_arr'] = array_reverse( $tpl['gallery_arr'], true );
foreach ($tpl['gallery_arr'] as $k => $v){
...
}
Or use a regular for loop with inverted parameters.
for($k = count($tpl['gallery_arr']) - 1; $k >= 0; $k--){
$v = $tpl['gallery_arr'][$k];
...
}
I am newish to PHP and I am trying to cycle through an array and stop after 5 items.
I am using the following:
$images = ( $f->APIVer == "1.2.2" ) ? $images['Images'] : $images;
// Display the thumbnails and link to the medium image for each image
foreach ( $images as $index => $image) {
echo '<li><a href="'.$image['XLargeURL'].'"><img src="'.$image['TinyURL'].'" alt="thumbnail"/></li>';
}
while ( $index < 5 );
}
Although it does not seem to work...
Am I doing something wrong?
Thanks in advance
If the array has a zero based index you can do:
foreach ( $images as $index => $image) {
if ($index == 5) break;
echo '<li><a href="'.$image['XLargeURL'].'"><img src="'.$image['TinyURL'].'" alt="thumbnail"/></li>';
}
Otherwise you can add your own counter:
$i = 0;
foreach ( $images as $index => $image) {
$i++;
if ($i == 5) break;
echo '<li><a href="'.$image['XLargeURL'].'"><img src="'.$image['TinyURL'].'" alt="thumbnail"/></li>';
}
What you tried is another type of loop:
$index = 0;
do {
echo '<li><a href="'.$images[$index]['XLargeURL'].'"><img src="'.$images[$index]['TinyURL'].'" alt="thumbnail"/></li>';
$index++;
} while ( $index < 5 );
Or:
$index = 0;
while ( $index < 5 ) {
echo '<li><a href="'.$images[$index]['XLargeURL'].'"><img src="'.$images[$index]['TinyURL'].'" alt="thumbnail"/></li>';
$index++;
}
Another alternative would be a for loop:
for($index=0; $index < 5; $index++) {
echo '<li><a href="'.$images[$index]['XLargeURL'].'"><img src="'.$images[$index]['TinyURL'].'" alt="thumbnail"/></li>';
}
$images = ( $f->APIVer == "1.2.2" ) ? $images['Images'] : $images;
$nm = 0;
foreach ( $images as $index => $image) {
if($nm < 5){
echo '<li><a href="'.$image['XLargeURL'].'"><img src="'.$image['TinyURL'].'" alt="thumbnail"/></li>';
}
$nm++;
}
It should be like this:
$images = ( $f->APIVer == "1.2.2" ) ? $images['Images'] : $images;
$i = 0;
// Display the thumbnails and link to the medium image for each image
foreach ( $images as $index => $image) {
if ($i == 5) break;
echo '<li><a href="'.$image['XLargeURL'].'"><img src="'.$image['TinyURL'].'" alt="thumbnail"/></li>';
$i++;
}
while is a loop on its own, just like foreach.
Given that $index is an integer you could just break out of the loop:
foreach ($images as $index => $image) {
echo '<li><a href="'.$image['XLargeURL'].'"><img src="'.$image['TinyURL'].'" alt="thumbnail"/></li>';
if ($index >= 5) {
break;
}
}
This is what im using to loop through an array of arrays.
$csvpre = explode("###", $data);
$i = 0;
$bgc = 0;
foreach ( $csvpre AS $key => $value){
$info = explode("%%", $value);
$i++;
if($i == "1"){
echo "<tr bgcolor=#efefef><td></td>";
foreach ( $info as $key => $value ){ echo "<td>$value</td>"; }
echo "</tr>";
} else {
if($bgc=1) { $bgcgo = "bgcolor=\"#b9b9b9\"" ;} else { $bgcgo = "bgcolor=\"#d6d6d6\""; }
echo "<tr $bgcgo><td></td>";
foreach ( $info as $key => $value ){ echo "<td>$value</td>"; }
echo "</tr>";
$bgc++;
}
}
How can i add an if/elseif statement to the last foreach, so that the output changes on a given line of the array.
Say i want <td>$value</td> for all unless specified, but on line 30, i want <textarea>$value</textarea>
You mean like this:
<?php
.......
echo "<tr $bgcgo><td></td>";
$j = 0; //you need a counter
foreach ( $info as $key => $value ) {
$j++;
if ($j != 30) {
echo "<td>$value</td>";
} else {
echo "<textarea>$value</textarea>";
}
}
echo "</tr>";