How to create ordinal numbers for list item? - php

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>";
}

Related

Insert new item to wordpress array [PHP]

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
}

stuck with simple php looping logic

I have an array which can have any no. of elements in it. Now i want to loop this array and create design such that each li can have 15 elements inside it , next set of li will be created based of multiples of 15's elements.
Now my array has exact 15 elements and the code i am trying creating 2 li , which it should create only 1 li.
May be my logic is too bad or I am missing anything.
Here is my code:-
<?php $result = array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15); ?>
<div class="slide">
<?php $design = '<ul class="slides"><li><div class="MainSlider">';
foreach($result as $key=>$row)
{
$design .= '<div class="MainSliderPic">'.$key.'</div>';
if(($key+1)% 15 == 0){
$design .= '</div></li><li><div class="MainSlider">';
}
if(count($result) == $key+1){
$design .= '</div></li></ul>';
}
}
echo $design;
?>
</div>
You can use array_chunk for to achieve it:
$result = array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15);
$chunks = array_chunk($result, 15);
foreach ($chunks as $chunk) {
echo '<ul><li>';
echo implode('</li><li>', $chunk);
echo '</li></ul>';
}
Don't mix opening and closing of tags in your code. Do it separately where it belongs, e.g.
$design = '<ul class="slides">';
$n = 0;
foreach($result as $key=>$row) {
if ($n == 0)
$design .= '<li><div class="MainSlider">';
$design .= '<div class="MainSliderPic">' . $key . '</div>';
++$n;
if ($n == 15) {
$design .= '</div></li>';
$n = 0;
}
}
$design .= '</ul>';
echo $design;
Try following code:
<?php $result = array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15); ?>
<div class="slide">
<?php $design = '<ul class="slides"><li><div class="MainSlider">';
foreach($result as $key=>$row)
{
$design .= '<div class="MainSliderPic">'.$key.'</div>';
if((($key+1)% 15 == 0) && (count($result) != ($key+1))){
$design .= '</div></li><li><div class="MainSlider">';
}
if(count($result) == $key+1){
$design .= '</div></li></ul>';
}
}
echo $design;
?>
</div>

Nesting foreach loop in php

Hi can anyone help me through this,. I'm a beginner learning, please help me nest through foreach loop. Here is the code.
<?php
$resource_url = "/app/resources/";
$names = array('Affiliate program','Careers','Corporate info','Eco Initiative','Government Customers','Social Responsibility');
?>
<ul>
<?php foreach ($names as $arr) {
$links = array('affiliate_program','careers','corporate_info','eco','government','responsibility');
foreach($links as $url){
echo "<li><a href=\"";
echo $resource_url;
echo $url;
echo "\">";
echo $arr;
echo "</a></li>";
}
}?>
</ul>
Try this.
$base_url = "/app/resources/";
$names = array('Affiliate program','Careers','Corporate info','Eco Initiative','Government Customers','Social Responsibility');
$links = array('affiliate_program','careers','corporate_info','eco','government','responsibility');
foreach(array_combine($links, $names) as $key => $url){
echo "<li><a href=\"";
echo $base_url;
echo $key;
echo "\">";
echo $url;
echo "</a></li>";
}
You've inserted the $links inside the foreach loop. Basically every time you loop one array item, ie. Affiliate Program, you loop the entire array of $links. Put $links outside the foreach loop or better yet.
<?php
$resource_url = "/app/resources/";
$names = array(
'affiliate_program' => 'Affiliate program',
'careers' => 'Careers',
'corporate_info' => 'Corporate info',
'eco' => 'Eco Initiative',
'government' => 'Government Customers',
'responsibility' => 'Social Responsibility');
?>
<ul>
<?php foreach($names as $href => $arr) {
echo "<li><a href=\"";
echo $href;
echo "\">";
echo $arr;
echo "</a></li>";
}?>
</ul>
You can do this way -
$resource_url = "/app/resources/";
$names = array('Affiliate program','Careers','Corporate info','Eco Initiative','Government Customers','Social Responsibility');
$links = array('affiliate_program','careers','corporate_info','eco','government','responsibility');
foreach(array_combine($links, $names) as $key => $url){
echo "<li><a href=\"";
echo $resource_url;
echo $key;
echo "\">";
echo $url;
echo "</a></li>";
}
Or generate a single array (key => value) and loop through it.
If you want something like this:
click me to see the image
You can simply:
<?php
$resource_url = "/app/resources/";
$names = array('Affiliate program','Careers','Corporate info','Eco Initiative','Government Customers','Social Responsibility');
$links = array('affiliate_program','careers','corporate_info','eco','government','responsibility');
echo("<ul>");
for($i=0; $i < count($names); $i++){
echo "<li><a href='";
echo $resource_url;
echo $links[$i];
echo "'>";
echo $names[$i];
echo "</a></li>";
}
echo("</ul>");
?>

HTML every nth iteration with a twist--the nth changes every xth time using array values

Everyone knows how to output a bit of html every nth iteration in a foreach loop.
$i=0;
foreach($info as $key){
if($i%3 == 0) {
echo $i > 0 ? "</div>" : ""; // close div if it's not the first
echo "<div>";
}
//do stuff
$i++;
}
I'm trying to do this same thing, but instead of a known value for $i, I'm pulling values in from an array like
Array(0=>2, 1=>1, 2=>5)
so that instead of
<div>
item
item
item
</div>
<div>
item
item
item
</div>
<div>
item
item
item
</div>
I can get something like this:
<div>
item
item
</div>
<div>
item
</div>
<div>
item
item
item
item
item
</div>
But I just can't get it to work. I think I'm close, but something's just escaping me. Any ideas?
Here's the code I'm running right now:
//$footnote = array of values
$i=0;
$m=0;
$bridge .= '<div class="grid block menu">';
foreach($value['sections'] as $section) {
if ($i++%$footnote[$m] === 0) {
$bridge .= '</div><div class="grid block menu">';
$m++;
}
$secname = $section['name'];
$dishcount = count($section['items']);
$bridge .= '<h3>'. $secname .' '.$footnote[0].'</h3>';
$i++;
} //end section foreach
$bridge .= '</div>';
I think the issue you're having is in the if($i++%...) section of your code.
Instead of incrementing $i and checking the result of the modular expression, just check if $i == $footnote[$m] and then reset $i back to 0 when it is a success.
I modified your script a little locally, try this out:
$i = $m = 0;
$bridge .= '<div class="grid block menu">';
foreach($value['sections'] as $section)
{
if ($i == $footnote[$m])
{
$bridge .= '</div><div class="grid block menu">';
$m++;
$i = 0;
}
$secname = $section['name'];
$dishcount = count($section['items']);
$bridge .= '<h3>'. $secname .' '.$footnote[$m].'</h3>';
$i++;
}
$bridge .= '</div>';
This way, you are actually iterating through each footnote instead of just checking to see if it is divisible by the number specified.
Untested code, let me know if any changes are necessary so I can update the answer appropriately.
// Calculate section breaks
$sections = [ 2, 1, 5];
$sectionBreaks = [];
$sum = 0;
foreach ($sections as $section) {
$sum += $section;
$sectionBreaks[] = $sum;
}
// Add the items to each section
$results = [];
$result = '';
$i = 0;
foreach ($items as $item) {
if (array_search($i, $sectionBreaks) !== false) {
$results[] = $result;
$result = '';
}
$result .= '<h3>' . $item . '</h3>';
}
// Collapse it all together
$finalResult = '<div>' . implode('</div><div>', $results) . '</div>';
This is the way to loop thru data in order to achieve the example you exposed at first. foreach and for. This works, but unless you give us some data to work with I wont be able to adjust it to it.
$bridge='';
foreach($value['sections'] as $section) {
$bridge .= '<div class="grid block menu" number="'.$section.'"><h3>MY TITLE!! '. $section['name'] .'</h3>';
for ($x = 0; $x <= $section; $x++) {
$bridge .= "Here goes the content; Item $x<br>";
}
$bridge .= '</div>';
}
echo $bridge;
I hope it helps :)

How to build multi level navigation with PHP

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>';
}

Categories