I am trying to build a php loop which generates a html navigation with years like this:
<ul>
<li>
1900
<ul>
<li>1901</li>
<li>1902</li>
</ul>
</li>
<li>
2000
<ul>
<li>2001</li>
<li>2002</li>
</ul>
</li>
</ul>
Php I am trying:
foreach (range(0, 1000, 100) as $number) { ?>
<ul>
// Here I should be checking
// if it is year 1942 or 2000 or 1080
// and place ti correctly
<?php echo $number; ?>
</ul>
<?php }
The above works, gives me a new ul every 100.
But I don't understand how I should set my php to open a new <ul> and <li> if we have "sub years" inside the century itself, also I should be checking what year is it that I am outputting in order to have it in its right <ul> or <li>
echo "<ul>";
for ($century=1900; $century <= 2000 ; $century =$century + 100) {
echo "<li>". $century;
echo "<ul>";
for ($year = $century; $year < $century + 100; $year++) {
echo "<li>". $year . "</li>";
}
echo "</ul>";
echo "</li>";
}
echo "</ul>";
I hope this is what you need:
<?php
$start_year=1900;
$end_year=2100;
for($year=$start_year;$year<=$end_year;$year++){
if($year%10==0) //Replace 10 (for decades) with 100 (for centuries)
{
if($year!=$start_year){
echo '</ul></li></ul>';
}
echo '<ul><li>'.$year.'<ul>';
}
else{
echo '<li>'.$year.'</li>';
}
}
?>
Related
I am trying to do something fit with the design, so i have to show 2 item in 1 loop. Also, according to this design, should be line break in each 4 loop. Line breaks applying automatically, but its fixed 4. So, in 4 loop it shows actually 8 items. However, the point I'm stuck on is; items are not displaying in the placement I want. You can see the placement I want to do with more detail in the picture.
Red Numbers = Current placement
Green Numbers = Placement it should be
Yellow boxes = For loop items ( 8 items )
Black boxes = Items in array (16 items )
.
ul,li {
padding:0;
margin:0;
list-style:none;
}
li {
width:30px;
}
.gen {
width: 140px
}
li {
display: inline-block;
}
.ic {
width: 100%
}
<ul class="gen">
<li>
<ul>
<li class="ic">a</li>
<li class="ic">b</li>
</ul>
</li>
<li>
<ul>
<li class="ic">c</li>
<li class="ic">d</li>
</ul>
</li>
<li>
<ul>
<li class="ic">e</li>
<li class="ic">f</li>
</ul>
</li>
<li>
<ul>
<li class="ic">g</li>
<li class="ic">h</li>
</ul>
</li>
<li>
<ul>
<li class="ic">i</li>
<li class="ic">j</li>
</ul>
</li>
<li>
<ul>
<li class="ic">k</li>
<li class="ic">l</li>
</ul>
</li>
<li>
<ul>
<li class="ic">m</li>
<li class="ic">n</li>
</ul>
</li>
<li>
<ul>
<li class="ic">o</li>
<li class="ic">p</li>
</ul>
</li>
</ul>
As you can see, items are not placing alphabetically from left to right.
Here is the loop i used :
// 16 items
$items = array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p');
$half = count($items) / 2;
$k = 0;
echo '<ul class="gen">';
for($i = 0; $i<$half; $i++) {
echo '<li>';
echo '<ul>';
echo '<li class="ic">'.$items[$k].'</li>';
echo '<li class="ic">'.$items[$k+1].'</li>';
echo '</ul>';
echo '</li>';
$k = $k+2;
}
echo '</ul>';
I hope this helps. You can run this code and see the output.
// chaganged the array to match your picture.
$items = array('1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16');
//used to format the output
echo "<pre>";
for ( $i = 0; $i<count($items) ; $i++ ) {
echo "loop start \n";
// access 1st item
echo $items[$i]."\n";
// based on your example, it seems that you need the item that is 4 places after the first item of the loop
echo $items[$i+4]."\n";
echo "loop end\n";
// we can use mod to check if we have reached the 4th element (index 3), we use $i+1 to check the second element of the loop
if ( ( $i+1 )%4 == 0 ) {
echo "\n---new line---\n";
$i = $i + 4;
}
echo "\n";
}
echo "</pre>";
Assuming that you will take care of the formatting, your code could be changed to this:
$items = array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p');
for($first = 0; $first < count($items); $first += 8) {
for($add = 0; $add < 4; $add++) {
echo $items[$first + $add]; //first item
echo $items[$first + $add + 4]; //second item
}
}
May I see your HTML code? Here's a suggestion, display your results like this (run code snippet and use my css and php loop);
#wrapper{
background-color:rgba(0,0,0,0.6);
padding:10px;
}
.result{
display:inline-block;
padding:7px;
border:3px solid #FEBF01;
width:120px;
}
.result-child{
display:block;
padding:10px 10px;
background-color:rgba(0,0,0,0.8);
color:rgba(50,255,100,1);
font-size:2em;
text-align:right;
}
.result .result-child:first-of-type{
margin-bottom:10px;
}
<div id="wrapper">
<div class="result">
<div class="result-child">1</div>
<div class="result-child">1</div>
</div>
<div class="result">
<div class="result-child">2</div>
<div class="result-child">2</div>
</div>
</div>
Loop code:
for($i = 0; $i<$half; $i++) {
echo "<div class='result'>";
echo "<div class='result-child'>".$items[$k]."</div>"; //first item
echo "<div class='result-child'>".$items[$k+1]."</div>"; //second item
echo "</div>";
$k = $k+2;
}
I would like to display all existing categories and all posts.
I wrote a foreach loop, but I don't know where is the place of the closing ul tag.
My code:
<?php
$current_cat = 0;
$curr_nb_item = 0;
foreach($blog_widget as $row)
{
if($current_cat != $row->category_id)
{
$current_cat = $row->category_id;
echo "<ul> " . $row->category_id . " ";
echo "<li>";
echo $row->title;
echo "</li>";
++$curr_nb_item;
}
else {
echo "<li>";
echo $row->title;
echo "</li>";
++$curr_nb_item;
}
}
?>
Now I got this result in html:
<ul>1
<li>
cat 1 post n
</li>
<li>
cat 1 post h
</li>
<ul>2
<li>
cat 2 post x
</li>
<li>
cat 2 post y
</li>
I tried a lot of variation without result. I hope somebody could help for me. Many thanks.
This may not be the cleanest, but it should work. Modify the top of your if statement to look like this:
if($current_cat != $row->category_id)
{
if ($row != $blog_widget[0])
{
echo "</ul>";
}
$current_cat = $row->category_id;
echo "<ul> " . $row->category_id . " ";
// etc...
And then after the closing brace for your foreach loop:
echo "</ul>";
First of all, I don't know how to count in php, maybe someone could recommend me a good source to read;
Second, I'm not asking to solve this for me, but I just want a hint or simpler explanation that would make sense;
Here is my function:
<ul>
<?php foreach ($categories as $category) { ?>
<li>
<p><?php echo $category['name']; ?></p>
<?php if ($category['children']) { ?>
<div>
<?php for ($i = 0; $i < count($category['children']);) { ?>
<?php $j = $i + ceil(count($category['children']) / $category['column']); ?>
<?php for (; $i < $j; $i++) { ?>
<?php if (isset($category['children'][$i])) { ?>
<ul>
<li class="none"><h1><?php echo $category['children'][$i]['name']; ?></h1></li>
<?php if ($category['children'][$i]['children_level2']) { ?>
<?php for ($wi = 0; $wi < count($category['children'][$i]['children_level2']); $wi++) { ?>
<li>
<?php echo $category['children'][$i]['children_level2'][$wi]['name']; ?>
</li>
<?php } ?>
<?php } ?>
</ul>
<?php } ?>
<?php } ?>
<?php } ?>
</div>
<?php } ?>
</li>
<?php } ?>
</ul>
I want this part, every 13 li elements to create a new .ul. ./ul. tags
<?php if (isset($category['children'][$i])) { ?>
<ul>
<li class="none"><h1><?php echo $category['children'][$i]['name']; ?></h1></li>
<?php if ($category['children'][$i]['children_level2']) { ?>
<?php for ($wi = 0; $wi < count($category['children'][$i]['children_level2']); $wi++) { ?>
<li>
<?php echo $category['children'][$i]['children_level2'][$wi]['name']; ?>
</li>
<?php } ?>
<?php } ?>
</ul>
<ul><li>maximum of 13 elements</li></ul>
after 13 <li></li> elements create new <ul></ul> tags and put the 14 <li></li> element into the new <ul></ul> tag
I hope I explained what I want to do, for now I'll be waiting for your answers,
p.s. this is more for my learning skills then actual work, so thanks in advice
It's simple. Use condition
if ($wi && $wi % 13 == 0) {
print '</ul><ul>';
}
In order to count 13 elements, you can use the modulo operator %. The idea is that ($iw % 13) is equal to 0 if the content of $iw is a multiple of 13. (see more about the modulo operation)
With an if statement you can insert </ul><ul> when you need in your for loop.
There's two possibilities I can think of:
you could have a separate counter wich resets to 1 and outputs a <ul> every time it reaches 13
or you could check each time if $wi id divisible by 13:
<?php
for ($wi = 0; $wi < count($category['children'][$i]['children_level2']); $wi++) {
if ($wi && $wi % 13 == 0) {
// output <ul>
}
}
?>
If you're unsure what % means, here's the appropriate PHP manual page
Code for Tree structure using ul, li in php...
*In the output i am missing ul or in some place.*
I want this for checkboxes when we click the parent check box their childs must be selected and sub childs too.
<?php
$groups = array(
array('depth'=>0,'value'=>'DEF'),
array('depth'=>1,'value'=>'DEF 1'),
array('depth'=>2,'value'=>'DEF_1'),
array('depth'=>1,'value'=>'DEF2'),
array('depth'=>0,'value'=>'GROUP'),
array('depth'=>1,'value'=>'GROUP1'),
array('depth'=>1,'value'=>'GROUP2'),
array('depth'=>2,'value'=>'GROUP2_1'),
array('depth'=>2,'value'=>'TELUGU'),
array('depth'=>3,'value'=>'RAM'),
array('depth'=>0,'value'=>'GROUP3'),
array('depth'=>1,'value'=>'GROUP3_1'),
array('depth'=>1,'value'=>'GROUP3_2'),
array('depth'=>1,'value'=>'GROUP3_3'),
array('depth'=>1,'value'=>'DEF2_1'),
array('depth'=>0,'value'=>'GROUP4'),
array('depth'=>0,'value'=>'T8')
);
$count = count($groups);
$old_depth = 0;
echo '<ul>'.chr(13).chr(10);
for($i = 0; $i<$count; $i++)
{
if($old_depth==$groups[$i]['depth'])
{
echo '<li>';
echo $groups[$i]['value'];
if(isset($groups[$i+1]))
{
if($old_depth!=$groups[$i+1]['depth']){
echo '<ul>';
}
else
{
echo '</li>';
}
}
}
if($old_depth<$groups[$i]['depth'])
{
echo '<li>';
echo $groups[$i]['value'];
if(isset($groups[$i+1]))
{
if(($groups[$i]['depth'])+1==$groups[$i+1]['depth']){
echo '<ul>';
}
else
{
echo '</li>';
if(!($groups[$i]['depth']==$groups[$i+1]['depth']))
{
echo '</ul></li>';
}
if($groups[$i+1]['depth']==0)
{
echo '</ul></li>';
}
}
}
else
{
echo '</ul>';
}
}
}
echo '</li></ul>';
?>
I want the output in this format.
<ul>
<li>DEF
<ul>
<li>DEF1
<ul>
<li>DEF_1</li>
</ul>
</li>
<li>DEF2</li>
</ul>
</li>
<li>GROUP
<ul>
<li>GROUP1</li>
<li>GROUP2
<ul>
<li>GROUP2_1</li>
<li>TELUGU
<ul>
<li>RAM</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
<li>GROUP3
<ul>
<li>GROUP3_1</li>
<li>GROUP3_2</li>
<li>GROUP3_3</li>
<li>DEF2_1</li>
</ul>
</li>
<li>GROUP4</li>
<li>T8</li>
</ul>
I can't figure out how to group my subcategories into a container after every fifth record.
for example I would like to output my subcats like this
<ul>
<li>
Main category
<div>
<div class="subcontainer">
sub1
sub2
sub3
sub4
sub5
</div>
<div class="subcontainer">
sub6
sub7
sub8
sub9
sub10
</div>
</div>
</li>
</ul>
I tried this:
<?php
$i=0;
foreach ($cat->sub as $child) {
if($i % 5 == 0){
echo '<div class="subcontainer"><div>'.$child->name.'</div></div>';
} $i++;
}
?>
But this will output only the 5th elements in the array.
You need to always print your $child->name variable and on every 5th item also print the </div><div> part. You almost have it.
$i=0;
echo '<div class="subcontainer">';
foreach($cat->sub as $child) {
$i++;
echo $child->name; // I'm assuming this is what contains your "sub1"-"sub10"
if ($i % 5 == 0) {
echo '</div><div class="subcontainer">';
}
}
echo '</div>';
You can also get $iwith foreach ($cat->sub as $i=>$child)