Creating row using php loop - php

I want to create a row if columns greater than 3 using PHP loop because i am using wordpress
My code is here
<div class="row">
<div class="column">column1</div>
<div class="column">column2</div>
<div class="column">column3</div>
</div>
If columns are greater than 3, then it should create a new row like this
<div class="row">
<div class="column">column1</div>
<div class="column">column2</div>
<div class="column">column3</div>
</div>
<div class="row">
<div class="column">column1</div>
</div>
Any help will be highly appreciated.
Thanks in advance

Sure - just use modulus:
<?php
$elements = array('foo', 'bar', 'rab', 'oof');
echo '<div class="row">';
foreach ($elements as $i => $element) {
if ($i > 0 && $i % 3 == 0) {
echo '</div><div class="row">';
}
echo '<div class="column">' . $element . '</div>';
}
echo '</div>';
?>
DEMO
Output:
<div class="row">
<div class="column">foo</div>
<div class="column">bar</div>
<div class="column">rab</div>
</div>
<div class="row">
<div class="column">oof</div>
</div>

You need something like this:
<?
echo '<div class="row">';
for ($i=0; $i<15;$i++){
if ($i%3 == 0 && $i != 0){
echo '</div><div class="row">';
}
echo '<div class="column">column '.($i+1).'</div>';
}
echo '</div>';
?>
WORKING CODE

There is alternative solution with function.
<?php
// Managing the Code -- Functions: Handling a Variable Number of Parameters
// building a row of 10 <td> columns with a variable number of items
function preferencesRow()
{
// initialize $output
$output = '';
// use "func_get_args()" to collect all parameters into an array
$params = func_get_args();
// used to make sure 10 columns are filled
$maxCols = 10;
// loop through the parameters
foreach ($params as $item) {
$output .= "<td width='80px' align='center'>$item</td>\n";
$maxCols--;
}
// fill in the rest of the row with empty columns
for ($x = 0; $x < $maxCols; $x++) {
$output .= "<td width='80px'> </td>\n";
}
return $output;
}
// NOTE: you can use "." or "," with echo
echo '<h1>Preferences</h1><hr />' . PHP_EOL;
echo '<table border=1>' . PHP_EOL;
echo '<tr><th>Tarzan</th>';
echo preferencesRow('Africa', 'jungles', 'tantor', 'mangani', 'cabin in the woods',
'hunting', 'swinging in trees', 'Jane');
echo '</tr>';
echo '<tr><th>Jane</th>';
echo preferencesRow('London', 'parties', 'dancing', 'social events', 'lavish estates');
echo '</tr>';
echo '</table>' . PHP_EOL;

Related

Dynamically building bootstrap row, 3 per row

I am not sure the best way to go about this, on a site i'm building (using bootstrap) i want to have 3 cards per row using the grid system like:
HTML:
<div class="container">
<div class="row">
<div class="col-sm">
One of three columns
</div>
<div class="col-sm">
One of three columns
</div>
<div class="col-sm">
One of three columns
</div>
</div>
</div>
Which is no problem normally, but i'm building it (or trying to) dynamically:
PHP:
<main>
<br /><br /><br />
<?php
$pages = array_slice(scandir($_SERVER['DOCUMENT_ROOT']), 2);
$mixed = shuffle($pages);
$count = 0;
echo '<div class="container"><div class="row">';
foreach($pages as $page)
{
$count++;
if (strpos($page, '.php') !== false && $page != 'index.php') {
$html = file_get_contents($page);
$code = explode("|", extractXvideos($html));
?>
<div class="col-md-4">
<div class="card" style="width: 18rem;">
<img src="<?= $code[3]; ?>" class="card-img-top" alt="<?= $code[0]; ?>">
<div class="card-body">
<p class="card-text"><?= substr($code[0], 0, 25); ?> ...</p>
</div>
</div>
</div>
<?php
if ($count == 18) {
// The reason only 15 is showing is because we ignore ".", ".." & "index.php".
break;
}
}
}
echo '</div></div>';
?>
</main>
For this project i'm scanning .php pages on the server, then trying to lay them out 3 per row, so after every row of 3 i need to start a new row echo '<div class="container"><div class="row">'; from what i can see, i do not know the best way to go about this, any help would be appreciated.
main
<?php
$pages = array_slice(scandir($_SERVER['DOCUMENT_ROOT']), 2);
$mixed = shuffle($pages);
$count = 0;
$output = '';
foreach($pages as $page) {
$count++;
if (strpos($page, '.php') !== false && $page != 'index.php') {
$html = file_get_contents($page);
$code = explode("|", extractXvideos($html));
$output .= '<div class="container">';
$output .= '<div class="row">';
$output .= '<div class="col">Column</div>';
$output .= '<div class="col">Column</div>';
$output .= '<div class="col">Column</div>';
$output .= '</div>
$output .= '</div>';
}
}
echo $output;
?>
main
try this template and apply your conditions.

How to display div only once in the loop and next div display inside div which run only once

I try to found the solution on google and SO but I haven't got solution.
I have a code something like.
$index = 0;
while (some condition here) {
if ($index < 4) {?>
<div class="first4">
<p>Some text here</p>
</div>
<?php }
else{
$check=0;
if ($check==0){?>
<div class="displayOnceInwhile">
<?php $check=1; }?>
<div class="InsideaboveClass"></div>
<?php }
$index++;}?>
What I am doing with the above code is, if $index is less then 4 then the inner text will display else $check will run only once in the loop but it's not working. Also, Notice here I confused where should I closed the displayOnceInwhile closing </div>.
Expected result
<!--first 4 will display-->
<div class="first4"><p>Some text here</p></div>
<div class="first4"><p>Some text here</p></div>
<div class="first4"><p>Some text here</p></div>
<div class="first4"><p>Some text here</p></div>
<!--Set will display like this-->
<div class="displayOnceInwhile">
<div class="InsideaboveClass"></div>
</div>
Hope This is what you are trying to do is.
<?php
$check = 0;
$index = 0;
while (some condition here) {
if ($index < 4) {
echo '<div class="first4"><p>Some text here</p></div>';
} else {
if ($check==0){
echo '<div class="displayOnceInwhile">';
$check=1;
}
echo '<div class="InsideaboveClass"></div>';
}
$index++;
}
echo '</div>';
?>
You could use following to build your HTML
<?php
$first = '<div class="first4" ><p > Some text here </p ></div >';
$firstOpen = '<div class="first4" ><p > Some text here </p >';
$firstClose = '</div>';
$once = '<div class="displayOnceInwhile"><div class="InsideaboveClass"></div>';
$index = 0;
while ($index < 3) {
echo $first;
$index++;
}
echo $firstOpen;
echo $once;
echo $firstClose;

Group items by 3 in for each loop

I have an array of items and want to loop through them but instead of grouping them inside of a div I would like to group them by css Class.
I dont want to do div grouping because of mobile views there might be 4 or more in line and the breaks css class will change as the screen size changes or will be moved to absolute positioning removing the break all together and forming a grid.
I can create the breaks every 3 items but the class assign im struggling with.
Any help would be appreciated.
I would like to create the below layout in a PHP foreach loop.
ex.
<div class="listing">
<div class="group_1">item1</div>
<div class="group_1">item2</div>
<div class="group_1">item3</div>
<div class="break_1"></div>
<div class="group_2">item4</div>
<div class="group_2">item5</div>
<div class="group_2">item6</div>
<div class="break_2"></div>
<div class="group_3">item7</div>
<div class="group_3">item8</div>
<div class="group_3">item9</div>
<div class="break_3"></div>
<div class="group_4">item10</div>
<div class="group_4">item11</div>
<div class="group_4">item12</div>
<div class="break_4"></div>
</div>
Not very sure if it is what you are looking, anyway, given a basic array like this:
$arr=array(1,2,3,4,5,6,7,8,9,10,11,12);
you can loop it and have the code you expect this way:
$group_num=1;
echo "<div class='listing'>";
foreach ($arr as $key=>$item) {
echo "<div class='group_$group_num'>$item</div>";
if ($key%3==0) {
echo "<div class='break'></div>";
$group_num++;
}
}
echo "</div>";
Results:
<div class='listing'>
<div class='group_1'>1</div>
<div class='group_1'>2</div>
<div class='group_1'>3</div>
<div class='break'></div>
<div class='group_2'>4</div>
<div class='group_2'>5</div>
<div class='group_2'>6</div>
<div class='break'></div>
<div class='group_3'>7</div>
<div class='group_3'>8</div>
<div class='group_3'>9</div>
<div class='break'></div>
<div class='group_4'>10</div>
<div class='group_4'>11</div>
<div class='group_4'>12</div>
<div class='break'></div>
if we have array of items as :
$items = ['item1', 'item2', 'item3', 'item4', 'item5', 'item6', 'item7', 'item8'];
$result = '';
foreach($items as $index => $item) {
$groupNumber = intval(floor($index/3)) + 1;
$result .= "<div class='group_$groupNumber'>$item</div>";
}
echo $result;
and this is a sample output
Thanks to the help of Giacomo Pittalis and Hassan Ali Salem I have managed to solve this in a bit of a crude way.
<?php
$items = ['item1', 'item2', 'item3', 'item4', 'item5', 'item6', 'item7', 'item8', 'item9', 'item10'];
$i = 0;
$len = count($items);
foreach ($items as $item) {
$mod = intval(floor($i / 3) + 1);
echo '<div class="' . 'group_' . $mod . '">' . $items[$i] . '</div>';
if (($i % 3 == 2)) { ?>
<div class="content_holder hidden" id="group_<?php echo $mod; ?>"></div>
<?php }
if ($i == $len - 1) { ?>
<div class="content_holder hidden" id="group_<?php echo $mod; ?>"></div>
<?php }
$i++;
}
?>

Iterating over 12 items specify markup up on nth items

Hi I am using Bootstrap 2 with a PHP content management system.
I am rendering 12 items from the database, each 3 items needs to be wrapped in a row. However I am unable to achieve this my last attempt is below (with simplified markup):
$i = 1;
echo "<div class='row-fluid'>";
foreach($posts as $p) {
if ($i % 3 == 0) {
echo "</div>";
}
if ($i % 4 == 0) {
echo "<div class='row-fluid'>";
}
echo "<div class='span4'><h5>$p->title</h5></div>";
$i++;
}
In affect what I am looking for is something like this:
<div class="row">
<div class="item></item>
<div class="item"</item>
<div class="item"></item>
</div>
<div class="row">
<div class="item></item>
<div class="item"</item>
<div class="item"></item>
</div>
<div class="row">
<div class="item></item>
<div class="item"</item>
<div class="item"></item>
</div>
<div class="row">
<div class="item></item>
<div class="item"</item>
<div class="item"></item>
</div>
I have tried everything I can think of any help would be great thanks.
Try if there are 12 rows :
echo "<div class='row-fluid'>";
foreach($posts as $p) {
echo "<div class='span4'><h5>$p->title</h5></div>";
if ($i % 3 == 0) {
echo "</div>";
echo ( $i< 12 )? "<div class='row-fluid'>" : "";
}
$i++;
}
I think this will work
$i = 0;
echo "<div class='row-fluid'>";
foreach($posts as $p) {
echo "<div class='span4'><h5>$p->title</h5></div>";
if ($i % 3 == 0) {
echo "</div><div class='row-fluid'>";
}
$i++;
}
echo "</div>";

PHP loop modulo

I would like to make this in a loop:
<div class="global">
<div class="left">1</div>
<div class="right">2</div>
</div>
<div class="global">
<div class="left">3</div>
<div class="right">4</div>
</div>
<div class="global">
<div class="left">5</div>
<div class="right">6</div>
</div>
<div class="global">
<div class="left">7</div>
<div class="right">8</div>
</div>
<div class="global">
<div class="left">9</div>
<div class="right">10</div>
</div>
I know do something link this :
for($i=0;$i<4;$i++){
if($i %2){
$classe='class="right"';
}
else{
$classe='class="left"';
}
echo "<div ".$classe." >".$i."</div>";
}
which result:
<div class="left">1</div>
<div class="right">2</div>
<div class="left">3</div>
<div class="right">4</div>
How Can I integrate the div "global" between ?
Thanks a lot for your help
Iterate two by two:
for ($i = 1; $i <= 4; $i += 2) {
echo '<div class="global">';
echo '<div class="left">' . $i . '</div>'
echo '<div class="right">' . ($i+1) . '</div>'
echo '</div>';
}
You just need a little extra...
echo '<div class="global">'; //start the first global div
for($i=0;$i<4;$i++){
if($i %2){
$classe='class="right"';
}
else{
$classe='class="left"';
}
echo "<div ".$classe." >".$i."</div>";
if($i %2)
{
//after each "right" div, close and open a new global div
echo "</div>\n<div class=\"global\">";
}
}
echo '</div>'; //close the final global div
You can also compact the whole thing a bit:
echo '<div class="global">'; //start the first div
for($i=0;$i<4;$i++)
{
if($i %2)
{
echo "<div class=\"right\" >$i</div>\n</div>\n<div class=\"global\">";
}
else
{
echo "<div class=\"right\" >$i</div>";
}
}
echo '</div>'; //close the final global div
I like printf.
$i=1;
while ($i < 8) {
printf('<div class="global"><div class="left">%d</div><div class="right">%d</div></div>', $i++, $i++);
}
Edit:
Although this doesn't really answer the OP question, and none of the other answers (yet) use modulus as per the question title. So, here is another far more ugly way :)
echo '<div class="global">';
for($i=0;$i<8;$i++){
if ($i %2) {
$classe='right';
$sep='</div><div class="global">';
}
else{
$classe='left';
$sep='';
}
printf('<div class="%s">%d</div>%s', $classe, $i+1, $i<7?$sep:'');
}
echo '</div>';
This code works :
echo '<div class="global">'; //start the first global div
for($i=0;$i<7;$i++){
if($i %2){
$classe='class="right"';
}
else{
$classe='class="left"';
}
echo "<div ".$classe." >".$i."</div>";
if($i %2)
{
//after each "right" div, close and open a new global div
echo "</div>\n<div class=\"global\">";
}
}
echo '</div>';

Categories