I have a sample list 1,2,3,4,5,6,7,8
I want create 3 colums with format
1 4 7
2 5 8
3 6
This is my code
$columns = 3;
$rows = ceil(count($lists) / $columns);
foreach ($lists as $key => $value) {
if($key % $rows == 0) {
echo '<div class="col-md-4">';
}
echo $value;
if($key % $rows == 0) {
echo '</div>';
}
}
This is result error
<div class="col-md-4">1</div>
2
3
<div class="col-md-4">4</div>
5
6
<div class="col-md-4">7</div>
8
How to fix it for result:
<div class="col-md-4">1 2 3</div>
<div class="col-md-4">4 5 6</div>
<div class="col-md-4">7 8</div>
This solution works for your last edit:
<div class="col-md-4">1 2 3</div>
<div class="col-md-4">4 5 6</div>
<div class="col-md-4">7 8</div>
So, code is:
$lists = array(1,2,3,4,5,6,7,8);
$columns = 3;
$rows = ceil(count($lists) / $columns);
echo '<div class="col-md-4">';
foreach($lists as $key => $value) {
if ($key % $rows == 0 && $key != 0) {
echo '</div><div class="col-md-4">'.$value.' ';
} else {
echo $value.' ';
}
}
echo '</div>';
... because you're only adding the div to every third value. I think this is what you want
$columns = 3;
$rows = ceil(count($lists) / $columns);
foreach ($lists as $key => $value) {
if($key % $rows == 0) {
echo '<div class="row">';
}
echo '<div class="col-md-4">' . $value . '</div>';
if($key % $rows == 0) {
echo '</div>';
}
}
It's a lot simpler to achieve what you're after. Say you have the following list:
$list = '1,2,3,4,5,6,7,8';
All you'd have to do is explode your string (list) into an array and chunk that into groups of 3. Now that simplifies what you are required to do when printing out your rows:
$chunks = array_chunk(explode(',', $list), 3);
foreach($chunks as $row){
echo "<div class='row'>";
foreach($row as $value){
echo "<div class='col-md-4'>{$value}</div>";
}
echo "</div>";
}
Example/Demo
References
array_chunk()
explode()
Assuming you need fixed 3 columns, here the code (you may have to fix the code based on your column requirement)
$columns = 3;
$lists = array(1,2,3,4,5,6,7,8);
$rows = ceil(count($lists) / $columns);
$i = 0;
while($i < $rows)
{
echo "<div class=col-xs-4>".#$lists[$i]."</div>";
echo "<div class=col-xs-4>".#$lists[$i+$rows]."</div>";
echo "<div class=col-xs-4>".#$lists[$i+$rows+$rows]."</div>";
$i++;
}
Related
How can I generate header for printed 2d array in php?
So my array look like this:
$tab=array(
array(0,1,2,3),
array(1,2,3,4),
array(2,3,4,5)
)
This code:
foreach ($tab as $key => $row){
echo '<b>o<sub>'.($key+1).'</sub></b> ';
foreach ($row as $item) {
echo $item.' ';
}
echo '<br>';
}
Print this:
o1 0 1 2 3
o2 1 2 3 4
o3 2 3 4 5
But I need this:
a1 a2 a3 d
o1 0 1 2 3
o2 1 2 3 4
o3 2 3 4 5
Where lenght of rows may be diferent and last column always must be d
Thanks for help
You can check if this is the first iteration in the first foreach and if so, add the first line.
foreach ($tab as $key => $row) {
// If first iteration, add the header
if ($key === 0)
{
foreach ($row as $i => $item)
{
// Last header must be 'd'
if ($i === count($row) - 1)
echo '<b>d</b>';
else
echo '<b>a<sub>' . ($i + 1) . '</sub></b> ';
}
echo '<br />';
}
// Add the current row
echo '<b>o<sub>' . ($key + 1) . '</sub></b> ';
foreach ($row as $item) {
echo $item . ' ';
}
echo '<br />';
}
If the first row is a static row, then you can simply print it using echo function at first.
But if its a dynamic tab as well, then you can use the following code:
$count = count($tab[0]);
for($i = 1; $i <= $count; $i++)
{
if($i != $count) echo '<b>a<sub>' . ($i + 1) . '</sub></b> ';
else echo '<b>d</b> ';
}
foreach ($tab as $key => $row){
echo '<b>o<sub>'.($key+1).'</sub></b> ';
foreach ($row as $item) {
echo $item.' ';
}
echo '<br>';
}
You can try this, except use <table> or <div> to align your elements.
foreach ($tab[0] as $key => $item)
echo $key === 0 ? title(' ', ' ') : title('a', $key);
echo title('d', '') . '<br>';
function title(string $letter, string $index)
{
return "<b>{$letter}<sub>{$index}</sub></b>";
}
I'm currently listing all my interests inside one div. However, I'd like to spread these across columns. So have 4 interests per column. How can I change my CSS to allow this?
<?php
$interests = get_terms('interests',array( 'taxonomy' => 'interests' ));
foreach($interests as $term){
echo '<li>'.$term->name.'</li>';
}
?>
Preferred HTML output:
<div class="columns">
<ul>
<li>F1</li>
<li>Cycling</li>
<li>Football</li>
<li>Rugby</li>
</ul>
</div>
<div class="columns">
<ul>
<li>Running</li>
<li>Swimming</li>
</ul>
</div>
<?php
$interests = get_terms('interests',array( 'taxonomy' => 'interests' ));
echo '<div class="columns">';
$index = 0;
foreach($interests as $term){
if ($index > 0 and $index % 4 == 0) {
//if not the first row and dividable by 4
echo '</div><div class="columns">';
}
echo '<li>'.$term->name.'</li>';
$index++;
}
echo '</div>';
?>
For you comment:
$interests = get_terms('interests',array( 'taxonomy' => 'interests' ));
$count = count($interests);
$numItemsPerRow = ceil($count / 3);
//we need this in case of 2-1-1 for example, otherwise you get 2-2
$numItemsOffsetFix = $count % 3 == 1;
$index = 0;
echo '<div class="columns">';
foreach($interests as $term){
if ($index > 0 and $index % $numItemsPerRow == 0) {
echo '</div><div class="columns">';
if ($numItemsOffsetFix) {
$numItemsPerRow--;
$numItemsOffsetFix = false;
}
}
echo '<li>'.$term->name.'</li>';
$index++;
}
echo '</div>';
I cannot solve problem with starting ending divs after couple of elements from array.
What i want to get is something like that:
<div>
element1
element2
element3
element4
</div>
<div>
element5
element6
element7
element8
</div>
<div>
element9
element10
</div>
Here is my php code:
$array = array("element1","element2","element3","element4","element5","element6","element7","element8","element9","element10");
$perRow = 4;
$count = 1;
foreach ($array as $arr){
// here div needs to start, use 4 elements from array and close
if($count % $perRow == 0 OR $count == 1){
echo '<div>';
}
echo $arr . '<br>';
// here should div close
$count++;
}
Try something like this
$array = array("element1","element2","element3","element4","element5","element6","element7","element8","element9","element10");
$perRow = 4;
$count = 0;
echo '<div>';
foreach ($array as $arr){
// here div needs to start, use 4 elements from array and close
if($count % $perRow == 0 && $count!=0){
echo '</div><div>';
}
echo $arr . '<br>';
// here should div close
$count++;
}
echo '</div>';
Okay I am not familiar with arrays and maybe something like this would work:
$array = array("element1","element2","element3","element4","element5","element6","element7","element8","element9","element10");
$i=0;
echo '<div>'
if (i<3) {
echo '$array[$i]';
$i++;
}
echo '</div>';
echo '<div>';
if ($i>3 && $i<7) {
echo '$array[$i]';
$i++;
}
echo '</div>';
echo '<div>';
if ($i>7 && $i<10) {
echo '$array[$i]';
$i++;
}
echo '</div>';
Need some clues with the rest of this script:
// Num of columns
$col = 3;
foreach ($schema as $key => $array) {
// wrapper div
if($key % $col == 0) {
echo '<div class="wrapper" style="float: left;">;
}
// divs inside columns
foreach ($array as $c => $value) {
echo '<div>' . $value . '</div>';
}
// Here I need to close wrapper div
// # to do
}
// Num of columns
$col = 3;
echo '<div class="wrapper" style="float: left;">';
$count = 1;
foreach ($schema as $key => $array) {
// wrapper div
if($count % $col == 0) {
echo '</div>';
if(isset($schema[$key+1])) // check if next element exists then start new div
echo '<div class="wrapper" style="float: left;">';
}
// divs inside columns
foreach ($array as $c => $value) {
echo '<div>' . $value . '</div>';
}
$count++;
}
if(count($schema) % $col != 0)
echo '</div>'; // at last close div
I have
foreach ($a as $key => $value) {
echo "<DIV id='container'>
<DIV id='middle'>$value</DIV>
</DIV>";
//echo "<br />$key:$value<br />\n";
}
which displays result one below other,
like
1234
5678
2010-05-20
5678
1590
2010-05-19
but i want it in a table like structure like
1234 5678 2010-05-20
5678 1590 2010-05-19
how can i do that?
You could keep track with for example modulus if you should begin a new row or not.
EDITED after reformatting of the question
I think what you need is simply something on the line of:
echo '<table><tr>';
$n = 0;
foreach ($a as $key => $value)
{
if ($n==3)
{
$n = 0;
echo '</tr><tr>';
}
echo '<td>'.$value.'</td>';
$n++;
}
echo '</tr></table>';
You can do like:
$counter = 0;
foreach ($a as $key => $value) {
$counter++;
if (($counter % 3) === 0)
{
echo "<DIV id='container'>
<DIV id='middle'>$value</DIV>
</DIV>";
}
else
{
echo "<DIV id='container' style='float:left;'>
<DIV id='middle'>$value</DIV>
</DIV>";
}
}
Kind of what nico was saying:
foreach ($a as $key => $value)
{
echo '<div style="width:33%;float:left">'.$value.'</div>';
}
Or what don was saying:
$i = 0;
$open = false;
echo '<table>';
foreach( $a as $key => $value )
{
if( 0 == $i % 3 ) {
echo '<tr>';
$open = true;
}
echo "<td>$value</td>";
if( 2 == $i % 3 ) {
echo '</tr>';
$open = false;
}
$i++;
}
if( $open )
echo '</tr>';
echo '</table>';
Simply use like this,
foreach ($a as $key => $value) {
echo "<DIV id='container' style='display:inline'>
<DIV id='middle' style='display:inline'>$value</DIV>
</DIV>";
//echo "<br />$key:$value<br />\n";
}
add the break statement depending on your second line.
By default, div's don't float next to each other. You could give all the div's, except every third one float: left in the style. However it's okay to use table's if your displaying a table.
If you can derive from the value of $key wether to start a new row or not you can do something like:
echo '<table><tr>';
foreach ($a as $key => $value) {
if($key == 'somevalue'){
//start a new row if the $key allow
echo '</tr><tr>';
}
echo '<td>'.$value.'</td>';
}
echo '</tr></table>';
If you can't derive from the value of $key wether to start a new row or not, use a counter, like so:
echo '<table><tr>';
$cnt = 0;
foreach ($a as $key => $value) {
if($cnt % 3 == 0){
//start a new row if this row contians 3 cells
echo '</tr><tr>';
}
echo '<td>'.$value.'</td>';
$cnt++;
}
echo '</tr></table>';