I have an array
$foo = array(1,2,3,4,5,6,7,8,9);
when I use foreach loop for this
foreach($foo as $val):
print '<li>'.$val.'</li>';
endforeach
Out put is ,
<li> 1 </li>
<li> 2 </li>
<li> 3 </li>
<li> 4 </li>
<li> 5 </li>
But I want out put something like that
<li> 1, 2 </li>
<li> 3, 4 </li>
<li> 5, 6 </li>
<li> 7, 8 </li>
Is it possible?
$foo = array(1,2,3,4,5,6,7,8,9);
foreach (array_chunk($foo, 2) as $chunk) {
echo "<li>" . implode(', ', $chunk) . "</li>\n";
}
For php you can do:
for($i=0;$i<count($foo);$i+=2) {
echo "<li>{$foo[$i]}, {$foo[$i+1]}</li>";
}
This might be what you require:
foreach($foo as $key=>$val) {
if ($val&1) {
echo '<li>' . $val;
if($key == (count($foo)-1)){
echo '</li>';
}
} else {
echo ',' . $val . '</li>';
}
}
Please try this:
$foo = array(1,2,3,4,5,6,7,8,9);
$i=1;
$firstElement = "";
foreach($foo as $val):
if($i%2==0)
{
print '<li>'.$firstElement.','.$val.'</li>';
}
else
{
$firstElement = $val;
}
$i++;
endforeach
Related
I am display data from the database. Currently, I have 6 records in my database and I am getting my output like
<ul>
<li>Records1</li>
<li>Records2</li>
<li>Records3</li>
<li>Records4</li>
<li>Records5</li>
<li>Records6</li>
</ul>
Now what I am doing is, I have to close the </ul> tag after 4th li tag and then start new ul after 4th li.
My expected output is
<ul>
<li>Records1</li>
<li>Records2</li>
<li>Records3</li>
<li>Records4</li>
</ul>
<ul>
<li>Records5</li>
<li>Records6</li>
</ul>
is it possible?
I am using below code
<?php
if ($tyler_query->have_posts()) {
$index = 0;
$check=0;
$first4=0;
while ( $tyler_query->have_posts() ) {
$tyler_query->the_post();
if ($index < 4) {
if ($first4==0){?>
<ul>
<?php $first4=1;}?>
<li>
<!--output here-->
</li>
<?php if ($first4==4){?>
</ul>
<?php }?>
<?php }
else {
if ($check==0){?>
<ul>
<?php $check=1;}?>
<li>
<!--output here-->
</li>
<?php } $index++;}?>
</ul>
<?php }?>
You can just insert a closing tag followed by an opening tag, whenever it meets your condition. In the following after every third item:
<?php
$items = [
'Syd',
'Roger',
'Nick',
'David',
'Richard'
];
$i = 0;
echo '<ul>';
foreach($items as $item) {
if($i++%3 == 0)
echo '</ul><ul>';
echo '<li>' . $item . '</li>';
}
echo '</ul>';
Output:
<ul><li>Syd</li><li>Roger</li><li>Nick</li></ul><ul><li>David</li><li>Richard</li></ul>
It's quick example. Hope help you.
if ($tyler_query->have_posts()) {
$index = 0;
$check = 6;
?>
<ul>
<?php while ($tyler_query->have_posts()) {
$tyler_query->the_post(); ?>
<li><?php echo 'some_value' ?></li>
<?php if ($index % $check === 0 ) { ?>
</ul><ul>
<?php }
$index++;
} ?>
</ul>
<?php } ?>
your code would work if you echo the HTML tag/output you want to see in the browser.
<?php
if ($tyler_query->have_posts()) {
$index = 0;
$check = 0;
$first4 = 0;
while ($tyler_query->have_posts()) {
$tyler_query->the_post();
$output = "whatever the output object is";
if ($index < 4) {
if ($first4 == 0) {
echo '<ul>';
$first4 = 1; // increment so that the this if block wont trigger again
}
echo '<li>' . $output . '</li>';
// increment so that the next if block trigger once
if ($first4 == 4) {
echo '</ul>';
}
$first4++;
}
if ($index >= 4){
if ($check == 0) {
echo '<ul>';
$check = 1;
}
// assuming you want to have the rest of the data in this block.
// data 5 and above
else {
echo '<li>' . $output . '</li>';
}
}
$index++;
}
echo '</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>
Working with php -
Im trying to run a "foreach" on an array, but i want to wrap every two results in li tags. Output will look like this.
<li>
<div>result 1</div>
<div>result 2</div>
</li>
<li>
<div>result 3</div>
<div>result 4</div>
</li>
<li>
<div>result 5</div>
<div>result 6</div>
</li>
how do i go about doing this?
Thanks!
$chunks = array_chunk($arr, 2);
foreach ($chunks as $chunk) {
// $chunk could have either 2 elements, or just one on the last iteration on an array with odd number of elements
echo '<li>';
foreach ($chunk as $value) {
echo '<div>' . $value . '</div>';
}
echo '</li>';
}
$results = array(1, 2, 3, 4, 5, 6);
echo "<li>";
foreach($results as $pos => $result) {
if ($pos > 2 && $pos % 2 == 0) {
echo "</li>\n<li>";
}
echo "<div>result $result</div>";
}
echo "</li>";
or more simply:
$results = array(1, 2, 3, 4, 5, 6);
$max = count($results);
for($i = 0; $i < $max; $i++) {
echo "<li>";
echo "<div>result " . $results[$i] . "</div>";
$i++;
echo "<div>result " . $results[$i] . "</div>";
echo "</li>";
}
$count = 0;
foreach ($array as $key=>$value) {
++$count;
if ($count == 1) {
echo "<li>";
echo "<div>" . $value."</div>";
} else {
echo "<div>" . $value."</div>";
echo "</li>";
$count = 0;
}
}
I want add left, center & right class to my ordered lists while loop.
Code
<?php while ($fetch) { ?>
<li>haha</li>
<?php } ?>
Results should be
<ul>
<li class="left">haha</li>
<li class="center">haha</li>
<li class="right">haha</li>
<li class="left">haha</li>
<li class="center">haha</li>
<li class="right">haha</li>
</ul>
Let me know
<?php $classes = array("left","center","right");
$i = 0;
while ($fetch) {
?>
<li class="<?php echo $classes[$i++ % 3] ?>">haha</li>
<?php } ?>
$cnt=0;
while ($fetch)
{
switch ($cnt%3)
{
case 0 : $class = 'left'; break;
case 1 : $class = 'center'; break;
case 2 : $class = 'right'; break;
}
echo '<li class="', $class, '">haha</li>';
++$cnt;
}
I've just tested the following code and verified that it produces the desired output:
<?php
$items = array('haha', 'haha', 'haha', 'haha', 'haha', 'haha');
$cssClasses = array('left', 'center', 'right');
echo "<ul>\n";
$i=0;
foreach ($items as $item) {
echo "\t<li class=\"" . $cssClasses[$i++ % 3] . '">' . $item . "</li>\n";
}
echo "</ul>\n";
?>
The output is:
<ul>
<li class="left">haha</li>
<li class="center">haha</li>
<li class="right">haha</li>
<li class="left">haha</li>
<li class="center">haha</li>
<li class="right">haha</li>
</ul>