I need this type of structure in PHP foreach loop. and i tried this code :
<ul>
<li>1</li>
</ul>
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
</ul>
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
</ul>
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
</ul>
i tried this condition inside loop and working fine but i need 1 li on first ul.
<?php
$counter = 1;
foreach( $myposts as $post ) :
setup_postdata($post);
if ($counter % 5 == 1) {
echo '<ul>';
}
echo '<li>testing ...</li>'
if($counter % 5 == 0) {
echo '</ul>';
}
$counter++;
endforeach;
?>
any one extend my logic ??
This is what I came up with:
$myposts = array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16);
$counter = 0;
echo '<ul>';
foreach( $myposts as $k=>$post ) :
//setup_postdata($post);
echo '<li>'.$post.'</li>';
if($counter==0 || $counter==5){
echo "</ul>".($k!=count($myposts)-1 ? "<ul>" : "");
$counter = 0;
}
$counter++;
endforeach;
if($counter==5){
echo '</ul>';
}
This largest misconception most people don't bother to close that last tag (in your case ul), or they open a new tag (in your case ul) when they shouldn't.
P.s. I had to comment setup_postdata for this to work.
Note: Adding or removing elements from the array will not effect the structure of 1,5,5,...,(then remainder depending on what is left)
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>
I have the following PHP code that pulls posts from a specific category and displays them in an unordered list.
I'd like to change this so it displays 5 <li>'s in one <ul> then creates a new <ul> for another 5 and so on.
Here is my existing code:
<?php
$args = array( 'posts_per_page' => 15, 'offset'=> 1, 'category' => $cat_ID );
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post );
?>
<li>
<?php the_title(); ?>
</li>
<?php
endforeach;
wp_reset_postdata();
?>
Another way to do it is by using array_chunk, example:
$myposts = [1,2,3,4,5,11,22,33,44,55,111,222,333,444];
foreach (array_chunk($myposts, 5) as $posts) {
echo "<ul>\n";
foreach ($posts as $post) {
echo '<li>' . $post. '</li>'. "\n";
}
echo "</ul>\n\n";
}
Outputs:
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
</ul>
<ul>
<li>11</li>
<li>22</li>
<li>33</li>
<li>44</li>
<li>55</li>
</ul>
<ul>
<li>111</li>
<li>222</li>
<li>333</li>
<li>444</li>
</ul>
Here's a rough example using the modulus operator on every 5th iteration.
$myposts = array(1,2,3,4,5,6,7,8,9,10,11);
$output = '<ul>' . "\n";
foreach ($myposts as $count => $post ) {
if ($count > 1 && $count % 5 == 0 ) {
$output .= '</ul><ul>' . "\n";
}
$output .= '<li>' . $post . '</li>' . "\n" ;
}
rtrim($output, '</ul><ul>' . "\n"); //if it was the tenth iteration we don't want to open another ul
echo $output . '</ul>' . "\n";
Output:
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
</ul><ul>
<li>6</li>
<li>7</li>
<li>8</li>
<li>9</li>
<li>10</li>
</ul><ul>
<li>11</li>
</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>
I want to add some markup in the while loop, so that each three items are wrapped in a <ul> and each of the ul should be wrapped in a div. There can be maximum 6 items, and I want to get following output:
<div class="one">
<ul>
<li>...</li>
<li>...</li>
<li>...</li>
</ul>
</div>
<div class="two">
<ul>
<li>...</li>
<li>...</li>
<li>...</li>
</ul>
</div>
I am trying following code:
<div class="one">
<ul>
<?php
$i = 0 ;
while (have_posts()) : the_post();
$i++; ?>
<li>...</li>
<?php
if ($i === 3){
echo "</ul></div><div class='two'><ul>";
$right_div = true;
}
?>
}
if ($right_div){
</ul></div>
<?php } ?>
?>
It works fine if there are at least 3 items, but if there are less than 3, then breaks the code as it does not close the ul and div.
It is important to use the while loop because its part of a WordPress theme which uses the while loop to get the posts.
Im not sure I get it, but I think the problem is:
if ($right_div){
</ul></div>
<?php } ?>
That should not be a condition, that should be always printed.
If I'm understanding correctly, you're saying you want to
group things in group of 3's, but if you have 11 items or something
like that it breaks. If that's the case, then on the last iteration
of the while loop you should close the ul and div elements.
Like so:
if(condition){
echo "</ul></div>";
}
Taking reference from http://codex.wordpress.org/Function_Reference/have_posts
function more_posts() {
global $wp_query;
return $wp_query->current_post + 1 < $wp_query->post_count;
}
if (!more_posts()){
echo "</ul></div><div class='two'><ul>";
$right_div = true;
}
Oh and i am not a wordpress guy :P
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;