Group items in while loop - php

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

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>

removed foreach item doesn't keep count correctly

<ul>
<?php foreach (array_slice($items, 0, 3) as $item): ?>
<?php if (!isset($item->story)): ?> <!-- hide story updates -->
<li>
<?php if (isset($item->message)) echo $item->message; ?>
</li>
<?php endif; ?>
<?php endforeach; ?>
</ul>
I am displaying a news feed, and only want to display the items without a story update. So i am using !isset to check if a story item is present, and hide the item if it is.
This works perfectly, but now I am trying to display 3 results in the feed, but i think the story items are still being counted in the foreach, so when the last 3 updates are story items, nothing displays in the feed.
I have read this question which looks like what I am trying to do, but I dont understand where he has got the $elementkey variable from, and what it does?
How do I remove the item from the foreach, and remove its count so 3 items will display?
This should work for you:
(Here i just added a $count variable which i only increment if a message get's posted. And after 3 i break the foreach)
<ul>
<?php $count = 0;
foreach ($items as $item): ?>
<?php if ($count >= 3) break; if (!isset($item->story)): ?> <!-- hide story updates -->
<li>
<?php if (isset($item->message)) {
echo $item->message;
$count++;
}
?>
</li>
<?php endif; ?>
<?php endforeach; ?>
</ul>

List items inside php

So i have this in html
<ul>
<li>Home</li>
<li>Stuff</li>
<li>Other stuff</li>
</ul>
And i want to add this php code in the list:
<?php
...
elseif($_SESSION['logged']==false)
echo 'Login</li>';
echo 'Register</li>';
?>
But instead of displaying 2 separate list items, one with Login and one with Register i only get one with those two links.
What can I do?
You missed starting li tags and brackets.
do this:
<?php
...
elseif($_SESSION['logged']==false) {
echo '<li>Login</li>';
echo '<li>Register</li>';
}
?>
If $_SESSION['logged'] is going to be not set or false then use empty(), also your missed you opening li tags:
<ul>
<li>Home</li>
<li>Stuff</li>
<li>Other stuff</li>
<?php if(empty($_SESSION['logged'])): ?>
<li>Login</li>
<li>Register</li>
<?php endif; ?>
</ul>
You have forgotteh curly bracket { and li
<?php
...
elseif($_SESSION['logged']==false) {
echo '<li>Login</li>';
echo '<li>Register</li>';
}
?>

PHP: create row and colum class in li [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
there are 20 li
<ul>
<li id="1"></li>
<li id="2"></li>
<li id="3"></li> <!--- Row1--->
<li id="4"></li>
<li id="5"></li>
<li id="6"></li><!--- Row2--->
<li id="7"></li>
<li id="8"></li>
<li id="9"></li><!--- Row3--->
<li id="10"></li>
<li id="11"></li>
<li id="12"></li><!--- Row4--->
</ul>
i want to give class to first 3 li element 1,2,3 to row 1
li 4,5,6 to row 2
is it possible with li??
<?php $i=0;?>
<ul>
<?php foreach($data as $da):?>
<li id="<?php echo $i+1;?>"></li>
<?php $i++; ?>
<?php endforeach; ?>
</ul>
<style>
.row { float:left; }
.row > ul { display:inline-block; }
</style>
<ul>
<?php
$i=0;
foreach($data as $da){
if($i%3===0){
echo ($i!==0 ? '</ul>' : null );
echo "<li class='row'><ul><li id='".($i+1)."'></li>";
}else{
echo "<li id='".($i+1)."'></li>";
}
$i++;
}
?>
</ul>
You may also need to introduce the clearfix class.
I'm not really sure what you are trying to do, but i understand i want to give class to first 3 li element.
So why not check your iteration if it's lower as 3 ?(iteration 0, 1 , 2)
<ul>
<?php foreach($data as $da):?>
<?php if($i < 3) :?>
<li class="yourClass" id="<?php echo $i+1;?>">Got class</li>
<?php else :?>
<li id="<?php echo $i+1;?>">No class</li>
<?php endif;?>
<?php $i++; ?>
<?php endforeach; ?>
</ul>
If you want to have from every 'row' the first 3 li's. Well there no rows in an unorded list. You can just genereate tables for this or you can create multiple <ul>s that will stand for an 'row'.
Why dont you close your ul, and then bring another ul. Then float left your ul's with a right spacing.
<?php $i=0;?>
<?php $x=0;?>
<ul>
<?php foreach($data as $da):?>
<li id="<?=$i+1;?>"></li>
<?php if ($x == 2) {
echo "</ul><ul>";
$x = 0;
?>
<?php $i++; $x++ ?>
<?php endforeach; ?>
</ul>
There are no 'Rows' as such in a Unordered List.
You can certainly apply classes to the elements based on the sequence and could have a 'rowEnd' or 'rowStart' class applied to the last or first list item in that row without much hassle, but what are you trying to acheive by grouping them in rows?
A table sounds like a more natural fit to this type of data perhaps?
Amended Answer:
<?php
$data = array();
$data = array_pad($data, 50, 'x'); // Just creating some dummy data..
?>
<?php $i=0;
$row = 1; // First row as default...
?>
<ul>
<?php foreach($data as $da):?>
<?php
if(($i > 2 && $i % 3 == '0')) { // Increment where $i is divisble by 3
$row++;
}
?>
<li class="row<?=$row?>" id="<?php echo $i+1;?>"><?=$da?></li>
<?php $i++; ?>
<?php endforeach; ?>
</ul>

How Not to Display a seperator or a border after last data of the list?

How Not to Display a separator or a border after last data of the list?
I have a list of data which is coded in the form of <ul> and <li>
and it is formatted in the way that after every record a border or separator needs to be displayed but after last record this border should not be displayed.
I am aware of the fact that it has to be coded with the help of for loop, but I cannot catch the logic for that.
I am working in PHP and the data is being fetched from Mysql DB
below is the format of data to be displayed and in it, the last <li> is for displaying separator
UPDATED
for ($i=0, $n=sizeof($order->products); $i<$n; $i++)
{
<ul>
<li class="col1"><?php echo $order->products[$i]['qty']; ?></li>
<li class="col2"><?php echo $product_image; ?></li>
<li class="col3"><?php echo $product_name; ?></li>
<li class="col4"><?php echo $currencies->display_price($finalprice, $order->products[$i]['tax'], $order->products[$i]['qty']); ?></li>
<li class="dotted-border"></li>
</ul>
}
Unless I misunderstood it:
if($i<$n-1)
echo"<li class="dotted-border"></li>";
You have an empty list item that's there for nothing but a border. Get rid of it. What you should be doing is putting a border at the top of every <ul> except the first one, and you can do that in CSS alone (without having to worry about putting presentation into your markup).
#parentContainer ul + ul {border-top: dotted black 1px;}
That being said, a more appropriate structure for what you are doing is a table rather than a list. You don't need to display it as a table, but that's exactly what it is from a semantic point of view.
The following should work:
<?php for ($i=0, $n=sizeof($order->products); $i<$n; $i++): ?>
<ul>
<li class="col1"><?php echo $order->products[$i]['qty']; ?></li>
<li class="col2"><?php echo $product_image; ?></li>
<li class="col3"><?php echo $product_name; ?></li>
<li class="col4"><?php echo $currencies->display_price(
$finalprice, $order->products[$i]['tax'],
$order->products[$i]['qty']
); ?></li>
<?php if($i == $n-1) ?>
<li class="dotted-border"></li>
<?php endif; ?>
</ul>
<?php endfor; ?>
However why dont you add the dotted-border class to the ul instead of having an empty element?
a) either pack all strings in an array and the implode('<.. border ..>', $array) them
b) if ($numberOfCurrentItem < count($order->products)) { displayBorder(); }

Categories