PHP Loop on every value on multidimensional array X times [duplicate] - php

I have a foreach statement in my app that echos a list of my database results:
<?php
foreach($featured_projects as $fp) {
echo '<div class="result">';
echo $fp['project_name'];
echo '</div>';
}
?>
I would like to:
On every third result, give the div a different class. How can I achieve this?

You can use a counter and the modulo/modulus operator as per below:
<?php
// control variable
$counter = 0;
foreach($featured_projects as $fp) {
// reset the variable
$class = '';
// on every third result, set the variable value
if(++$counter % 3 === 0) {
$class = ' third';
}
// your code with the variable that holds the desirable CSS class name
echo '<div class="result' . $class . '">';
echo $fp['project_name'];
echo '</div>';
}
?>

<?php
foreach ($featured_projects as $i => $fp) {
echo '<div class="result' . ($i % 3 === 0 ? ' third' : '') . '">';
echo $fp['project_name'];
echo '</div>';
}
?>

If the $featured_projects array is based on incremental index you could simply use the index and the modulo % operator.
Otherwise you would have to add a counter.
http://php.net/manual/en/language.operators.arithmetic.php

add a counter in this loop and check if counter equals three and apply class.

Using a counter and modulo operator this is easy to implement

<?php
foreach($featured_projects as $fp) {
if(++$i % 3 === 0) {
$class = ' something';
} else {
$class = '';
}
echo '<div class="result' . $class . '">';
echo $fp['project_name'];
echo '</div>';
}
?>

<?php
$i = 0;
foreach($featured_projects as $fp) {
echo '<div class="'.($i++%3 ? 'result' : 'other_class').'">';
echo $fp['project_name'];
echo '</div>';
}
?>

What leaves your code mostly in tact would be
<?php
$i = 1;
foreach($featured_projects as $fp) {
printf ('<div class="%s">',(($i % 3) ? "result" : "result_every_third" ));
echo $fp['project_name'];
echo '</div>';
$i++;
}
?>
But you may want to consider using a for or while construct around "each($featured_projects)" (see http://php.net/manual/en/function.each.php) which may result in neater code.

<?php
$counter = 0;
foreach ($featured_projects as $fp) {
echo '<div class="result' . ($counter++ % 3 === 0 ? ' third' : '') . '">';
echo $fp['project_name'];
echo '</div>';
}
?>

You can add a counter in loop ...try the following...
<?php
$i = 0;
foreach($featured_projects as $fp) {
$i = ++$i;
if(($i%3) == 0)
{
$class1 = 'test1';
}
else
{
$class1 = 'test2';
}
echo '<div class="'.$class1.'">';
echo $fp['project_name'];
echo '</div>';
}
?>

This is the working version, sorry for my prev version:
<?php
$featured_projects[0]['project_name'] = "pippo";
$featured_projects[1]['project_name'] = "pippo2";
$featured_projects[2]['project_name'] = "pippo3";
$class[0] = "class1";
$class[1] = "class2";
$i=0;
foreach($featured_projects as $fp) {
$i++;
if ($i == 3) {
$c = $class[1];
$i=0;
} else {
$c = $class[0];
}
echo "<div class=\"$c\">";
echo $fp['project_name'];
echo "</div>\n";
}
?>
Produces:
<div class="class1">pippo</div>
<div class="class1">pippo2</div>
<div class="class2">pippo3</div>

Related

PHP - How to wrap every 2 items of a 'for' loop within a div

I'm looking for a way to wrap every too items of this very basic for loop within a div :
<?php
for( $i=1; $i<=50; $i++ )
{
echo "<div><a href='item-".$i."'>".$i."</a></div>";
}
?>
This produces the following :
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
The output i need would be :
<div>1 2</div>
<div>3 4</div>
Thanks
Easiest way i can think of is by doing this. This also works if you have a resultset from a database, or an array of item objects, just replace the range() function with the array.
<?php
foreach (array_chunk(range(1, 50), 2) as $chunk) {
echo "<div>";
foreach ($chunk as $itemId) {
echo "<a href='item-" . $itemId . "'>" . $itemId . "</a>";
}
echo "</div>" . PHP_EOL;
}
Use modulo operator
code:
<?php
$chunks = 2;
$display = true;
for( $i=1; $i<=6; $i++ )
{
if ($display && ($i % $chunks || $chunks === 1)) {
echo "<div>";
$display = false;
$last = true;
}
echo "<a href='item-".$i."'>".$i."</a>";
if (!($i % $chunks)) {
echo "</div>" . PHP_EOL;
$display = true;
$last = false;
}
}
if ($last) {
echo "</div>" . PHP_EOL;
}
generates:
<div><a href='item-1'>1</a><a href='item-2'>2</a></div>
<div><a href='item-3'>3</a><a href='item-4'>4</a></div>
<div><a href='item-5'>5</a><a href='item-6'>6</a></div>
and with:
$chunks = 3;
you will get:
<div><a href='item-1'>1</a><div><a href='item-2'>2</a><a href='item-3'>3</a></div>
<div><a href='item-4'>4</a><div><a href='item-5'>5</a><a href='item-6'>6</a></div>
and so one.

How do I count elements in a while loop and apply css class?

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

PHP : Setting first element active with do/while loop

Here is my code snippet:
for($x=0;$x<=$flaga1;$x++){
$index++;
echo "<div class=\"item\" data-slide-number=".$index.">".$sth."</div>";
}
It iterates the code depending on $sth. But I want to add active class to the first occurence of the code, so it will be:
<div class="active item" data-slide-number="0">"sth"</div>
<div class="item" data-slide-number="1">"sth"</div>
<div class="item" data-slide-number="2">"sth"</div>
I'm trying to do something like this, but it doesn't work:
for($x=0;$x<=$flaga1;$x++){
$active=0;
$divclass="<div class=\"item\"";
do {
$divclass="<div class=\"active item\"";
$active++;
} while ($active<=0);
$index++;
echo "".$divclass." data-slide-number=".$index.">".$sth."</div>";
}
As per code submitted by you. You can try this.
for($x=0;$x<=$flaga1;$x++){
$active = "";
if($x === 0 ) {
$active = "active";
}
$index++;
echo "<div class=\"item\ <?php echo $active; ?>" data-slide-number=".$index.">".$sth."</div>";
}
Or you can do this, as suggested by Niet the Dark Absol.
for($x=0;$x<=$flaga1;$x++){
$index++;
echo "<div class=\"item\ <?php if($x === 0) {echo "active"; } ?>" data-slide-number=".$index.">".$sth."</div>";
}
You can also set this up in your for loop definition if you like:
for ($x = 0, $a = 'active'; $x < $flaga1; $x++, $a = '') {
echo "<div class=\"item $a\" data-slide-number=". ++$index .">" . $sth . "</div>\n";
}

start end div after some elements php

I cannot solve problem with starting ending divs after couple of elements from array.
What i want to get is something like that:
<div>
element1
element2
element3
element4
</div>
<div>
element5
element6
element7
element8
</div>
<div>
element9
element10
</div>
Here is my php code:
$array = array("element1","element2","element3","element4","element5","element6","element7","element8","element9","element10");
$perRow = 4;
$count = 1;
foreach ($array as $arr){
// here div needs to start, use 4 elements from array and close
if($count % $perRow == 0 OR $count == 1){
echo '<div>';
}
echo $arr . '<br>';
// here should div close
$count++;
}
Try something like this
$array = array("element1","element2","element3","element4","element5","element6","element7","element8","element9","element10");
$perRow = 4;
$count = 0;
echo '<div>';
foreach ($array as $arr){
// here div needs to start, use 4 elements from array and close
if($count % $perRow == 0 && $count!=0){
echo '</div><div>';
}
echo $arr . '<br>';
// here should div close
$count++;
}
echo '</div>';
Okay I am not familiar with arrays and maybe something like this would work:
$array = array("element1","element2","element3","element4","element5","element6","element7","element8","element9","element10");
$i=0;
echo '<div>'
if (i<3) {
echo '$array[$i]';
$i++;
}
echo '</div>';
echo '<div>';
if ($i>3 && $i<7) {
echo '$array[$i]';
$i++;
}
echo '</div>';
echo '<div>';
if ($i>7 && $i<10) {
echo '$array[$i]';
$i++;
}
echo '</div>';

PHP and MySQL Loop help?

Let's say I have a user that entered 12 links into the database but when they are displayed I want to break up the links into groups of 3 and repeat the groups of three until all 12 kinks are displayed for example.
<div>
<p>Link 1</p>
<p>Link 2</p>
<p>Link 3</p>
</div>
Here is part of the code I'm working with. How should my code look like?
while ($row = mysqli_fetch_assoc($dbc)) {
if (!empty($row['category']) && !empty($row['url'])) {
echo '<div>';
echo '<p>' . $row['category'] . '</p>';
echo '</div>';
}
}
Something like this gets you three links per div. We add the extra conditional echo at the end for the case that there's not a multiple of 3 links
$ctr = 0;
while ($row = mysqli_fetch_assoc($dbc)) {
if (!empty($row['category']) && !empty($row['url'])) {
if ($ctr%3 == 0) {
echo '<div>';
}
$ctr ++;
echo '<p>' . $row['category'] . '</p?';
if ($ctr%3 == 0) {
echo '</div>';
}
}
}
if ($ctr%3 != 0) {
echo '</div>';
}
if you want to avoid incrementing/resetting i every time you reach three, you can just use the modulus operator:
http://php.net/manual/en/language.operators.arithmetic.php
Something like:
if( $i%3 == 0 ){
// divisible by 3...
}
An easier method would be more like:
while(true) {
$c = 0;
echo "<div>";
while ($row = mysqli_fetch_assoc($dbc) && $c++ < 3) {
if (!empty($row['category']) && !empty($row['url'])) {
echo '<p>' . $row['category'] . '</p>';
}
}
echo "</div>";
if(empty($row))
break;
}

Categories