How do I add UL LI every two loops on while - php

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;

Related

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

margin list after 5 li items with 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>

Need first ul has 1 li and each ul has 5 li

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)

Group items in while loop

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

PHP - limiting <li> tags in a <ul>

I have the following HTML output;
<ul>
<li>Test 1</li>
<li>Test 2.</li>
<li>Test 3</li>
<li>Test 4</li>
<li>Test 5</li>
<li>Test 6</li>
<li>Test 7</li>
</ul>
What I need to do, is display the same HTML, but with only the first 4 <li> tags, i.e.
<ul>
<li>Test 1</li>
<li>Test 2.</li>
<li>Test 3</li>
<li>Test 4</li>
</ul>
Is there an easy way I can do this in PHP?
Thanks
EDIT:
The data is coming from:
$data = $product->getDescription();
It is stored in the DB as the HTML content, I am currently displaying it using the above code;
Thanks
Umm yes..
Depending on where the data is coming from, I am going to assume it is a db of some sort and you have it in an array():
$databasevalue = array(); //stuff from database goes here / dragons
echo "<ul>";
for ( $counter = 0; $counter < 4; $counter ++) {
echo "<li>".$databasevalue[$counter]."</li>";
}
echo "</ul>";
Can't offer more without better question details.
How is data coming from ?
If this data comes from an array you can slice it with array_slice function
$list = array_slice($input, 0, 4);
// $list now only having first 4 item.
<?php
echo '<ul>' . PHP_EOL;
for ($i = 1; $i <= 4; $i++)
{
echo '<li>' . $i . '</li>' . PHP_EOL;
}
echo '<ul>' . PHP_EOL;
?>
Outputs:
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<ul>
Someone else answered with a good one (requires SimpleXMLElement, PHP5) but has been deleted:
<?php
$data = '<ul>
<li>Test 1</li>
<li>Test 2.</li>
<li>Test 3</li>
<li>Test 4</li>
<li>Test 5</li>
<li>Test 6</li>
<li>Test 7</li>
</ul>';
$ul = new SimpleXMLElement($data);
$li = $ul->li;
echo '<ul>'.PHP_EOL;
for($i = 0 ; $i < 4 ; $i++)
{
echo '<li>'.$li[$i].'</li>'.PHP_EOL;
}
echo '</ul>'.PHP_EOL;
?>
if you're using database, you should use "LIMIT" in your SQL, otherwise array_slice will do the job.
don't forget: documentation is your best friend (especially in php)

Categories