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>';
Related
I have an array which can have any no. of elements in it. Now i want to loop this array and create design such that each li can have 15 elements inside it , next set of li will be created based of multiples of 15's elements.
Now my array has exact 15 elements and the code i am trying creating 2 li , which it should create only 1 li.
May be my logic is too bad or I am missing anything.
Here is my code:-
<?php $result = array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15); ?>
<div class="slide">
<?php $design = '<ul class="slides"><li><div class="MainSlider">';
foreach($result as $key=>$row)
{
$design .= '<div class="MainSliderPic">'.$key.'</div>';
if(($key+1)% 15 == 0){
$design .= '</div></li><li><div class="MainSlider">';
}
if(count($result) == $key+1){
$design .= '</div></li></ul>';
}
}
echo $design;
?>
</div>
You can use array_chunk for to achieve it:
$result = array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15);
$chunks = array_chunk($result, 15);
foreach ($chunks as $chunk) {
echo '<ul><li>';
echo implode('</li><li>', $chunk);
echo '</li></ul>';
}
Don't mix opening and closing of tags in your code. Do it separately where it belongs, e.g.
$design = '<ul class="slides">';
$n = 0;
foreach($result as $key=>$row) {
if ($n == 0)
$design .= '<li><div class="MainSlider">';
$design .= '<div class="MainSliderPic">' . $key . '</div>';
++$n;
if ($n == 15) {
$design .= '</div></li>';
$n = 0;
}
}
$design .= '</ul>';
echo $design;
Try following code:
<?php $result = array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15); ?>
<div class="slide">
<?php $design = '<ul class="slides"><li><div class="MainSlider">';
foreach($result as $key=>$row)
{
$design .= '<div class="MainSliderPic">'.$key.'</div>';
if((($key+1)% 15 == 0) && (count($result) != ($key+1))){
$design .= '</div></li><li><div class="MainSlider">';
}
if(count($result) == $key+1){
$design .= '</div></li></ul>';
}
}
echo $design;
?>
</div>
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++;
}
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>';
$menu = mysql_query(" query 1");
$k = 1;
for ($s = 0; $s < mysql_num_rows($menu); $s++)
{
$menus= mysql_fetch_assoc($menu);
$mainmenu[]=$menus['name'];
$submenus=mysql_query("query 2");
for ($m = 0; $m < mysql_num_rows($submenus); $m++)
{
$submenu = mysql_fetch_assoc($submenus);
$subitem[]=$submenu['name'];
$url=$submenu['url'];
}
}
foreach($mainmenu as $mains)
{
echo '<li class="hasul"><a><span><b>' .$mains.'</b></span></a></li>';
foreach($subitem as $sub)
{
echo '<ul>';
echo '<li><span>' .$sub. '</span></li>';
echo '</ul>';
}
}
The code above show two queries which load a menu and submenus. Query2 uses some inputs from query1.
The menus do load correctly but they contain same menu items. Ideally each menu should have its own menu items.
You have a foreach inside your foreach, that means, that for every main menu you are outputing all the submenus.
Create some parameter for submenu, like:
$mainmenu = array(
1 => 'about',
2 => 'news',
3 => 'search',
);
$submenu = array(
1 => array( 'about my name', 'about my location' ),
3 => array( 'search me' ),
);
And now check only the submenu you need:
if ( is_array( $mainmenu ) )
{
echo '<ul>';
foreach( $mainmenu as $key=>$menu )
{
echo '<li>'.$menu.'</li>';
if ( is_array( $submenu[$key]) )
{
echo '<ul>';
foreach( $submenu[$key] as $sub )
{
echo '<li>'.$sub.'</li>';
}
echo '</ul>';
}
}
echo '</ul>';
}
This will produce:
<ul>
<li>about</li>
<ul>
<li>about my name</li>
<li>about my location</li>
</ul>
<li>news</li>
<li>search</li>
<ul>
<li>search me</li>
</ul>
</ul>
I hope you get the idea.
use this.. this could be easier to understand..
echo '<ul>';
$qry = mysql_query("query 1");
while ($row = mysql_fetch_array($qry))
{
echo "<li>";
echo "<a href='" . $row['url'] . "'>'" . $row['name'] . "'</a>";
$qry2 = mysql_query("query 2");
if (mysql_num_rows($query2) > 0)
{
echo "<ul>";
while ($row2 = mysql_fetch_array($query2))
{
echo "<li>";
echo "<a href='" . $row2['url2'] . "'>'" . $row2['name2'] . "'</a>";
echo "</li>";
}
echo "</ul>";
}
echo "</li>";
}
echo "</ul>";
let me know if you want any further help...
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