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

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>

Related

How to show 3 <li> in one row

I need to show 3 images in each row in <ul> tag. I have multiple <ul> tags and in each <ul> tag, I am showing 6 images. Now i have to show 3 <li> in first row and next 3 <li> in second row. Then same for next <ul>. I need to break row after 3 <li>. This is my code :
<div class="sets">
<?php foreach ($sets as $set => $items) : ?>
<ul class="set test-set">
<?php $i=0; foreach ($items as $thumb) : ?>
<?php
/* Prepare Image */
$content = '<img src="'.$thumb['cache_url'].'" width="'.$thumb['width'].'" height="'.$thumb['height'].'" alt="'.$thumb['filename'].'" />';
?>
<?php if($i === 0):
echo '<li><div>'; ?>
<?php endif; ?>
<?php echo $content; ?>
<?php if($i === 2): $i = 0; ?>
<?php else: $i++; endif; ?>
</div></li>
<?php endforeach; ?>
</ul>
<?php endforeach; ?>
</div>
http://jsfiddle.net/z4Q48/
li{
float: left;
}
li:nth-child(3n+4){
clear: both;
}
see http://css-tricks.com/how-nth-child-works/

Set a tab to active by adding a class to it using php [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
What would be a good way to set a tabs class using php? There's no jQuery involved here, only php. I get the tabs name from the script. Based on the $tab, I need to set the <li> with that name to class="active". Any good ideas on how I can do this?
$tab = 'movies';
<ul class="nav nav-tabs">
<li>Books</li>
<li>Music</li>
<li class="active">Movies</li>
<li>Others</li>
</ul>
I tried doing this using str_replace, but that's all very clumsy and ugly.
I'm not a big proponent of directly mixing HTML & PHP, but how about something like this:
$options = array("books", "music", "movies", "other");
$tab = 'music';
echo '<ul class="nav nav-tabs">';
foreach($options as $o) {
if($o == $tab) {
echo '<li class="active">'.ucfirst($o).'</li>';
} else {
echo '<li>'.ucfirst($o).'</li>';
}
}
echo '</ul>';
You could also use PHP's alternative syntax if you are inside the scope of a template:
<?php
$options = array("books", "music", "movies", "other");
$tab = 'music';
?>
<ul class="nav nav-tabs">
<?php foreach($options as $o): ?>
<?php if($o == $tab): ?>
<li class="active"><?php echo ucfirst($o); ?></li>
<?php else: ?>
<li><?php echo ucfirst($o); ?></li>
<?php endif; ?>
<?php endforeach; ?>
</ul>
<?php $tabs = array('Books', 'Music', 'Movies', 'Others'); ?>
<ul>
<?php foreach ($tabs as $tabName): ?>
<?php $class = ''; ?>
<?php if ($tab === $tabName) $class = ' class="active"'; ?>
<li<?php echo $class?>><?php echo $tabName?></li>
<?php endforeach?>
</ul>
$tab = 'movies';
<ul class="nav nav-tabs">
<li <?php if($tab === "Books") { echo class='active'; } ?>>Books</li>
<li>Music</li>
<li class="active">Movies</li>
<li>Others</li>
</ul>
and so on
How are you generating the navigation? Based on a database? In that case, while retrieving the laels from the database, you can compare them. Otherwise, generate them from an array with the labels, loop through the labels to generate the menu:
foreach($labels as $label)
{
if($label == $tab)
{// output active}
else
{regular labels}

create new ul after every 13 li elements

First of all, I don't know how to count in php, maybe someone could recommend me a good source to read;
Second, I'm not asking to solve this for me, but I just want a hint or simpler explanation that would make sense;
Here is my function:
<ul>
<?php foreach ($categories as $category) { ?>
<li>
<p><?php echo $category['name']; ?></p>
<?php if ($category['children']) { ?>
<div>
<?php for ($i = 0; $i < count($category['children']);) { ?>
<?php $j = $i + ceil(count($category['children']) / $category['column']); ?>
<?php for (; $i < $j; $i++) { ?>
<?php if (isset($category['children'][$i])) { ?>
<ul>
<li class="none"><h1><?php echo $category['children'][$i]['name']; ?></h1></li>
<?php if ($category['children'][$i]['children_level2']) { ?>
<?php for ($wi = 0; $wi < count($category['children'][$i]['children_level2']); $wi++) { ?>
<li>
<?php echo $category['children'][$i]['children_level2'][$wi]['name']; ?>
</li>
<?php } ?>
<?php } ?>
</ul>
<?php } ?>
<?php } ?>
<?php } ?>
</div>
<?php } ?>
</li>
<?php } ?>
</ul>
I want this part, every 13 li elements to create a new .ul. ./ul. tags
<?php if (isset($category['children'][$i])) { ?>
<ul>
<li class="none"><h1><?php echo $category['children'][$i]['name']; ?></h1></li>
<?php if ($category['children'][$i]['children_level2']) { ?>
<?php for ($wi = 0; $wi < count($category['children'][$i]['children_level2']); $wi++) { ?>
<li>
<?php echo $category['children'][$i]['children_level2'][$wi]['name']; ?>
</li>
<?php } ?>
<?php } ?>
</ul>
<ul><li>maximum of 13 elements</li></ul>
after 13 <li></li> elements create new <ul></ul> tags and put the 14 <li></li> element into the new <ul></ul> tag
I hope I explained what I want to do, for now I'll be waiting for your answers,
p.s. this is more for my learning skills then actual work, so thanks in advice
It's simple. Use condition
if ($wi && $wi % 13 == 0) {
print '</ul><ul>';
}
In order to count 13 elements, you can use the modulo operator %. The idea is that ($iw % 13) is equal to 0 if the content of $iw is a multiple of 13. (see more about the modulo operation)
With an if statement you can insert </ul><ul> when you need in your for loop.
There's two possibilities I can think of:
you could have a separate counter wich resets to 1 and outputs a <ul> every time it reaches 13
or you could check each time if $wi id divisible by 13:
<?php
for ($wi = 0; $wi < count($category['children'][$i]['children_level2']); $wi++) {
if ($wi && $wi % 13 == 0) {
// output <ul>
}
}
?>
If you're unsure what % means, here's the appropriate PHP manual page

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

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