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>
Related
I created a database to store a menu informations with 3 level (2 sub menus level).
You can see it on the left here : https://volt-services.fr/sites/stge/index.php#
Here is the table in the database:
And the code to translate it to HTML:
<?php
$req = $DB->query("SELECT * FROM menu ORDER BY ordered");
foreach ($req as $r) {
if($r->level == 1) {
$tab[$r->ordered] = array($r->id,$r->name,$r->class,$r->icon);
} elseif ($r->level == 2) {
$tab[$r->parent][4][$r->ordered] = array($r->id,$r->name);
} elseif ($r->level == 3) {
$tab[$r->cat][4][$r->parent][2][$r->ordered] = array($r->id,$r->name);
}
}
foreach ($tab as $key => $value) {
?>
<li data-menu="<?= $value[0]; ?>" class="<?= $value[2]; ?> menu-item">
<span class="material-icons-outlined"><?= $value[3]; ?></span>
<span class="titre_menu"> <?= $value[1]; ?></span>
<ul id="sub_menu<?= $value[0]; ?>" class="box_sub_exp <?= $value[2]; ?>">
<?php foreach ($value[4] as $ke => $val) { // SUB MENU POSITION 4 ?>
<li class="<?= $value[2]; ?> submenu-item"><?= $val[1]; ?>
<?php if(!empty($val[2])) { // IS A MENU IN POSITION 2 ?>
<ul class="box_sub_sub_exp">
<?php foreach ($val[2] as $k => $v) { // SUB MENU POSITION 2 ?>
<li class="<?= $value[2]; ?> submenu-item"><?= $v[1]; ?></li>
<?php } ?>
</ul>
<?php } ?>
</li>
<?php } ?>
</ul>
</li>
<?php } ?>
</ul>
</nav>
</section>
On the first sub menu everything is OK. But on the second, the first line is missing, on the third 2 first lines are missing, etc...
I think there is a problem with my loop, could you please help me to find it ?
FIXED : the problem was in the request, not "ORDER BY ordered" but by "id"
Even if you set a numeric key with $r->ordered, you have to sort your table before your display loop !
Give a numeric key to a table line doesn't "auto-sort" it on loop.
Try using ksort() on $tab before your display foreach. So it's will use keys you've define and sort your table in order.
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>
Is there a more elegant way to output the given html structure with an array? I found some recursive solutions but therefore a parent_id is needed (if I understand correctly).
HTML
<ul>
<li>Level 1</li>
<li>
<ul>
<li>Level 2</li>
<li>
<ul>
<li>Level 3</li>
</ul>
</li>
</ul>
</li>
</ul>
Array
Array
(
[0] => Level 1
[1] => Array
(
[0] => Level 2
[1] => Array
(
[0] => Level 3
)
)
)
Loops
<ul>
<?php foreach($array as $arr) : ?>
<li>
<?php if(is_array($arr)) : ?>
<ul>
<?php foreach($arr as $a) : ?>
<li>
<?php if(is_array($a)) : ?>
<ul>
<?php foreach($a as $aa) : ?>
<li><?php echo $aa; ?></li>
<?php endforeach; ?>
</ul>
<?php else : ?>
<?php echo $a; ?>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
<?php else : ?>
<?php echo $arr; ?>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
I´m a bit worried about the performance of this approach :-D
do it just recursive:
<?PHP
function doOutputList($TreeArray)
{
echo '<ul>';
foreach($TreeArray as $arr)
{
echo '<li>';
if(is_array($arr))
{
doOutputList($arr);
}
else
{
echo $arr;
}
echo '</li>';
}
echo '</ul>';
}
doOutputList($array);
?>
Or if you like good readable HTML Code try this:
<?PHP
function doOutputList($TreeArray, $deep=0)
{
$padding = str_repeat(' ', $deep*3);
echo $padding . "<ul>\n";
foreach($TreeArray as $arr)
{
echo $padding . " <li>\n";
if(is_array($arr))
{
doOutputList($arr, $deep+1);
}
else
{
echo $padding .' '. $arr;
}
echo $padding . " </li>\n";
}
echo $padding . "</ul>\n";
}
doOutputList($array);
?>
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>
Hello :) I realy need your help here. I dynamically generate list of items but instead of putting each item in separate <li> I want to get something like this:
<ul>
<li>
<div>$pt</div>
<div>$pt</div>
<div>$pt</div>
</li>
<li>
<div>$pt</div>
<div>$pt</div>
<div>$pt</div>
</li>
</ul>
Here is code I have:
<ul class="some-ul-class">
<?php $itemCount = 3; ?>
<?php $i=0; foreach ($p->getItems() as $pt): ?>
<?php if ($i++%$itemCount==0): ?>
<li class="item">
<?php endif; ?>
<div>$pt</div>
</li>
<?php endforeach; ?>
</ul>
But as the result I get structure like this:
<ul>
<li>
<div>$pt</div>
</li>
<div>$pt</div>
<div>$pt</div>
<li>
<div>$pt</div>
</li>
<div>$pt</div>
<div>$pt</div>
</ul>
Thank you for help
<ul class="some-ul-class">
<?php $itemCount = 3; ?>
<?php $i=0; foreach ($p->getItems() as $pt): ?>
<?php if ($i%$itemCount==0): ?>
<li class="item">
<?php endif; ?>
<div>$pt</div>
<?php if ($i%$itemCount==2): ?>
</li>
<?php endif; $i++; ?>
<?php endforeach; ?>
</ul>
You can try this.
<ul class="some-ul-class">
<?php $itemCount = 3;
$i=0;
foreach ($p->getItems() as $pt):
if ($i%$itemCount==0):
echo '<li class="item">';
endif;
echo "<div>$pt</div>";
if ($i%$itemCount==2):
echo '</li>';
endif; $i++;
endforeach; ?>
</ul>
Try something like this:
<ul class="some-ul-class">
<?php $itemCount = 4; ?>
<li>
<?php $i = 1; foreach ($p->getItems() as $pt): ?>
<?php if ( $i % $itemCount == 0): ?>
</li><li>
<?php endif; ?>
<?php $i++; ?>
<div><?php echo $pt; ?></div>
<?php endforeach; ?>
</li>
</ul>
This generates:
<ul class="some-ul-class">
<li>
<div>1</div>
<div>2</div>
<div>3</div>
</li><li>
<div>4</div>
<div>5</div>
<div>6</div>
</li>
</ul>
Demo
Your code will not achieve nested DIVS in LI because you need a multidimensional array to nest the items within the container. The solution is to break the initial DB result set in to chunks with array chunk.
This just splits your array (1,2,3,4,5,6) to ([0] => array(1,2,3), [2] => array(4,5,6)
Run through the loop below you will get two LI with 3 nested DIV. The code is not tested but should be something like operational.
<?php
$items = array(1,2,3,4,5,6,8,9,10,11,12,13,14,15);
// Your initial item array
$rows = 3;
// Number of rows in each li
$items = array_chunk($items, $rows);
// Final nested array in blocks of 3
if ($items) {
echo "<ul class='some-ul-class'>\n";
foreach ( $items as $item ) {
echo "<li class='items'>\n";
foreach ($item as $divs) {
echo "<div>{$divs}</div>\n";
}
echo "</li>\n";
}
echo "</ul>\n";
}
?>