scaffolding for bootstrap columns in php while loops - php

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>

Related

PHP: Fetch 6 results on 1 line then fetch next 6 on 2nd line

The aim is to fetch all the mechanics stored in a database and put them on one line until 6 are displayed and then begin a new line.
<?php
//MySqli Select Query
$mechanics = $mysqli->query("SELECT * FROM mechanics");
while($row = $mechanics->fetch_assoc()) {
$name = $row['name'];
$initial = $row['initial'];
echo'
<div class="row text-center ">
<div class="col-lg-2 col-md-2 col-sm-2 col-xs-6">
<div class="div-square">
<a href="blank.html" >
<h3>'.$initial.'</h3>
<h4>'.$name.'</h4>
</a>
</div>
</div>
</div> ';
}
?>
It looks like you're using the bootstrap grid, but you're currently printing each record in its own row. You can do it like this instead:
Open a row
echo '<div class="row text-center ">';
Then output the query results, each in a column
while ($row = $mechanics->fetch_assoc()) {
$name = $row['name'];
$initial = $row['initial'];
echo '<div class="col-sm-2 col-xs-6">
<div class="div-square">
<a href="blank.html" >
<h3>'.$initial.'</h3>
<h4>'.$name.'</h4>
</a>
</div>
</div>';
}
Close the row.
echo '</div>';
Incidentally, you don't need col-lg-2 col-md-2 col-sm-2 If you've defined the column as col-sm-2 that 2 will apply to the larger sizes as well.

How to display in 3 columns per row using cards in materialize

It displays the cards in one column instead of three at medium sizes.
I've tried the basic layout in HTML and it works fine.
echo '<div class="row>';
if($res->num_rows > 0){
while($row = $res->fetch_assoc()){
echo '<div class="col s12 m4">' .
'<div class="card">' .
'<div class="card-content">' .
'<span class="card-title">' . $row["Kanji"] . '</span>' .
$row["Onyomi"]. ' ' . $row["Kunyomi"] .
'</div>' .
'<div class="card-action">' .'<p>' . $row["English"] . '</p>' . '</div>' .
'</div>'.
'</div>';
}
}
echo '</div>';
I did it back to front.
Here is what worked.
<div class="row">
<?php if($res->num_rows > 0){
while($row = $res->fetch_assoc()){
?>
<div class="col s12 m4">
<div class="card">
<div class="card-content">
<span class="card-title"><?php echo $row["Kanji"] ?></span>
<p><?php echo $row["Onyomi"]; echo $row["Kunyomi"] ?></p>
</div>
<div class="card-action">
<?php echo $row["English"] ?>
</div>
</div>
</div>
<?php } } ?>
you can check for materialize grid https://materializecss.com/grid.html
Using materialize grid you you can put the necessary columns.
One example for 3 columns in a row
<div class="row">
<div class="col s4">Column 1</div>
<div class="col s4">Column 2</div>
<div class="col s4">Column 3</div>
</div>
you forgot quotation marks at the end of row class: class="row>

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>

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>

PHP loop place every 3rd loop in row

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>

Categories