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>
Related
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>
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>
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 am trying to generate the bellow menu dynamically using php and mysql
<ul id="prod_nav" class="clearfix">
<!-- top --> <li class="active"><span class="down">Clothes</span>
<ul>
<h1> Men </h1>
<li>Shirt </li>
<li>T-shirt </li>
<li>Polo shirt </li>
<li>Formal shoes </li>
<li>Sport shoes </li>
<li>Suit </li>
<li>Underwear </li>
<li>Socks </li>
<li>Pants </li>
</ul>
<ul>
<h1> Women </h1>
<li>Shirt </li>
<li>T-shirt </li>
<li>Polo shirt </li>
<li>High heel shoes </li>
<li>Sport shoes </li>
<li>Wedding clothes </li>
<li>Underwear </li>
<li>Leather </li>
<li>Socks </li>
<li>Pants </li>
</ul>
</li>
</ul>
but I am not sure which way is the best principals for generating the menu?
should I use while loop and if or Case or for loop?
which way is the best way?
Thanks
You should have an array
$a=array('shirt','t-shirt','polo shirt','formal shoes','sport shoes','suit','underwear','socks','pants');
and use it like this:
<ul>
<h1>MEN</h1>
<?php foreach($a as $val) :?>
<li><?php echo $val; ?></li>
<?php endforeach ;?>
</ul>
<ul>
<h1>womaen</h1>
<?php foreach($a as $val) :?>
<li><?php echo $val; ?></li>
<?php endforeach ;?>
</ul>
$gender = array('men','women');
$productlist = array('shirt','t-shirt','polo shirt','formal shoes','sport shoes','suit','underwear','socks','pants');
foreach($gender as $individual) {
echo "<h2>{$individual}</h2>";
echo "<ul>";
foreach($productlist as $product) {
<li><?php echo $product; ?></li>
}
echo "</ul>";
}
can you please used this:
$gender = array('men','women');
$productlist = array('shirt'=>'shirt_url.php','t-shirt'=>'t-shirt_url.php','polo shirt'=>'polo_shirt.php');
foreach($gender as $individual) {
echo "<h2>{$individual}</h2>";
echo "<ul>";
foreach($productlist as $product_key=>$product_url) {
?>
<li><?php echo $product_key; ?></li>
<?php
}
echo "</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";
}
?>