Reputation: 615
I am trying to create a new row or breaking my row of results to drop down to a new row in my results set because at the moment they just go on and on and break out of the container and do not create a new row as it hits the end of the container.
My code is fairly simple - I am using the bulma css framework and specifically the is-3
class to display each result so its gives me 4 columns in the container. So after every four a new row should be created.
My code is is fairly simple:
<div class="container">
<div class="columns projectList">
<?php while($stmt->fetch()) { ?>
<div class="column is-3">
<div class="card">
<div class="card-content">
<div class="content">
<p class="projectStatus">
<?php
if($phase == 0){
echo 'Phase: Pending';
}
elseif($phase == 1){
echo 'Phase: Planning';
}
elseif($phase == 2){
echo 'Phase: Design';
}
elseif($phase == 4){
echo 'Phase: Development';
}
else{
echo 'No Phase Added';
}
?>
</p>
<p class="projectTitle"><span>!</span><?php echo $projectName; ?></p>
<div class="projectTimes">
<div class="inner">
<span class="days">12</span>
<span class="for">Days Left In Design</span>
</div>
<div class="inner">
<span class="days">42</span>
<span class="for">Days to finish</span>
</div>
</div>
</div>
</div>
<footer class="card-footer">
<a class="card-footer-item"><i class="material-icons"></i>32</a>
<a class="card-footer-item"><i class="material-icons"></i>10</a>
</footer>
</div>
</div>
<!-- END ROW HERE AND CREATE NEW ONE -->
<?php } $stmt->close(); } ?>
</div>
</div>
My question: How do I line break after every fourth result?
Upvotes: 0
Views: 2914
Reputation: 6650
<div class="container">
<?php
$i = 0;
while($stmt->fetch()) {
if($i%4==0){
echo '<div class="columns projectList">';
}
$i++;
?>
<div class="column is-3">
<div class="card">
<div class="card-content">
<div class="content">
<p class="projectStatus">
<?php
if($phase == 0){
echo 'Phase: Pending';
}
elseif($phase == 1){
echo 'Phase: Planning';
}
elseif($phase == 2){
echo 'Phase: Design';
}
elseif($phase == 4){
echo 'Phase: Development';
}
else{
echo 'No Phase Added';
}
?>
</p>
<p class="projectTitle"><span>!</span><?php echo $projectName; ?></p>
<div class="projectTimes">
<div class="inner">
<span class="days">12</span>
<span class="for">Days Left In Design</span>
</div>
<div class="inner">
<span class="days">42</span>
<span class="for">Days to finish</span>
</div>
</div>
</div>
</div>
<footer class="card-footer">
<a class="card-footer-item"><i class="material-icons"></i>32</a>
<a class="card-footer-item"><i class="material-icons"></i>10</a>
</footer>
</div>
</div>
<?php
if($i%4==0){
echo '</div>'
$i = 0;
}
?>
<!-- END ROW HERE AND CREATE NEW ONE -->
<?php } $stmt->close(); } ?>
</div>
</div>
Upvotes: 2
Reputation: 1286
This is a fairly simple one. I take from your question that you essentially want to split your results from the query into groups of 3? With each group being displayed as a row?
What i would recommened is fetching all of your results at once and storing as a variable. Assuming this variable is an array you can use array_chunk() to split the array of rows into the cunks you need. This will result in an array of arrays with each of the inner arrays containing the specified number of items. See: http://php.net/manual/en/function.array-chunk.php
Ive mocked up a quick example for you here:
<?php
//Fetch all data, this will become the $items array
$items = array(
'item 1',
'item 2',
'item 3',
'item 4',
'item 5',
'item 6',
'item 7',
'item 8',
'item 9',
'item 10',
'item 11',
'item 12',
'item 13',
'item 14',
);
//Split the items into 'rows' (chunks)
//The second parameter here basically indiciates how many items you want in each row
$rows = array_chunk($items, 3);
//Loop over each row group, remembering each row is an array of items (columns)
//Do your row building HTML within this foreach loop
//Do your columnd building HTML within the inner foreach loop
foreach($rows as $columns){
echo "---------------<br />";
//Build column html for each item within the row
foreach($columns as $column){
echo $column.' | ';
}
echo "<br />";
echo "---------------";
echo "<br /><br />";
}
?>
Run the abover here: http://phpfiddle.org/
You can see how it splits up the rows etc.
Hope this helps!
Upvotes: 0
Reputation: 1322
is this what you are looking for ?
<div class="container">
<?php
$x = 1;
while($stmt->fetch()) {
?>
<?php if($x == 1) { ?> <div class="columns projectList"> <?php }
$x++;
?>
<div class="column is-3">
<div class="card">
<div class="card-content">
<div class="content">
<p class="projectStatus">
<?php
if($phase == 0){
echo 'Phase: Pending';
}
elseif($phase == 1){
echo 'Phase: Planning';
}
elseif($phase == 2){
echo 'Phase: Design';
}
elseif($phase == 4){
echo 'Phase: Development';
}
else{
echo 'No Phase Added';
}
?>
</p>
<p class="projectTitle"><span>!</span><?php echo $projectName; ?></p>
<div class="projectTimes">
<div class="inner">
<span class="days">12</span>
<span class="for">Days Left In Design</span>
</div>
<div class="inner">
<span class="days">42</span>
<span class="for">Days to finish</span>
</div>
</div>
</div>
</div>
<footer class="card-footer">
<a class="card-footer-item"><i class="material-icons"></i>32</a>
<a class="card-footer-item"><i class="material-icons"></i>10</a>
</footer>
</div>
</div>
<!-- END ROW HERE AND CREATE NEW ONE -->
<?php
if($x % 4 == 0)
{
echo '<div/>';
$x = 1;
}
} $stmt->close(); } ?>
</div>
Upvotes: 0
Reputation: 381
Using this logic you can break your ROW at fourth iteration
$i = 0;
while(while($stmt->fetch())){
if($i%4==0){
//div to create new ROW
}
<div class="column is-3">
//all your logic go here
</div>
if($i%4==0){
// close the div for create new ROW
}
$i++;
}
Upvotes: 0
Reputation: 9123
There is something called the modulus
.
for ($c = 1; $c < count($results); $c++) {
if ($c % 4 == 0) {
// This is the fourth iteration
}
}
Upvotes: 1