margin list after 5 li items with php - php

I hope topic title will be okey, doesnt really find good idea for naming topic. What i want is to produce that my UL list will get some class or margin after a few LI in list.
Something like that
<ul>
<li></li>
<li></li>
<li></li>
</ul>
<ul class="1">
<li></li>
<li></li>
<li></li>
</ul>
<ul class="2">
<li></li>
<li></li>
<li></li>
</ul>
Now what i have in code is something like that, but offcourse doesnt work...:
$datas = array('a','b','c','d','e','f','g','h');
$countData = count($datas);
for($i = 0; $i < $countData; $i++){
echo '<ul>';
$j = 0;
foreach($datas as $data){
$j++;
if($j < 3){
echo '<li>'.$data.'</li>';
}
}
echo '</ul>';
}

Guess you can try this:
$datas = array('a','b','c','d','e','f','g','h');
echo "<ul>\n";
$class = 0;
foreach ($datas as $i => $data) {
echo "<li>$data</li>\n";
if (($i+1) % 3 == 0)
echo "</ul>\n<ul class=".++$class.">\n";
}
echo "</ul>";
It produces:
<ul>
<li>a</li>
<li>b</li>
<li>c</li>
</ul>
<ul class=1>
<li>d</li>
<li>e</li>
<li>f</li>
</ul>
<ul class=2>
<li>g</li>
<li>h</li>
</ul>

Related

Displaying each two array items in one loop item with right placement

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;
}

<UL> basesd on common id fetch from databse

I'm trying to iterate through my array and group li's with their relative ul, based on a common id.
expected result is as follows
<ul>
<li>b</li>
<li>c</li>
<li>a</li>
<li>g</li>
<li>e</li>
</ul>
<ul>
<li>d</li>
<li>f</li>
<li>i</li>
</ul>
<ul>
<li>d</li>
<li>f</li>
<li>i</li>
</ul>
I have tried following code
<?php
$IMPLODED_trid = 1,2,3,4,5,6 ;
$result=mysqli_query('SELECT * FROM tablegroup where id IN ($IMPLODED_trid)');
while($row=mysqli_fetch_array($result))
{
$name=$row['name'];
?>
<ul>
<li><?php $name ;?></li>
</ul>
<?php
}
?>
but above code gives following result
<ul>
<li>c</li>
</ul>
<ul>
<li>a</li>
</ul>
<ul>
<li>d</li>
</ul>
<ul>
<li>i</li>
</ul>
You'll want to use two loops.
One outer for generating the <ul> elements, another for generating the <li> elements within the <ul>
Such code might look like this
<?php
$result = []; //grouped by ul ID
for ($i = 1; $i < count($ids); $i++) {
echo '<ul>';
while ($row = $result[$i])
echo '<li>' . $result[$i]['name'] . '</li>';
echo '</ul>';
}
?>
It's valid PHP but it doesn't work ofcourse.
This is the structure you're looking for tho since you'll want an outer loop to go through the amount of <ul> elements you need and within that loop you'll want to loop through the list-items themselves.
The outer loop wouldn't be needed if you only had one <ul> ofcourse.
Your code is wrong. Put ul tag out of the body of while loop. Try this:
<ul>
<?php
$IMPLODED_trid = 1,2,3,4,5,6 ;
$result=mysqli_query('SELECT * FROM tablegroup where id IN ($IMPLODED_trid)');
while($row=mysqli_fetch_array($result))
{
$name=$row['name'];
?>
<li><?php $name ;?></li>
<?php
}
?>
</ul>

foreach every 3 put inside a new ul

Actually I have this foreach loop:
<ul>
<?php foreach ( $tasks as $task ) { ?>
<li>
...
</li>
<?php } ?>
</ul>
It returns:
<ul>
<li>...</li>
<li>...</li>
<li>...</li>
<li>...</li>
...
</ul>
Now I need to change the loop to put every 3 <li> into a new <ul>, so I can have:
<div>
<ul>
<li>...</li>
<li>...</li>
<li>...</li>
</ul>
<ul>
<li>...</li>
<li>...</li>
<li>...</li>
</ul>
...
</div>
How I have to modify the foreach loop to achieve that result?
This should work for you:
Just array_chunk() your array into chunks of 3 and then implode() each subArray, e.g.
<div>
<?php
foreach (array_chunk($tasks, 3) as $task) {
echo "<ul><li>" . implode("</li><li>", $task) . "</li></ul>";
}
?>
</div>
try this
foreach (array_chunk($arrays, 3, true) as $array)
{
echo '<ul>';
foreach($array as $value)
{
echo "<li>".$value."</li>";
}
echo '</ul>';
}
Please try this..
<ul>
<?php foreach ( $tasks as $k=>$task ) { ?>
<?php if($k % 3 == 1) { ?>
<ul>
<?php } ?>
<li></li>
<?php if($k % 3 == 0) { ?>
</ul>
<?php } ?>
<?php } ?>
</ul>
I like #Rizer123's answer for conciceness, but if you wish to keep iterating over the individual items, rather than chunking (for example; if you need to perform some manipulation on the individual items) I'd suggest something like the following:
<ul>
<?php foreach ( array_values($tasks) as $k => $task ) {
if($k % 3 == 0) { ?>
<ul>
<?php } ?>
<li>
...
</li>
if($k % 3 == 2) { ?>
</ul>
<?php } ?>
<?php } ?>
</ul>

Nested ul li with a php array and a specific count

I have 10 users in an array:
$array = array(
"aaa",
"bbb",
"ccc",
"ddd",
"eee",
"fff",
"ggg",
"hhh",
"iii",
"jjj",
);
And I want to display lists based on 5 or fewer users e.g.:
<ul>
<li>
<ul>
<li>aaa</li>
<li>bbb</li>
<li>ccc</li>
<li>ddd</li>
<li>eee</li>
</ul>
</li>
<li>
<ul>
<li>fff</li>
<li>ggg</li>
<li>hhh</li>
<li>iii</li>
<li>jjj</li>
</ul>
</li>
</ul>
At the moment I have:
<ul>
<?php foreach($users as $user): ?>
<li><?php echo $user ?></li>
<?php endforeach; ?>
</ul>
However I am not creating the inner uls. What is the best way to approach this? Using a for loop and counting out 5? Or is there a neater method?
Use array_chunk() to split an array into multiple arrays with a specified number of items.
<ul>
<?php
$users = array_chunk($array, 5);
foreach ($users as $user) {
echo "<li><ul>";
foreach ($user as $idv) {
echo "<li>" . $idv . "</li>";
}
echo "</ul></li>";
}
?>
</ul>
Look an alternative solution :)
<li>
<ul>
<?php
echo '<li>'.join('</li><li>',array_slice($array,0,5)).'</li>';
?>
</ul>
</li>
<li>
<ul>
<?php
echo '<li>'.join('</li><li>',array_slice($array,5,10)).'</li>';
?>
</ul>
</li>
<ul>
<?php
$lenght = count($users);
for( $i=0; $i<$lenght; $i++ ){
if( $i%5 == 0 ){
echo '<li><ul>';
}
echo '<li>'.$users[0].'</li>';
if( $i%5 == 4 || ($i+1==$lenght) ){
echo '</ul></li>';
}
}?>
</ul>

How do I add UL LI every two loops on while

I want to add my ul & li every two loop.. Example
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<ul>
<li> <?php the_title() ?> - <?php the_content() ?></li>
</ul>
<?php endwhile; ?>
Let's say I have 4 posts and I want the result should be like this
<ul>
<li>Title 1 - content 1</li>
<li>Title 2 - content 2</li>
</ul>
<ul>
<li>Title 3 - content 3</li>
<li>Title 4 - content 4</li>
</ul>
add a counter variable (start =0) that increments at the end of each pass through the loop. Then at the beginning of each pass, test if($counter%2==0){ echo "</ul><ul>";}and put the first <ul> and last </ul> outside of the loop
I would do something like this:
for($i = 0; $i < $numberOfUls; $i++)
{
$result = '<ul>';
for($j = 0; $j < $numberOfLis; $j++)
{
$result .= '<li>Title content</li>'; // Perhaps an array with the whole list $listContent[$i][$j];
}
$result .= '</ul>';
}
echo $result;

Categories