I want to insert new <li> element to my function output.
function gallery_nav() {
$i = 0;
foreach ($myArray as $key ) {
$i++;
echo '<li>'. $i .'</li>';
}
}
It printing this html
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
<li>6</li>
<li>7</li>
Can i insert custom <li> element into this array? Like this:
<li>1</li>
<li>2</li>
<li class="custom">3</li>
<li>4</li>
<li>5</li>
<li class="custom">6</li>
<li>7</li>
<li>8</li>
<li>9</li>
If your custom indexes are assigned in an array, you can do something like this:
<?php
$myArray = array(1,2,3,4,5,6,7); # Your original array
$customArray = array(3,6); # Your custom array
$VarCount = count($myArray); # Set the default loop count
function gallery_nav($myArray, $customArray=NULL, $VarCount) {
for($i=1; $i<=$VarCount; $i++) {
$Class = NULL;
if(in_array($i, $customArray)){
$VarCount++; # Increment extra <li>
$Class = "class=\"custom\"";
}
echo "<li {$Class}>{$i}</li>
";
}
}
gallery_nav($myArray,$customArray, $VarCount);
?>
Which results in:
<li >1</li>
<li >2</li>
<li class="custom">3</li>
<li >4</li>
<li >5</li>
<li class="custom">6</li>
<li >7</li>
<li >8</li>
<li >9</li>
See live demo.
You could simply modify your function to check for a specific iteration number and output something extra:
$i = 0;
foreach ($myArray as $item => $value) {
$i++;
if ($i % 3 === 0) {
echo '<li class="custom">' . $i . '</li>';
}
echo '<li>'. $i .'</li>';
}
This will add an extra li element every 3 iterations.
If you don't want it to add an extra li every third iteration, then modify it back to this:
$i = 0;
foreach ($myArray as $item => $value) {
$i++;
if ($i % 3 === 0) {
$class = 'custom';
} else {
$class = '';
}
echo '<li class="' . $class .'">' . $i . '</li>';
}
If you only need it on iterations 3 and 6 and no others, modify the condition to this:
if ($i === 3 || $i === 6) {
// code
}
Related
I have a list item with this code:
foreach ($medias as $i => $media) {
$html .= "<li class=''>...</li>";
}
I want output as below:
<li class="id-1">...</li>
<li class="id-2">...</li>
<li class="id-3">...</li>
<li class="id-4">...</li>
....
Thank for your help!
Try this:
for($i=0; $i<count($media); $i++) {
$html .= '<li class="id-'.($i+1).'">...</li>';
}
Try this:
foreach ($medias as $i => $media) {
$html .= "<li class='".($i + 1)."'>...</li>";
}
Try this:
$x=0;
foreach ($medias as $i => $media) {
$x++;
echo "<li class='id-".$x."'>...</li>";
}
I was just trying to display a particular content in a loop in list and after a condition is met it should display in a div.This is what i have tried
function the_meta() {
if ( $keys = get_post_custom_keys() ) {
echo "<ul class='post-meta'>\n";
$i = 0;
foreach ( (array) $keys as $key ) {
$keyt = trim($key);
if ( is_protected_meta( $keyt, 'post' ) )
continue;
$values = array_map('trim', get_post_custom_values($key));
$value = implode($values,', ');
echo apply_filters('the_meta_key', "<li id='slots_".$key."'><span class='post-meta-key'>$key:</span> $value</li>\n", $key, $value);
if (++$i > 8)
echo apply_filters('the_meta_key', "<div><span class='post-meta-key'>$key:</span> $value</div>\n", $key, $value);
}
echo "</ul>\n";
}
}
what I am getting is as follows
<ul class='post-meta'>
<li id='slots_Numeri_estratti_1'><span class='post-meta-key'>Numeri_estratti_1:</span> 22</li>
<li id='slots_numeri_estratti_2'><span class='post-meta-key'>numeri_estratti_2:</span> 23</li>
<li id='slots_numeri_estratti_3'><span class='post-meta-key'>numeri_estratti_3:</span> 24</li>
<li id='slots_numeri_estratti_4'><span class='post-meta-key'>numeri_estratti_4:</span> 25</li>
<li id='slots_numeri_estratti_5'><span class='post-meta-key'>numeri_estratti_5:</span> 26</li>
<li id='slots_numeri_estratti_6'><span class='post-meta-key'>numeri_estratti_6:</span> 27</li>
<li id='slots_numeri_estratti_7'><span class='post-meta-key'>numeri_estratti_7:</span> 28</li>
<li id='slots_numeri_estratti_8'><span class='post-meta-key'>numeri_estratti_8:</span> Concorso Nr. 36 - Montepremi complessivo € 10.474.698,21</li>
<li id='slots_numeri_estratti_9'><span class='post-meta-key'>numeri_estratti_9:</span> 21</li>
<div id='slots_numeri_estratti_9'><span class='post-meta-key'>numeri_estratti_9:</span> 21</div>
<li id='slots_numeri_estratti_10'><span class='post-meta-key'>numeri_estratti_10:</span> 33</li>
<div id='slots_numeri_estratti_10'><span class='post-meta-key'>numeri_estratti_10:</span> 33</div>
</ul>
The values are displaying twice, one in list and other in div.I have been searching on Google for hours and couldn't get any results to solve it.Someone please help me .Thanks!!
It should be like this
if (++$i > 8)
echo apply_filters('the_meta_key', "<div><span class='post-meta-key'>$key:</span> $value</div>\n", $key, $value);
else
echo apply_filters('the_meta_key', "<li id='slots_".$key."'><span class='post-meta-key'>$key:</span> $value</li>\n", $key, $value);
rather than
echo apply_filters('the_meta_key', "<li id='slots_".$key."'><span class='post-meta-key'>$key:</span> $value</li>\n", $key, $value);
if (++$i > 8)
echo apply_filters('the_meta_key', "<div><span class='post-meta-key'>$key:</span> $value</div>\n", $key, $value);
$menu = mysql_query(" query 1");
$k = 1;
for ($s = 0; $s < mysql_num_rows($menu); $s++)
{
$menus= mysql_fetch_assoc($menu);
$mainmenu[]=$menus['name'];
$submenus=mysql_query("query 2");
for ($m = 0; $m < mysql_num_rows($submenus); $m++)
{
$submenu = mysql_fetch_assoc($submenus);
$subitem[]=$submenu['name'];
$url=$submenu['url'];
}
}
foreach($mainmenu as $mains)
{
echo '<li class="hasul"><a><span><b>' .$mains.'</b></span></a></li>';
foreach($subitem as $sub)
{
echo '<ul>';
echo '<li><span>' .$sub. '</span></li>';
echo '</ul>';
}
}
The code above show two queries which load a menu and submenus. Query2 uses some inputs from query1.
The menus do load correctly but they contain same menu items. Ideally each menu should have its own menu items.
You have a foreach inside your foreach, that means, that for every main menu you are outputing all the submenus.
Create some parameter for submenu, like:
$mainmenu = array(
1 => 'about',
2 => 'news',
3 => 'search',
);
$submenu = array(
1 => array( 'about my name', 'about my location' ),
3 => array( 'search me' ),
);
And now check only the submenu you need:
if ( is_array( $mainmenu ) )
{
echo '<ul>';
foreach( $mainmenu as $key=>$menu )
{
echo '<li>'.$menu.'</li>';
if ( is_array( $submenu[$key]) )
{
echo '<ul>';
foreach( $submenu[$key] as $sub )
{
echo '<li>'.$sub.'</li>';
}
echo '</ul>';
}
}
echo '</ul>';
}
This will produce:
<ul>
<li>about</li>
<ul>
<li>about my name</li>
<li>about my location</li>
</ul>
<li>news</li>
<li>search</li>
<ul>
<li>search me</li>
</ul>
</ul>
I hope you get the idea.
use this.. this could be easier to understand..
echo '<ul>';
$qry = mysql_query("query 1");
while ($row = mysql_fetch_array($qry))
{
echo "<li>";
echo "<a href='" . $row['url'] . "'>'" . $row['name'] . "'</a>";
$qry2 = mysql_query("query 2");
if (mysql_num_rows($query2) > 0)
{
echo "<ul>";
while ($row2 = mysql_fetch_array($query2))
{
echo "<li>";
echo "<a href='" . $row2['url2'] . "'>'" . $row2['name2'] . "'</a>";
echo "</li>";
}
echo "</ul>";
}
echo "</li>";
}
echo "</ul>";
let me know if you want any further help...
Im working on a prototype, and would like to build a multi level navigation - however not by looping through an array. I have a $depth and a $children, which should determine the depth of the navigation and the number of children on each level. So $depth = 4, $children = 8 would yield 4096 menu items.
This is a snippet af the output I would like:
<ul>
<li class="level-1">
Subject 1
<ul>
<li class="level-2">
Subject 1.1
<ul>
<li class="level-3">
Subject 1.1.1
</li>
...
</ul>
</li>
...
</ul>
</li>
...
</ul>
So far I have tried this, but I cant get my head around it :(
function draw_list ($depth, $children) {
echo '<ul>';
for ($i = 0; $i < $children; $i++) {
echo '<li>' . ($i++);
$depth--;
if ($depth > 0) {
echo draw_list($depth, $children);
}
echo '</li>';
}
echo '</ul>';
}
Several things required as I see it...
The $depth--; needs to be outside of the for loop
You were incrementing $i twice, once in the for statement, and then again on the echo '<li>' . ($i++);
The $depth check was stopping one early, so make >= instead of just > (Edit, thinking about it, this is an incorrect statement)
That should give you...
function draw_list ($depth, $children) {
echo '<ul>';
$depth--;
for ($i = 0; $i < $children; $i++) {
echo '<li>' . $i;
if ($depth > 0) {
echo draw_list($depth, $children);
}
echo '</li>';
}
echo '</ul>';
}
Update
For the display of the level numbering, try passing a string value through as a parameter...
function draw_list ($depth, $children, $display=''){
echo '<ul>';
$depth--;
for ($i = 0; $i < $children; $i++) {
echo '<li>' . $display . ($i + 1);
if ($depth > 0) {
echo draw_list($depth, $children, $display . ($i + 1) . '.');
}
echo '</li>';
}
echo '</ul>';
}
I ended up doing this:
function build_nav ($depth, $children, $levels = array()) {
echo '<ul>';
$depth--;
for ($i = 0; $i < $children; $i++) {
$levels[$depth] = $i+1;
echo '<li>';
echo 'Subject: ' . implode('.', $levels);
if ($depth > 0) {
build_nav($depth, $children, $levels);
}
echo '</li>';
}
echo '</ul>';
}
I want php to print a class on each fourth list-item. Is that possible?
<ul>
<li>List-item 1</li>
<li>List-item 1</li>
<li>List-item</li>
<li class="new_class">List-item</li>
<li>List-item 1</li>
<li>List-item 1</li>
<li>List-item</li>
<li class="new_class">List-item</li>
</ul>
In your for loop, iterate an index and if ( $index % 4 == 0 ) { echo 'class="new_class"'; }
$items = array ('List-item 1', 'List-item 1', 'List-item', 'List-item', 'List-item 1', 'List-item 1', 'List-item', 'List-item');
printf("<ul>");
for ($index=0; $index < count($items); $index++)
{
if ($index%4 == 0)
{
$class = ' class="new_class"';
}
else
{
$class = '';
}
printf("<li%s>%s</li>", $class, $item[$index]);
}
printf("</ul>");