Change layout of every two divs with PHP - php

I'm currently wrapping every two divs in a parent row with php, which is working great.
However what I need to do is have it so that the 3rd & 4th, 7th & 8th etc divs have a different layout. This is currently my code to wrap every two divs in a parent row.
<?php
if( have_rows('products') ) :
$counter = 1;
$i = 0;
?>
<div class="o-table l-grid-half">
<?php while( have_rows('products') ) :
the_row();
if ($i > 0 && ($i % 2 == 0)) {
// close row and open new row
?>
</div>
<div class="o-table l-grid-half">
<?php } ?>
<div class="o-table-cell l-grid-half__item ">
<div class="o-table-cell l-grid-half__item ">
<img src="<?php the_sub_field('product_image'); ?>"/>
</div>
<div class="o-table-cell o-table-vt l-grid-half__item o-full-sm-pd o-roman l-product__item o-bg-item-<?php echo($counter);?>">
<h3 class="hd-panel-sm"><?php the_sub_field('product_heading'); ?></h3>
<?php the_sub_field('product_summary'); ?>
<i class="icon-right-open"></i><span>Find out more</span>
</div>
</div>
<?php
$i++;
$counter++;
endwhile;
?>
</div>
<?php endif;
?>
Everything I have tried to wrap it into the existing code has broken the script.
The end goal I would like the html to look like this (simplified version):
<div class="o-table im-the-parent-row">
<div class="o-table-cell im-the-child-div">
<img src="img-on-the-left.jpg"/>
<p>text on the right</p>
</div>
<div class="o-table-cell im-the-child-div">
<img src="img-on-the-left.jpg"/>
<p>text on the right</p>
</div>
</div>
<div class="o-table im-the-parent-row">
<div class="o-table-cell im-the-child-div">
<p>text on the left</p>
<img src="img-on-the-right.jpg"/>
</div>
<div class="o-table-cell im-the-child-div">
<p>text on the left</p>
<img src="img-on-the-right.jpg"/>
</div>
</div>
Updated code:
<?php
if( have_rows('products') ) :
$counter = 1;
$i = 0;
?>
<div class="o-table l-grid-half">
<?php while( have_rows('products') ) :
the_row();
if ($i > 0 && ($i % 2 == 0)) {
// close row and open new row
?>
</div>
<div class="o-table l-grid-half">
<?php } ?>
<!--Row 3&4, 7&8 have the image on the left -->
<?php if($i > 0 && (($i % 4 == 0) || (($i+1) % 4 == 0))) { ?>
<div class="o-table-cell l-grid-half__item ">
<div class="o-table-cell o-table-vt l-grid-half__item o-full-sm-pd o-roman l-product__item o-bg-item-<?php echo($counter);?>">
<h3 class="hd-std-sm"><?php the_sub_field('product_heading'); ?></h3>
<?php the_sub_field('product_summary'); ?>
<i class="icon-right-open"></i><span>Find out more</span>
</div>
<div class="o-table-cell l-grid-half__item ">
<img src="<?php the_sub_field('product_image'); ?>"/>
</div>
</div>
<?php } else {?>
<!--Row 1&2, 5&6 have the image on the right -->
<div class="o-table-cell l-grid-half__item ">
<div class="o-table-cell l-grid-half__item ">
<img src="<?php the_sub_field('product_image'); ?>"/>
</div>
<div class="o-table-cell o-table-vt l-grid-half__item o-full-sm-pd o-roman l-product__item o-bg-item-<?php echo($counter);?>">
<h3 class="hd-std-sm"><?php the_sub_field('product_heading'); ?></h3>
<?php the_sub_field('product_summary'); ?>
<i class="icon-right-open"></i><span>Find out more</span>
</div>
</div>
<?php } ?>
<?php
$i++;
$counter++;
endwhile;?>
</div>
<?php endif;
?>

This code gives you a "true" when $i is multiple of 4:
($i > 0 && ($i % 4 == 0)) # -> 4rd, 8th, etc
You can then shift $i to get the 3rd and 7th itens:
($i > 0 && ($i+1) % 4 == 0) # -> 3rd, 7th, etc
EDIT - To get both cases at once:
if($i > 0 && (($i % 4 == 0) || (($i+1) % 4 == 0))) {
/* do something... when $i = 3,4,7,8,11,12,... */
}

Take one variable starts with 1
Increment with every loop... 1 2 3 4
After 4 move back to 1
When its 3 and 4 you can write your custom code.
(You can save as variable or in session as you wish)

Related

PHP logic to add a class to 2 rows every other 2 rows

Can anyone help me with the logic to add a class .col-md-offset-2 to every two rows in my code below...
<?php while(have_rows('report_quotes')): the_row(); ?>
<div class="col-md-12 <?php // help me ?>">
<?php /* quote */ ?>
<?php Post::get_template_part('sections/quote'); ?>
</div>
<?php endwhile; ?>
So the output will look like this...
<div class="col-md-12">...</div>
<div class="col-md-12">...</div>
<div class="col-md-12 col-md-offset-2">...</div>
<div class="col-md-12 col-md-offset-2">...</div>
<div class="col-md-12">...</div>
<div class="col-md-12">...</div>
<div class="col-md-12 col-md-offset-2">...</div>
<div class="col-md-12 col-md-offset-2">...</div>
<div class="col-md-12">...</div>
<div class="col-md-12">...</div>
<div class="col-md-12 col-md-offset-2">...</div>
<div class="col-md-12 col-md-offset-2">...</div>
...
Can anyone help?
Thanks
You can do this by using the modulus operator (%)
<?php $i = 0; while($i < 6): ++$i; ?>
<div class="col-md-12 <?=in_array($i % 4, array (0,3)) ? 'col-md-offset-4' : null ?>">...</div>
<?php endwhile; ?>
So in your case -
<?php $i = 0; while(have_rows()): the_row(); ++$i; ?>
<div class="col-md-12 <?=in_array($i % 4, array (0,3)) ? 'col-md-offset-4' : null ?>">
<?php /* quote */ ?>
<?php Post::get_template_part('sections/quote'); ?>
</div>
<?php endwhile; ?>
This essentially gets the remainder for each increment and in our case we know that if the remainder is 0 (divisible by 4) or a 3, then it should be indented.
Add a pointer that keeps track of the current loop index, and check if it the 3th or the 4th one in an iteration with the modulo operator.
Like this:
<?php
$i = -1;
while(have_rows('report_quotes')): the_row();
$i++;
?>
<div class="col-md-12
<?php if ( ($i % 4) == 2 || ($i % 4) == 3 ) { echo 'col-md-offset-2'; } ?> ">
<?php /* quote */ ?>
<?php Post::get_template_part('sections/quote'); ?>
</div>
<?php endwhile; ?>
Add a counter to know which row of the array you're in and use modulo:
<?php
$i = 0;
while(have_rows('report_quotes')): the_row(); ?>
<div class="col-md-12 <?php if (($i%4==2)||($i%4==3) {echo ' col-md-offset-2'; } ?> ?>">
<?php Post::get_template_part('sections/quote'); ?>
</div>
<?php endwhile; ?>

How can I display a two column array in PHP/HTML?

I have an array of icons. Right now, we only display them in buckets of 4. So if you have 7 icons, the 4th icon on the first slide will repeat as the 8th on the second slide. That's because the 3rd index of the array is stored in that same spot. To fix this, I want to loop through the array instead of explicitly iterating icon by icon.
<?php if (!empty($icons)) { // if we have icons attributes
// NOTE: we've changed it to show as many sets as we can
// (sets of 4)
$number_of_sets = ceil(count($icons) / 4);
$k=0; // for inner iteration
for ($j=0;$j < $number_of_sets; $j++) {
$slide_total ++;
?>
<div class="cf slide icon-slide">
<?php
// up to 4 icons
if (is_array($icons) && !empty($icons[$k])) {
$icon1 = $icons[$k];
$k++;
}
?>
<div class="col left">
<div class="cf icon">
<div class="col thumb">
<img src="<?=$icon1['thumb']?>" alt="<?=htmlentities($icon1['post_title'])?>" />
</div>
<div class="colR details">
<h4><?=$icon1['post_title']?></h4>
<p><?=$icon1['post_content']?></p>
</div>
</div>
<?php
// up to 4 icons
if (is_array($icons) && !empty($icons[$k])) {
$icon2 = $icons[$k];
$k++;
}
?>
<div class="cf icon">
<div class="col thumb">
<img src="<?=$icon2['thumb']?>" alt="<?=htmlentities($icon2['post_title'])?>" />
</div>
<div class="colR details">
<h4><?=$icon2['post_title']?></h4>
<p><?=$icon2['post_content']?></p>
</div>
</div>
</div>
<?php
// up to 4 icons
if (is_array($icons) && !empty($icons[$k])) {
$icon3 = $icons[$k];
$k++;
}
?>
<div class="colR right">
<div class="cf icon">
<div class="col thumb">
<img src="<?=$icon3['thumb']?>" alt="<?=htmlentities($icon3['post_title'])?>" />
</div>
<div class="colR details">
<h4><?=$icon3['post_title']?></h4>
<p><?=$icon3['post_content']?></p>
</div>
</div>
<?php
// up to 4 icons
if (is_array($icons) && !empty($icons[$k])) {
$icon4 = $icons[$k];
$k++;
}
?>
<div class="cf icon">
<div class="col thumb">
<img src="<?=$icon4['thumb']?>" alt="<?=htmlentities($icon4['post_title'])?>" />
</div>
<div class="colR details">
<h4><?=$icon4['post_title']?></h4>
<p><?=$icon4['post_content']?></p>
</div>
</div>
</div>
</div> <!-- // end icon slide -->
<?php
} // end for $j (number of sets of 4 icons
?>
My proposed solution:
<?php if (!empty($icons)) {
$num_cols = 2;
$i = 0;
$slide_items = 4;
?>
<div class="cf slide icon-slide">
<?php foreach ( $icons as $icon ) {
echo $i++%$num_cols==0 ? '</div><div class="col" style="width: 250px;">' : '';
?>
<div class="cf icon">
<div class="col thumb">
<img src="<?=$icon['thumb']?>" alt="<?=htmlentities($icon['post_title'])?>" />
</div>
<div class="colR details">
<h4><?=$icon['post_title']?></h4>
<p><?=$icon['post_content']?></p>
</div>
</div>
<?php } } // end if we have icons attributes ?>
I'm having trouble figuring out how to make another slide after I hit 4 icons. Adding the following line before the end of the foreach loop hasn't worked.
echo $i++%$slide_items==0 ? '</div><div class="cf slide icon-slide">' : '';
Here's some logic for the loop:
$i = count( $icons );
if ( $i % 4 != 0 )
$i = $i + ( $i % 4 );
for( $n = 0; $n < $i; $n++ )
{
if ( $n % 4 == 0 )
{
// code to open a row here
}
// code to open a column here
// echo your icon here, use isset() or !empty().
// code to close a column here
if ( $n > 0 && $n % 3 == 0 )
{
// code to close a row here
}
}
The IFs at the top is for keeping the column number consistent. Otherwise there'll be some weirdness at the end of the loop.

Add new row after 4th album PHP

so I am trying to add a new row after every 4th gallery and continue on until I am out of galleries to add. So if there is 17 galleries there will be 4 rows of 4 galleries and 1 row of the remaining gallery. here is an example of how it looks: http://www.csulb.edu/centers/latinohealth/media/galleries/
here is my code:
<?php $this->start_element('nextgen_gallery.gallery_container', 'container', $displayed_gallery); ?>
<div class="row-fluid secondone">
<div class="ngg-albumoverview span12">
<div class="row-fluid">
<?php $count = 0;?>
<?php foreach ($galleries as $gallery) {
$count++;
?>
<div class="ngg-album span3">
<div class="ngg-albumtitle">
<?php echo_safe_html($gallery->title); ?>
</div>
<div class="ngg-albumcontent">
<div class="ngg-thumbnail">
<a class="gallery_link" href="<?php echo nextgen_esc_url($gallery->pagelink); ?>"><img class="Thumb" alt="<?php echo esc_attr($gallery->title); ?>" src="<?php echo nextgen_esc_url($gallery->previewurl); ?>"/></a>
</div>
<div class="ngg-description">
<p><?php echo_safe_html($gallery->galdesc); ?></p>
<?php if (isset($gallery->counter) && $gallery->counter > 0) { ?>
<p><strong><?php echo $gallery->counter; ?></strong> <?php _e('Photos', 'nggallery'); ?></p>
<?php } ?>
</div>
</div>
</div>
<?php if ($count % 4 == 0 ) ?>
</div>
<div class="row-fluid">
<?php } ?>
</div>
</div>
</div>
<?php $this->end_element(); ?>
Found the problem:
the row:
<?php if ($count % 4 == 0 ) ?>
should be:
<?php if ($count % 4 == 0 ) { ?>
You need to do what you want with css styles, not with the php.
Create a container block with fixed width that can contain exacly 4 galleries and use the float property on the boxes of the galleries.

Setting an interval on do / while from 5 to 5

I've made a repeat region with dreamweaver and the result is this:
<div>
<?php do { ?>
<div class="slide">
<img src="<?php echo $row_Recordset1['imagemUrl']; ?>" />
<h2><?php echo $row_Recordset1['titleProj']; ?></h2>
</div>
<?php while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
</div>
No problem here, it works fine.
The thing is, I'm trying to use only 5 slides for each page, something like this:
<div>
<div class="page">
<?php do { ?>
<div class="slide">
<img src="<?php echo $row_Recordset1['imagemUrl']; ?>" />
<h2><?php echo $row_Recordset1['titleProj']; ?></h2>
</div>
<?php
$i = 0;
$r = $i % 5;
$i++;
if ( $r == 0 ) {
?>
</div> <!-- closes div.page -->
<div class="page"> <!-- adds a new div.page -->
<? } ?>
<?php while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
</div>
It's obviously not working, but hopefully you'll see what I'm trying to reach.
Thanks in advance!
UPDATE:
The result i get is:
<div class="page">
<div class="slide">...</div>
<div class="slide">...</div>
<div class="slide">...</div>
<div class="slide">...</div>
<div class="slide">...</div>
<div class="slide">...</div>
<div class="slide">...</div>
<div class="slide">...</div>
</div>
I would like it to be:
<div class="page">
<div class="slide">...</div>
<div class="slide">...</div>
<div class="slide">...</div>
<div class="slide">...</div>
<div class="slide">...</div>
</div>
<div class="page">
<div class="slide">...</div>
<div class="slide">...</div>
<div class="slide">...</div>
</div>
You're setting $i to 0 on every iteration. Place the $i = 0 before the do-while loop.
You should probably also place $i++ before $r = $i % 5; to get the result you require.
EDIT: You're also missing a closing brace before your while statement (typo?)
Hence, the complete code:
<div>
<div class="page">
<?php
$i = 0;
do { ?>
<div class="slide">
<img src="<?php echo $row_Recordset1['imagemUrl']; ?>" />
<h2><?php echo $row_Recordset1['titleProj']; ?></h2>
</div>
<?php
$i++;
$r = $i % 5;
if ( $r == 0 ) {
?>
</div> <!-- closes div.page -->
<div class="page"> <!-- adds a new div.page -->
<?php
}
} while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
</div>
the piece you gave is not a valid php code.
the line here:
<h2><?php echo $row_Recordset1['titleProj']; ?></h2>
opens a php tag without closing it before the next one you open.. so a first step would be to change that line to
<h2><?php echo $row_Recordset1['titleProj']; ?></h2>
also you missed closing a block, here:
<? } ?>
you should close both the if and the do/while block (so another })
another problem is the fact that on every step you change $i to 0, so every time $r will be 0 also
so in conclusion: take your problems step by step as you have more than one
<div>
<div class="page">
<?php for ($i = 0; $row_Recordset1 = mysql_fetch_assoc($Recordset1); $i++) { ?>
<div class="slide">
<img src="<?php echo $row_Recordset1['imagemUrl']; ?>" />
<h2><?php echo $row_Recordset1['titleProj']; ?></h2>
</div>
<?php if ( $i % 5 == 0 && $i ) { ?>
</div> <!-- closes div.page -->
<div class="page"> <!-- adds a new div.page -->
<?php } ?>
<?php } ?>
</div>
</div>
Something like this would loop through 5 times adding a new <div>content</div> to the variable $html. You can then echo the variable in your html body.
$html = "";
for ($i=0;$i<5;$i++) {
$html .= "<div class=\"page\">". $content[$i] . "</div>\n";
}
Additions:
<?PHP
while ($row_Recordset = mysql_fetch_assoc($Recordset1)) {
$html .= "<div class='page'>\n";
for ($i=0;$i<5;$i++) {
$html .= "<div class='slide'>". $row_Recordset[$i]['column'] . "</div>\n";
}
$html .= "</div>\n";
}
?>
This code will loop through the recordset populating $html on each iteration. Try echo $html and you will have an output structured as you want.

While loop problem in PHP

I have my markup structure as below:
<div>
<div>value1</div>
<div>value2</div>
<div>value3</div>
<div>value4</div>
<div class="clear"></div>
</div>
<div>
<div>value5</div>
<div>value6</div>
<div>value7</div>
<div>value8</div>
<div class="clear"></div>
</div>
I have my data in a PHP result set, let's say I have 9 records so the structure should be as below:
<div>
<div>value1</div>
<div>value2</div>
<div>value3</div>
<div>value4</div>
<div class="clear"></div>
</div>
<div>
<div>value5</div>
<div>value6</div>
<div>value7</div>
<div>value8</div>
<div class="clear"></div>
</div>
<div>
<div>value9</div>
<div class="clear"></div>
</div>
So, the while loop should run in a way so that it will print the parent div after 4 records printed successfully. But in above I have 9 records so it should close the dive if its the last record.
Please help, Thanks!
The preconfig...
<?php
$num_of_results = sizeof($your_array);
$loops = ceil($num_of_results/4);
$k = 0;
?>
In your web
<?php for($p = 0; $p < $loops; $p++) { ?>
<div>
<div>
<?php for($i = 0; $i < 4 && $k < $num_of_results; $i++) { ?>
<div><?php echo $your_array[$k]; $k++;?></div>
<?php } ?>
<div class="clear"></div>
</div>
</div>
<?php } ?>
That's your problem isnt it?
By getting some idea from logic given here I tried following and it works.
<div> <!-- started main div -->
<?php
$icount = 1;
$itotal = mysql_num_rows($result_rs);
while ($rs = mysql_fetch_array($result_rs)) {
echo '<div>'.$rs['value'].'</div>';
if ($icount % 4 == 0 && $icount != $itotal){
echo '<div class="clear"></div>';
echo '</div>'; //closed main div
echo '<div>'; //started new main div
}
$icount++;
}
?>
</div> <!-- closed main div -->
That, solved my problem.
Edited: added itotal condition, so when you will have only 4 records per page then also this will work properly.
Right, now I know what you're after. I've done this before when showing items in a grid and you need to break each row because of that browser.
Anyway, it's ugly but I don't think it gets any easier than this
<?php for ($i = 0, $total = count($resultSet); $i < $total; $i += 4) : ?>
<div>
<?php
for ($j = $i; $j < ($i + 4); $j++) :
if (!isset($resultSet[$j])) :
?>
<div class="clear"></div>
</div>
<?php break 2; endif ?>
<div><?php echo htmlspecialchars($resultSet[$j]) ?></div>
<?php endfor ?>
<div class="clear"></div>
</div>
<?php endfor ?>
<div>
<?php for ($i = 1; $i <= 9; $i++): ?>
<?php if ($i%4 == 1 && $i != 1): ?>
<div class="clear"></div>
</div>
<div>
<?php endif; //$i%4 == 1 && $i != 1 ?>
<div>Value <?php echo $i ?></div>
<?php endfor; //$i = 1; $i <= 9; $i++ ?>
<div class="clear"></div>
</div>
or with an array:
<div>
<?php foreach ($arr as $k=>$v): ?>
<?php if (($k+1)%4 == 1 && $k != 0): ?>
<div class="clear"></div>
</div>
<div>
<?php endif; //($k+1)%4 == 1 && $k != 0 ?>
<div><?php echo $v ?></div>
<?php endforeach; //$arr as $k=>$v ?>
<div class="clear"></div>
</div>
or with a mysqli resultset:
<div>
<?php $count = 1 ?>
<?php while ($row = mysqli_fetch_array($result)): ?>
<?php if ($count%4 == 1 && $count != 1): ?>
<div class="clear"></div>
</div>
<div>
<?php endif; //$count%4 == 1 && $count != 1 ?>
<div><?php echo $row['value'] ?></div>
<?php $count++ ?>
<?php endwhile; ?>
<div class="clear"></div>
</div>
Why not use modulo to "close" a div?
<div>
<?php foreach($data as $key => $value) : ?>
<div><?php echo $value ?></div>
<?php if($key % 4 == 0 && $key != 0) : // add a clearing div, close the first group and open another one ?>
<div class="clear"></div>
</div>
<div>
<? endforeach ?>
<?php if($key % 4 != 0) : // div has not been closed as the number of records % 4 was not equal 0 ?>
<div class="clear"></div>
</div>
<? endif ?>

Categories