I have the following code:
$loop = 0;
$columns = 3;
<?php while ( have_posts() ) :
the_post();
$loop++;
?>
<li class="<?php if ( $loop % $columns == 0) echo 'last'; if ( ( $loop - 1 ) % $columns == 0 ) echo 'first'; ?>">
</li>
What I want to achieve is:
<li class="first"></li>
<li class="last"></li>
<li class="first"></li>
<li class=""></li>
<li class="last"></li>
<li class="first"></li>
<li class=""></li>
<li class="last"></li>
What I am getting is (which makes sense..):
<li class="first"></li>
<li class="=""></li>
<li class="last"></li>
<li class="first"></li>
<li class=""></li>
<li class="last"></li>
<li class="first"></li>
<li class=""></li>
<li class="last"></li>
meaning the first 2 are given the class first and last respectively and after that it is every 3.
If i was able to use css I would say nth-child(3n+2) but I can't because I want to assign a new class, and I can only assign a style with css.
How can I achieve it with php?
The following will generate the code that you gave as an example:
<?php
$loop = 0;
$offset = 2;
while ($loop < 100) {
echo '<li class="';
if ($loop < $offset) {
if ($loop % 2 == 0) {
echo 'first';
} else {
echo 'last';
}
} else {
if (($loop - $offset) % 3 == 0) {
echo 'first';
} elseif (($loop - $offset + 2 ) % 3 == 0) {
echo '';
} else {
echo 'last';
}
}
echo '"></li>';
++$loop;
}
If the $loop variable is under the offset it print first and last alternately otherwise it will check divisibility with 3, giving the dividend an extra offset for each condition so they will be true sequentially.
Related
I have to make a small adjustment on a website, the problem comes on the page.
For example I have a page with 11 button, these 1 I do not have to show them at once, but I have for example I have to show the first 5 and then the page has to be moving but I never have to stop showing 5 results on the page.
I leave reference images:
This is the way the page does NOT have to look:
This is the way the page should look:
for example if I get to 7 that 3 is no longer shown and shows me 8
This is my code
<nav>
<ul class="pagination">
<?php if ($pagina == 1) : ?>
<li class="page-item disabled">
<a class="page-link" href="">
<span class="ti-arrow-left"></span>
</a>
</li>
<?php else : ?>
<li class="page-item">
<a class="page-link" href="?pagina=<?php echo $pagina - 1 ?>">
<span class="ti-arrow-left"></span>
</a>
</li>
<?php endif; ?>
<!---------------------------------------------------------------------->
<?php
for ($i = 1; $i <= $numeroDePaginas; $i++) {
if ($pagina == $i) {
echo "<li class='page-item active'>
<a class='page-link' href='?pagina=$i'>$i</a>
</li>";
} else {
if (($i + 2) < $pagina || ($i - 2) > $pagina) {
echo "<li class='page-item'>
<a class='page-link' href='?pagina=$i'>$i</a>
</li>";
}
}
}
?>
<!---------------------------------------------------------------------->
<?php if ($pagina == $numeroDePaginas) : ?>
<li class='page-item disabled'>
<a class='page-link' href=''>
<span class='ti-arrow-right'></span>
</a>
</li>
<?php else : ?>
<li class='page-item'>
<a class='page-link' href="?pagina=<?php echo $pagina + 1 ?>">
<span class='ti-arrow-right'></span>
</a>
</li>
<?php endif; ?>
</ul>
</nav>
Here you go. I have added next_pages variable and previous_pages variables, which will make sure to get the pagina - 2 and pagina + 2 if exists. Let me know if you need help understanding it.
<nav>
<?php
$next_pages = ($pagina + 2) <= $numeroDePaginas ? $pagina + 2 : $numeroDePaginas;
$previous_pages = ($pagina - 2) >= 1 ? $pagina - 2 : 1;
?>
<ul class="pagination">
<?php if ($pagina == 1) : ?>
<li class="page-item disabled">
<a class="page-link" href="">
<span class="ti-arrow-left"></span>
</a>
</li>
<?php else : ?>
<li class="page-item">
<a class="page-link" href="?pagina=<?php echo $pagina - 1 ?>">
<span class="ti-arrow-left"></span>
</a>
</li>
<?php endif; ?>
<!---------------------------------------------------------------------->
<?php
for ($i = $previous_pages; $i <= $next_pages; $i++) {
if ($pagina == $i) {
echo "<li class='page-item active'>
<a class='page-link' href='?pagina=$i'>$i</a>
</li>";
} else {
echo "<li class='page-item'>
<a class='page-link' href='?pagina=$i'>$i</a>
</li>";
}
}
?>
<!---------------------------------------------------------------------->
<?php if ($pagina == $numeroDePaginas) : ?>
<li class='page-item disabled'>
<a class='page-link' href=''>
<span class='ti-arrow-right'></span>
</a>
</li>
<?php else : ?>
<li class='page-item'>
<a class='page-link' href="?pagina=<?php echo $pagina + 1 ?>">
<span class='ti-arrow-right'></span>
</a>
</li>
<?php endif; ?>
</ul>
</nav>
Edit with more explaination:
I have added a variable called next_pages, this variable checks if the current page (ex: 9) + 2 is smaller than or equal to "numeroDePaginas" variable, I have done this to make sure that we never show any pages exceeding the number of pages we have, in case 9+2 is bigger than "numeroDePaginas" we set the variable equal to "numeroDePaginas"
For the previous_pages variable, we make sure that the current page(ex: 3) - 2 is never less than 1, (so it doesn't show 0 or negative pages for example), in case
3-2 is smaller than 1, we set the variable to 1.
Now, I have edited the for loop, to make sure that it starts from the previous_pages variable, and ends with next_pages variable.
Here are a test case:
$pagina = 7;
$numeroDePaginas = 9;
//$next_pages will be = 9 (i.e 7+2)
//$previous_pages will be = 5 (i.e 7-2)
for loop would print pages from 5 to 9.
I'm trying to get the subcategories for each category with xpath from this html code :
<ul class="simple_list">
<li class="category"><span class="category_span">ILUMINAT UTILITAR</span></li>
<li class="subcat"><span class="subcat_span">Indus70</span></li>
<li class="subcat"><span class="subcat_span">ApliLED48</span></li>
<li class="subcat"><span class="subcat_span">Minoris</span></li>
<li class="subcat"><span class="subcat_span">Canopus 230V</span></li>
<li class="category"><span class="category_span">ILUMINAT OFFICE</span></li>
<li class="subcat"><span class="subcat_span">Pavo 1 modul</span></li>
<li class="subcat"><span class="subcat_span">Laminar</span></li>
<li class="subcat"><span class="subcat_span">Alcor T</span></li>
<li class="subcat"><span class="subcat_span">Cassiopeia</span></li>
<li class="subcat"><span class="subcat_span">Sirius 8 module</span></li>
<li class="subcat"><span class="subcat_span">Cygnus</span></li>
<li class="subcat"><span class="subcat_span">Aries</span></li>
<li class="category"><span class="category_span">ILUMINAT INDUSTRIAL</span></li>
<li class="subcat"><span class="subcat_span">Castor 6 module</span></li>
<li class="subcat"><span class="subcat_span">Corona</span></li>
<li class="subcat"><span class="subcat_span">Octans</span></li>
<li class="subcat"><span class="subcat_span">Lacerta 3 module</span></li>
<li class="subcat"><span class="subcat_span">Fornax</span></li>
<li class="subcat"><span class="subcat_span">Cetus</span></li>
<li class="category"><span class="category_span">ILUMINAT DE EXTERIOR</span></li>
<li class="subcat"><span class="subcat_span">ELMA80</span></li>
<li class="subcat"><span class="subcat_span">Solaris 2 module</span></li>
<li class="category"><span class="category_span">BECURI CU LED</span></li>
<li class="subcat"><span class="subcat_span">LEC</span></li>
<li class="category"><span class="category_span">ACCESORII</span></li>
<li class="subcat"><span class="subcat_span">Banda</span></li>
<li class="subcat"><span class="subcat_span">Drosel</span></li>
For the subcat items I tried this :
$xpath->query('//li[#class="subcat" and not(preceding-sibling::li[#class="category"]['.$i.'])])
I want to extract all subcat items under a category item ... any xpath idea would be great .
At this point I'm doing this :
for ($i = 0; $i<=$nodecount; $i++) {
foreach( $xpath->query('//li[#class="category"]['.$i.']') as $category ) {
echo "<br/>" . "$i - Main Categ Name : " . $category->nodeValue . "<br/>";
//Now get the subcateg names and links
$subnodecount = 0;
foreach( $xpath->query('//li[#class="subcat"]') as $sub_categ_links_on_page ) {
$subnodecount++;
}
for ($y = 0; $y<=$subnodecount; $y++) {
foreach( $xpath->query('//li[#class="subcat" and preceding-sibling::li[#class!="category"]]['.$i.']['.$y.']') as $sub_category ) {
echo "--- $y - Sub Categ Name : " . $sub_category->nodeValue . "<br/>";
}//end foreach
}// end FOR $y
///////////////////////////////////////////////////////////
}//end foreach
}// end FOR $i
But I am stuck at the subcategs part..
I'm looking to get this output :
1 - Main Categ Name : ILUMINAT UTILITAR
--- 1 - Sub Categ Name : ApliLED48
--- 2 - Sub Categ Name : Minoris
--- 3 - Sub Categ Name : Canopus 230V
--- 4 - Sub Categ Name : Pavo 1 modul
[and so on]
Live code can be found and fiddled with, here :
http://codepad.viper-7.com/i8hweN
To get all those list below the category you could also use != in this case:
$xpath->query('//li[#class="subcat" and preceding-sibling::li[#class!="category"]]');
Sample Output
As an alternative, you could gather all those results first. Then you could just use CSS or a simple formatting for presentation:
$elements = $xpath->query('//li[#class="subcat" and preceding-sibling::li[#class!="category"]]');
$list = array();
if($elements->length > 0) {
foreach($elements as $cat) {
$category = $xpath->evaluate('string(./preceding-sibling::li[#class="category"][1])', $cat);
$list[$category][] = $cat->nodeValue;
}
}
// presentation
$i = $j = 1;
foreach($list as $category => $subcategory) {
echo $i . '. ' . $category . '<br/>'; $i++;
foreach($subcategory as $sub) {
echo str_repeat(' ', 5);
echo $j .'. ' . $sub . '<br/>'; $j++;
}
$j = 1;
}
Sample Output
I'm using bootstrap pagination, and a combination PHP/mysql method of displaying my results, (ex. results.php?results=1 for each page). This is working fine, but actually displaying the pagination div at the bottom of my page, i.e. which one is active, whether or not you can click the following or preceding items (if there actually are results on those pages)
So I've done a specific case for just about every combination I could think of that would affect anything, but it is certainly not efficient. Can anyone suggest a better way to do it? Some of the cases will be obvious, like if I'm on page one, I don't want to show page 0 and -1 in the pagination, others are less obvious, like if I'm on page 3, and there are no more pages, I want page 3 to be in the middle, as there are 5 numbers displayed, but you should not be able to click on 4 and 5.
I'm using an exact replica of the plugin showed here except I've added left and right increase / decrease by one in addition to the first / last page.
<div id="pagination" style="width: 340px; margin-left: auto; margin-right: auto;">
<? if ($results == 1) { ?>
<ul class="pagination">
<li class="disabled"> <i class="fa fa-lg fa-angle-double-left"></i> </li>
<li class="disabled"> <i class="fa fa-lg fa-angle-left"></i> </li>
<li class="active"><? echo $results; ?> <span class="sr-only">(current)</span></li>
<li <?if ($num_rows < $num_res) { echo "class=\"disabled\""; } ?>><? echo $results + 1; ?></li>
<li <?if ($num_rows < ($num_res * 2)) { echo "class=\"disabled\""; } ?>><? echo $results + 2; ?></li>
<li <?if ($num_rows < ($num_res * 3)) { echo "class=\"disabled\""; } ?>><? echo $results + 3; ?></li>
<li <?if ($num_rows < ($num_res * 4)) { echo "class=\"disabled\""; } ?>><? echo $results + 4; ?></li>
<li <?if ($num_rows < ($num_res)) { echo "class=\"disabled\""; } ?>> <i class="fa fa-lg fa-angle-right"></i> </li>
<li <?if ($num_rows < ($num_res)) { echo "class=\"disabled\""; } ?>> <i class="fa fa-lg fa-angle-double-right"></i> </li>
</ul>
<? } ?>
<? if ($results == 2) { ?>
<ul class="pagination">
<li> <i class="fa fa-lg fa-angle-double-left"></i> </li>
<li> <i class="fa fa-lg fa-angle-left"></i> </li>
<li><? echo $results - 1; ?></li>
<li class="active"><? echo $results; ?> <span class="sr-only">(current)</span></li>
<li <?if ($num_rows < ($num_res * 2)) { echo "class=\"disabled\""; } ?>><? echo $results + 1; ?></li>
<li <?if ($num_rows < ($num_res * 3)) { echo "class=\"disabled\""; } ?>><? echo $results + 2; ?></li>
<li <?if ($num_rows < ($num_res * 3)) { echo "class=\"disabled\""; } ?>><? echo $results + 3; ?></li>
<li <?if ($num_rows < ($num_res * 3)) { echo "class=\"disabled\""; } ?>> <i class="fa fa-lg fa-angle-right"></i> </li>
<li <?if ($num_rows < ($num_res * 3)) { echo "class=\"disabled\""; } ?>> <i class="fa fa-lg fa-angle-double-right"></i> </li>
</ul>
<? } ?>
<? if (($results == 3) && (($last_page == 3))) { ?>
<ul class="pagination">
<li> <i class="fa fa-lg fa-angle-double-left"></i> </li>
<li> <i class="fa fa-lg fa-angle-left"></i> </li>
<li><? echo $results - 2; ?> </li>
<li><? echo $results - 1; ?></li>
<li class="active"><? echo $results; ?><span class="sr-only">(current)</span></li>
<li <?if ($num_rows < ($num_res * 3)) { echo "class=\"disabled\""; } ?>><? echo $results + 1; ?></li>
<li <?if ($num_rows < ($num_res * 3)) { echo "class=\"disabled\""; } ?>><? echo $results + 2; ?></li>
<li <?if ($num_rows < ($num_res * 3)) { echo "class=\"disabled\""; } ?>> <i class="fa fa-lg fa-angle-right"></i> </li>
<li <?if ($num_rows < ($num_res * 3)) { echo "class=\"disabled\""; } ?>> <i class="fa fa-lg fa-angle-double-right"></i> </li>
</ul>
<? } ?>
<? if (($results > 2) && ($results < ($last_page - 1))) { ?>
<ul class="pagination">
<li> <i class="fa fa-lg fa-angle-double-left"></i> </li>
<li> <i class="fa fa-lg fa-angle-left"></i> </li>
<li><? echo $results - 2; ?> </li>
<li><? echo $results - 1; ?></li>
<li class="active"><? echo $results; ?><span class="sr-only">(current)</span></li>
<li><? echo $results + 1; ?></li>
<li><? echo $results + 2; ?></li>
<li <?if ($num_rows < ($num_res * 3)) { echo "class=\"disabled\""; } ?>> <i class="fa fa-lg fa-angle-right"></i> </li>
<li <?if ($num_rows < ($num_res * 3)) { echo "class=\"disabled\""; } ?>> <i class="fa fa-lg fa-angle-double-right"></i> </li>
</ul>
<? } ?>
<? if (($results > 2) && ($results == ($last_page - 1))) { ?>
<ul class="pagination">
<li> <i class="fa fa-lg fa-angle-double-left"></i> </li>
<li> <i class="fa fa-lg fa-angle-left"></i> </li>
<li><? echo $results - 3; ?></li>
<li><? echo $results - 2; ?></li>
<li><? echo $results - 1; ?></li>
<li class="active"><? echo $results ?><span class="sr-only">(current)</span></li>
<li><? echo $results + 1; ?></li>
<li <?if ($num_rows < ($num_res * 3)) { echo "class=\"disabled\""; } ?>> <i class="fa fa-lg fa-angle-right"></i> </li>
<li <?if ($num_rows < ($num_res * 3)) { echo "class=\"disabled\""; } ?>> <i class="fa fa-lg fa-angle-double-right"></i> </li>
</ul>
<? } ?>
<? if (($results > 2) && ($results == $last_page) && ($results != 3)){ ?>
<ul class="pagination">
<li> <i class="fa fa-lg fa-angle-double-left"></i> </li>
<li> <i class="fa fa-lg fa-angle-left"></i> </li>
<li><? echo $results - 4; ?> </li>
<li><? echo $results - 3; ?></li>
<li><? echo $results - 2; ?></li>
<li><? echo $results - 1; ?></li>
<li class="active"><? echo $results ?><span class="sr-only">(current)</span></li>
<li class="disabled"> <i class="fa fa-lg fa-angle-right"></i> </li>
<li class="disabled"> <i class="fa fa-lg fa-angle-double-right"></i> </li>
</ul>
<? } ?>
</div>
If no one feels like helping with the first portion, I understand, it works as it is as far as I can tell.
But I also am wondering if there's a way that on my class="disabled", I can remove the hrefs? Or do I just have to put an if statement on every one? Thanks.
Edit: Based on #Scopey's recommendations. I changed a few things as I saw fit, as well as a couple variable names.
$numberOfPages = ceil($num_rows / $num_res);
$numberOfPages = (int)$numberOfPages;
$url = explode('&', $_SERVER['REQUEST_URI']);
function myFilter($string) {
return strpos($string, 'results=') === false;
}
$url = array_filter($url, 'myFilter');
$url = implode('&', $url);
?>
<ul class="pagination">
<li<?php if($results === $numberOfPages): ?> class="disabled"<?php endif; ?>><a href="<?php
if($results !== $numberOfPages){
echo $url.'&results=' . 1;
} else { echo '#'; }
?>"> <i class="fa fa-lg fa-angle-double-left"></i> </a></li>
<li<?php if($results === 1): ?> class="disabled"<?php endif; ?>><a href="<?php
if($results !== 1){
echo $url.'&results=' . ($results - 1);
} else { echo '#'; }
?>"> <i class="fa fa-lg fa-angle-left"></i> </a></li>
<?php
// Print the pagination...
// Minimum of 5 pages in the pagination, even if there aren't 5 pages...
$pageCount = ($numberOfPages < 5) ? 5 : $numberOfPages;
// Loop through from page 1 until the last page ($pageCount)
for($i = 1; $i <= $pageCount; $i++)
{
// Echo out just the beginning of the <li> tag as we don't yet
// know if this needs to be disabled...
echo '<li';
// Added to show if current page is active
if($i === $results) {
echo ' class="active"';
}
// Check if:
// - This current page is greater than the amount of pages of
// results we have
// - OR, this is the currently selected page
if($i > $numberOfPages || $results === $i)
{
// <li> tag needs the disabled class....
echo ' class="disabled"';
}
// Finish the <li> tag and start generating the link
echo '><a href="';
// Opposite logic from above... Only if this is a page we know about
// AND this is not the current page.
if($i < $numberOfPages && $results !== $i)
{
// The link will be your page, with the get param, and the
// current page number we're printing
echo $url.'&results=' . $i;
}
else
{
// Otherwise just a # (no link)
echo '#';
}
// Finish this page... Print the page number ($i)
echo '">' . $i . '</a></li>';
}
?>
<li<?php if($results === $numberOfPages): ?> class="disabled"<?php endif; ?>><a href="<?php
if($results !== $numberOfPages){
echo $url.'&results=' . ($results + 1);
} else { echo '#'; }
?>"> <i class="fa fa-lg fa-angle-right"></i> </a></li>
<li<?php if($results === $numberOfPages): ?> class="disabled"<?php endif; ?>><a href="<?php
if($results !== $numberOfPages){
echo $url.'&results=' . $numberOfPages;
} else { echo '#'; }
?>"> <i class="fa fa-lg fa-angle-double-right"></i> </a></li>
<?php
#Scobey, this works great, as intended, and same as my original post, except for a couple things. In my example, I was able to get the current page in the middle of the . For example, << 1 | 2 | 3 | 4 | 5 >>, that's why I had so many different statements with +1 and -1, +2, -2 etc.
Second, my intention was to display 5 page options at all times, not just a minimum of five. So if I was on page 6, I would only see << 4 | 5 | 6 | 7 | 8 >>, not 1-8. It may not be necessary to do it this way, it's a nice to have. But, I don't want to get to a point where I have over 50 different page numbers in the pagination.
Generally dynamic pagination works only when you have a few things
A count of all possible results (total)
An amount of results you want to show per page
The current page number
Information about the results that should appear on this page.
Imagine that you have a database table called Results that has a whole bunch of results you want on your paginated page
1. Count of all possible results
This is achievable with a simple SQL statement like:
SELECT count(*) FROM Results
2. Amount per page
This is a value you get to decide yourself.
3. The current page number
This is usually 1 by default, but when the pagination is changed, it gets passed through as a get parameter.
4. A slice of results from the table
Achievable with:
SELECT * FROM Results LIMIT 0, 20
This example retrieves the first 20 results in our results table...
5. The pagination
Now, using 20 results as a default "count per page"
$countPerPage = 20;
// Get the total number of results
$result = pg_query('SELECT count(*) FROM Results'); // I'm using PostgreSQL for this example
$totalResultCount = (int)pg_fetch_result($result, 0, 0);
// The ceil function will round floats up.
$numberOfPages = ceil($totalResultCount / $countPerPage);
// Check if we have a page number in the _GET parameters
if(!empty($_GET) && isset($_GET['page']))
{
$page = (int)$_GET['page'];
}
else
{
$page = 1;
}
// Check that the page is within our bounds
if($page < 0)
{
$page = 1;
}
elseif($page > $numberOfPages)
{
$page = $numberOfPages;
}
// Build the query for the results...
$query = 'SELECT * FROM Results LIMIT ' . ($page - 1) * $countPerPage . ', ' . $countPerPage;
$results = pg_fetch_all(pg_query($query));
// Deal with printing your results etc...
?>
<ul class="pagination">
<li<?php if($page === 1): ?> class="disabled"<?php endif; ?>><a href="<?php
if($page !== 1){
echo 'page.php?page=' . $page - 1;
} else { echo '#'; }
?>">«</a></li>
<?php
// Print the pagination...
// Minimum of 5 pages in the pagination, even if there aren't 5 pages...
$pageCount = ($numberOfPages < 5) ? 5 : $numberOfPages;
// Loop through from page 1 until the last page ($pageCount)
for($i = 1; $i < $pageCount; $i++)
{
// Echo out just the beginning of the <li> tag as we don't yet
// know if this needs to be disabled...
echo '<li';
// Check if:
// - This current page is greater than the amount of pages of
// results we have
// - OR, this is the currently selected page
if($i > $numberOfPages || $page === $i)
{
// <li> tag needs the disabled class....
echo ' class="disabled"';
}
// Finish the <li> tag and start generating the link
echo '><a href="';
// Opposite logic from above... Only if this is a page we know about
// AND this is not the current page.
if($i < $numberOfPages && $page !== $i)
{
// The link will be your page, with the get param, and the
// current page number we're printing
echo 'page.php?page=' . $i;
}
else
{
// Otherwise just a # (no link)
echo '#';
}
// Finish this page... Print the page number ($i)
echo '">' . $i . '</a></li>';
}
?>
<ul class="pagination">
<li<?php if($page === $numberOfPages): ?> class="disabled"<?php endif; ?>><a href="<?php
if($page !== $numberOfPages){
echo 'page.php?page=' . $page + 1;
} else { echo '#'; }
?>">»</a></li>
<?php
Disclaimer
I just sat here and wrote this, I have not tested it, but the general idea is there. Hopefully it makes sense. I'm running out of time and don't have time to test or improve it, but if you're interested I might fix it up later.
Hope this helps!
I'm getting all the data from GT api (xml) from link: http://api.gametracker.rs/demo/xml/server_info/217.26.212.10:20021/
Especially for players list, as you can see, all the PID's are 0, i'm displaying them in table with following code:
<?php
foreach( $players as $player ) {
echo "
<ul>
<li id=\"number\">$player->pid.</li>
<li id=\"nickname\">$player->name</li>
<li id=\"score\">$player->score</li>
<li id=\"time\">$player->time</li>
</ul>";
}
?>
What I can do to make pid count itself for as many as found players in xml file?
$i = 0; //Counter variable
foreach( $players as $player ) {
$i++; //Increment the counter for each iteration
echo "
<ul>
<li id=\"number\">$player->pid.</li>
<li id=\"nickname\">$player->name</li>
<li id=\"score\">$player->score</li>
<li id=\"time\">$player->time</li>
</ul>";
}
echo 'There were ' . $i . ' players in total';
All you need to do is have a variable for that:
$i = 1;
foreach ($players as $player)
{
echo "<ul>
<li id=\"number-$i\">$i.</li>
<li id=\"nickname-$i\">$player->name</li>
<li id=\"score-$i\">$player->score</li>
<li id=\"time-$i\">$player->time</li>
</ul>";
$i++;
}
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>