PHP loop place every 3rd loop in row - php

I want to add 3 divs in a row through a loop in PHP.
For example:
<div class="container">
<div class="row">
<div class="col-md-4"> Item 1 </div>
<div class="col-md-4"> Item 2 </div>
<div class="col-md-4"> Item 3 </div>
</div>
<div class="row">
<div class="col-md-4"> Item 4 </div>
<div class="col-md-4"> Item 5 </div>
<div class="col-md-4"> Item 6 </div>
</div>
</div>
I did a few searches on Stackoverflow but I didn't find a good solution. Who can help me? This is my PHP code so far, but as you see, it place only 1 item in a row
for($i = 0; $i < $limit; $i++) {
if ($i % 3 == 0) {
$content .= "<div class='row'>";
}
$content.= "<div class='col-md-4 col-sm-6'>Item ". $i ."</div></div>";
if ($i % 3 == 0) {
$content .= "</div>";
}
}

You can use nested foreach loops instead if you put your numbers into a multidimensional array.
foreach (array_chunk(range(1, $limit), 3) as $row) {
$content .= "<div class='row'>";
foreach ($row as $i) {
$content .= "<div class='col-md-4 col-sm-6'>Item ". $i ."</div>";
}
$content .= "</div>";
}
array_chunk(range(1, $limit), 3) will produce an array like [[1, 2, 3], [4, 5, 6]]. This will use slightly more memory, but I prefer the simpler code.

This will do it and cope with odd numbers in $limit
$limit = 9;
$content = '';
for($i = 0; $i < $limit; $i++) {
if ($i % 3 == 0) {
$content .= "\n<div class='row'>";
}
// remove the extra `</div>` from this line
//$content.= "<div class='col-md-4 col-sm-6'>Item ". $i ."</div></div>";
$content.= "\n\t<div class='col-md-4 col-sm-6'>Item$i</div>";
if ($i % 3 == 2) {
$content .= "\n</div>";
}
}
//cope with odd numbers in $limit
if ( $i%3 != 0) {
$content .= "\n</div>";
}
echo $content;
Result:
<div class='row'>
<div class='col-md-4 col-sm-6'>Item0</div>
<div class='col-md-4 col-sm-6'>Item1</div>
<div class='col-md-4 col-sm-6'>Item2</div>
</div>
<div class='row'>
<div class='col-md-4 col-sm-6'>Item3</div>
<div class='col-md-4 col-sm-6'>Item4</div>
<div class='col-md-4 col-sm-6'>Item5</div>
</div>
<div class='row'>
<div class='col-md-4 col-sm-6'>Item6</div>
<div class='col-md-4 col-sm-6'>Item7</div>
<div class='col-md-4 col-sm-6'>Item8</div>
</div>
Or with $limit set to an odd number like 5
<div class='row'>
<div class='col-md-4 col-sm-6'>Item 1</div>
<div class='col-md-4 col-sm-6'>Item 2</div>
<div class='col-md-4 col-sm-6'>Item 3</div>
</div>
<div class='row'>
<div class='col-md-4 col-sm-6'>Item 4</div>
<div class='col-md-4 col-sm-6'>Item 5</div>
</div>

Related

Why doesn't this br tag do anything

I have a PHP loop listing results from a database:
What I have done is write an if statement in PHP so that every time 4 results have been outputted, execute a line of code. I then put a break tag inside this loop so that every 4 results, there would be a break <br /> tag. It looks like this:
if ($i %4 == 0){
echo "<br />";
echo $i;
}
When I look at the source code of the site the <br /> tag is there, but it doesn't move the rest of the information to another line. When I add a different line of code it shows, for example, if I print <p>Hello</p>, it outputs 'Hello'.
It just seems to be the <br /> that doesn't work. This results in all the results after the first 4 being off the end of the screen.
Here is the whole page and a screenshot of the output:
<section class="hero is-dark is-halfheight is-bold">
<div class="hero-head">
</div>
<div class="hero-body">
<div class="container has-text-centered">
<div class="columns">
<?php
$i = 0;
foreach($_SESSION['all'] as $result) {
echo '<div class="column is-3">';
echo '<a href="#">';
echo '<div class="box has-text-centered">';
echo $result;
echo '</div>';
echo '</a>';
echo '</div>';
$i++;
if ($i %4 == 0){
echo "<br />";
echo $i;
}
}
?>
</div>
</div>
</div>
</section>
And...
My best bet is because your CSS styling overrides br behavior. Take a look below snippet (open in full page), multiple brs doesn't do anything.
I presume you're using bulma CSS, and not going to talk about why does it work since there already well explained docs written.
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.6.1/css/bulma.min.css">
<section class="hero is-dark is-halfheight is-bold">
<div class="hero-body">
<div class="container has-text-centered">
<div class="columns">
<div class="column is-3">
<div class="box has-text-centered">
Box 1 1
</div>
</div>
<div class="column is-3">
<div class="box has-text-centered">
Box 1 2
</div>
</div>
<div class="column is-3">
<div class="box has-text-centered">
Box 1 3
</div>
</div>
<br>
<br>
<br>
<br>
<div class="column is-3">
<div class="box has-text-centered">
Box 2 1
</div>
</div>
<div class="column is-3">
<div class="box has-text-centered">
Box 2 2
</div>
</div>
<div class="column is-3">
<div class="box has-text-centered">
Box 2 3
</div>
</div>
</div>
</div>
</div>
</section>
Instead, wrap those column within another columns container. Just like below,
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.6.1/css/bulma.min.css">
<section class="hero is-dark is-halfheight is-bold">
<div class="hero-body">
<div class="container has-text-centered">
<div class="columns">
<div class="column is-3">
<div class="box has-text-centered">
Box 1 1
</div>
</div>
<div class="column is-3">
<div class="box has-text-centered">
Box 1 2
</div>
</div>
<div class="column is-3">
<div class="box has-text-centered">
Box 1 3
</div>
</div>
</div>
<div class="columns">
<div class="column is-3">
<div class="box has-text-centered">
Box 2 1
</div>
</div>
<div class="column is-3">
<div class="box has-text-centered">
Box 2 2
</div>
</div>
<div class="column is-3">
<div class="box has-text-centered">
Box 2 3
</div>
</div>
</div>
</div>
</div>
</section>
Then so, you're PHP code would likely to be as follow,
<section class="hero is-dark is-halfheight is-bold">
<div class="hero-body">
<div class="container has-text-centered">
<?php
$i = 0;
$nextClosingDivShouldBePrinted = [];
foreach($_SESSION['all'] as $result) {
if ($i % 4 == 0){
echo '<div class="columns">';
$nextClosingDivShouldBePrinted[] = $i + 3; // there will be 3 'column' to wrap in for modulus 4.
}
echo '<div class="column is-3">';
echo '<a href="#" class="box has-text-centered">';
echo $result;
echo '</a>';
echo '</div>';
if (in_array($i, $nextClosingDivShouldBePrinted)) {
echo '</div>';
}
$i++; //always put loop increment at lowest part.
}
?>
</div>
</div>
</section>
As mentioned in the comments, Br will not work, you will have to wrap your code in a row div like so:
<section class="hero is-dark is-halfheight is-bold">
<div class="hero-head">
</div>
<div class="hero-body">
<div class="container has-text-centered">
<div class="columns">
<?php
$i = 0;
foreach($_SESSION['all'] as $result) {
if ($i %4 == 0){
echo '<div class="row">';
echo $i;
}
echo '<div class="column is-3">';
echo '<a href="#">';
echo '<div class="box has-text-centered">';
echo $result;
echo '</div>';
echo '</a>';
echo '</div>';
$i++;
if ($i %4 == 0){
echo "</div>";
echo $i;
}
}
?>
</div>
</div>
</div>
</section>
<style>
.row {
margin-bottom: 15px;
}
</style>

While fetch row in PHP

I have that
<?php
$new=get_records("tbl_item","status=1 AND idshop='{$idshop}' AND special","id DESC", $startRow.",".$pageSize, " ");
$dem=1;
while($row_new=mysql_fetch_assoc($new)){
?>
<div class=""row>
<div class="col-3">
</div>
</div>
<?php } ?>
I want when this code run, it'll display 1 row with 4 col-3 until the end of the loop.
I want output like this
<div class=""row>
<div class="col-3">
</div>
<div class="col-3">
</div>
<div class="col-3">
</div>
<div class="col-3">
</div>
</div>
<div class=""row>
<div class="col-3">
</div>
<div class="col-3">
</div>
<div class="col-3">
</div>
<div class="col-3">
</div>
</div>
Please help me! Thanks all!
This should work:
<?php
$new=get_records("tbl_item","status=1 AND idshop='{$idshop}' AND special","id DESC", $startRow.",".$pageSize, " ");
$i=0;
while($row_new=mysql_fetch_assoc($new)){
if($i % 4 == 0) {
?>
<div class="row">
<?php
}
?>
<div class="col-3">
</div>
<?php
if($i % 4 == 3) {
?>
</div>
<?php
}
?>
<?php
$i++;
}
$i--;
if ($i % 4 != 3) {
?>
</div>
<?php
}
?>
The last if is in case the total number of records weren't a multiple of 4, then we need to close the div tag.
And, not to mention, you should definitely use MySQLi. Why should I use MySQLi?
try this bro:
update, if count rows less then 4:
$new = get_records("tbl_item", "status=1 AND idshop='{$idshop}' AND special", "id DESC", $startRow . "," . $pageSize, " ");
$dem = 1;
$x = 0;
$count = count($new);
while ($row_new = mysql_fetch_assoc($new)) {
$x++;
if ($x==1 || $x % 4 == 1) { ?>
<div class="row">
<?php } ?>
<div class="col-3">
<?= "x: $x" ?>
</div>
<?php if ($x % 4 == 0 || $x == $count) { ?>
</div>
<?php
}
}
Try this:
<?php
$new=get_records("tbl_item","status=1 AND idshop='{$idshop}' AND special","id DESC", $startRow.",".$pageSize, " ");
$dem=1;
while($row_new=mysql_fetch_assoc($new)){
?>
<div class="row">
<?php for($i = 1; $i <= 4; $i++){?>
<div class="col-3">
</div>
<?php } ?>
</div>
<?php } ?>

Having trouble in creating a while loop

I have written CSS for two column layout and I have a "users" database with notes of the users.
So when I run a while loop like this: while($row = mysqli_fetch_array($result))
PHP should dynamically create html like this (depending on no. of users):
<div class="row">
<div class="col">
<div class="col-content"><?php echo $row['notes'] ?></div>
<!-- Notes of user 1 -->
</div>
<div class="col">
<div class="col-content"><?php echo $row['notes'] ?></div>
<!-- Notes of user 2 -->
</div>
</div>
<div class="row">
<div class="col">
<div class="col-content"><?php echo $row['notes'] ?></div>
<!-- Notes of user 3 -->
</div>
<div class="col">
<div class="col-content"><?php echo $row['notes'] ?></div>
<!-- Notes of user 4 -->
</div>
</div>
If there's is only one user, html should be like this:
<div class="row">
<div class="col">
<div class="col-content"><?php echo $row['notes'] ?></div>
<!-- Notes of user 1 -->
</div>
</div>
And similarly if there are 3 users, there should one row with two columns and another row with one column.
Is there any way to do that?
Edit: Added my php code here.
<?php
$query = "SELECT * FROM users";
$result = mysqli_query($conn, $query);
while($row = mysqli_fetch_array($result)){
?>
<div class="row">
<div class="col">
<div class="col-content"><?php echo $row['notes'] ?></div>
</div>
</div>
<?php } ?>
Problem with that code is it's creating a new row with "one column" for every user. My goal is to have "two columns" in the row.
you can achieve things like that with the modulo operator (%) within loops.
$i = 0;
$rowCount = mysqli_num_rows($result);
$rowsHTML = '';
while($row = mysqli_fetch_array($result)){
$notesHTML = '<div class="col">
<div class="col-content">'.$row['notes'].'</div>
<!-- Notes of user 1 -->
</div>';
if($i%2 == 0){
$notesHTML = '<div class="row">'.$notesHTML;
// check if its the last item and the row has to be closed after 1 col
if($i+1 == $rowCount){
$notesHTML = $notesHTML.'</div>';
}
} else {
$notesHTML = $notesHTML.'</div>';
}
$rowsHTML .= $notesHTML;
$i++;
}
echo $rowsHTML;
<?php
$query = "SELECT * FROM users";
$result = mysqli_query($conn, $query);
$i = 0;
?>
<div class="row">
<?php
while($row = mysqli_fetch_array($result)){
if($i>1 && $i%2==0) echo '</div><div class="row">';
$i++;
?>
<div class="col">
<div class="col-content"><?php echo $row['notes'] ?></div>
</div>
<?php } ?>
</div>

scaffolding for bootstrap columns in php while loops

I'm trying to present my sql rows in bootstrap grid dynamically
<div class="row>
<div class="col-md-3">sql row 1</div>
<div class="col-md-3">sql row 2</div>
<div class="col-md-3">sql row 3</div>
<div class="col-md-3">sql row 4</div>
</div>
<div class="row>
<div class="col-md-3">sql row 5</div>
<div class="col-md-3">sql row 6</div>
<div class="col-md-3">sql row 7</div>
<div class="col-md-3">sql row 8</div>
</div>
and I'm having trouble how to set up the while loop. what I have now just does
<div class="row>
<div class="col-md-3">sql row 1</div>
<div class="col-md-3">sql row 2</div>
<div class="col-md-3">sql row 3</div>
<div class="col-md-3">sql row 4</div>
<div class="col-md-3">sql row 5</div>
<div class="col-md-3">sql row 6</div>
<div class="col-md-3">sql row 7</div>
<div class="col-md-3">sql row 8</div>
</div>
how can I set up loop to assign four col-md-3 classes to each row automatically and then continue. Current code:
<div class="row">
while($row = $result->fetch_assoc())
{
$spons_amt = round($row["spons_amt"] + 500, 2);
echo '<div class="col-md-3">' . $spons_amt . '</div>'
}
mysqli_close($conn);
</div>
I'm guessing what I need to do is
<div class="row">
while($row = $result->fetch_assoc())
// count number of rows
{
//list 4 of these (to make a row)
$spons_amt = round($row["spons_amt"] + 500, 2);
echo '<div class="col-md-3">' . $spons_amt . '</div>'
}
// echo </div><div class="row"> between every set of 4
mysqli_close($conn);
</div>
Am I going about this the right way? How could I complete the code?
The most elegant solution is to use PHP's array_chunk function:
foreach(array_chunk($entries, 4) as $entriesRow) {
echo '<div class="row">';
foreach ($entriesRow as $entry) {
echo "<div class='col-md-3'>$entry</div>";
}
echo '</div>';
}
(Assuming that you have fetched all DB rows in $entries array.)
Try something like this:
$n = 0;
echo '<div class="row">';
while($row = $result->fetch_assoc())
{
$spons_amt = round($row["spons_amt"] + 500, 2);
if($n%4 == && $n =! 0) {
echo '</div>';
echo '<div class="row">';
echo '<div class="col-md-3">' . $spons_amt . '</div>';
$n = 1;
} else {
echo '<div class="col-md-3">' . $spons_amt . '</div>';
$n++;
}
}
echo '</div>';
No need for all this complexity. Bootstrap works within a 12 col grid system automatically. Just make sure you loop and make col size so that that evenly divides by 12 e.g. col-md-4.
This example will provide 3 per row automatically since 12 / 4 = 3. Just update to use ECHO or whatever you need this is just a basic concept example.
<div class="row">
LOOPCODE
{
<div class="col-md-4">
DATA
</div>
}
</div>

PHP modulus to wrap divs and ignoring LIMIT

I'm trying to add a container every 2 iterations. I've implemented a count, but I'm getting weird results as I inspect the page.
Any ideas what may be going on and what I may be doing wrong with the wrapper injection?
<?php
include "includes2014_new/db_conx.php";
$sql ="SELECT * FROM articles WHERE id ORDER BY id DESC LIMIT 5";
$resultsHome = mysqli_query($db_conx,$sql);
$productCount = mysqli_num_rows($resultsHome); // count the output amount
if ($productCount > 0) {
$counter = 1;
while($row = mysqli_fetch_array($resultsHome)){
$id = $row["id"];
$article_title = $row["article_title"];
$category = $row["category"];
$readmore = $row["readmore"];
$author = $row["author"];
$date_added = $row["date_added"];
$content = $row["content"];
$short = substr(strip_tags($content), 0, 80);
if (($counter % 2) === 1) {
$blogList .= '<div class="clearer">';
}
//----------------
if($readmore == ''){
$blogList .= '<div class="col span_6_of_12 postBox textLeft fulltwelvepadfix">
<div class="section miniPad">
<div class="col span_8_of_12">'.$article_title.'</div>
<div class="col span_4_of_12"><img src="images/'.$id.'.jpg" height="80px" width="100px" alt="'.$category.' " /></div>
</div>
<div class="section miniPad">
<div class="col span_12_of_12">'.$date_added.' - '.$author.'</div>
<div class="col span_12_of_12"><p>'.$short.'[...]</p></div>
<div class="section group">Read More</div>
</div>
<div class="comments section group">
<div class="commentsInner">Comments(0)</div>
</div>
</div>';
}
else {
$blogList .= '<div class="col span_6_of_12 postBox textLeft fulltwelvepadfix">
<div class="section miniPad">
<div class="col span_8_of_12">'.$article_title.'</div>
<div class="col span_4_of_12"><img src="images/'.$id.'.jpg" height="80px" width="100px" alt="'.$category.' " /></div>
</div>
<div class="section miniPad">
<div class="col span_12_of_12">'.$date_added.' - '.$author.'</div>
<div class="col span_12_of_12"><p>'.$short.'[...]</p></div>
<div class="section group">Read More</div>
</div>
<div class="comments section group">
<div class="commentsInner">Comments(0)</div>
</div>
</div>';
}
//----------------
if (($counter % 2) === 0) { $blogList .= '</div>'; }
$counter++;
}
}
?>
Try to change
SELECT * FROM articles WHERE id ORDER BY id DESC LIMIT 5
to
SELECT * FROM articles WHERE `id`= '$id' ORDER BY id DESC LIMIT 5

Categories