resume foreachloop after incrementing php bootstrapp drop down menu - php

Question in short: i want to output array: 0, 1 , 2 then break, echo something , output 3,4,5 and keep this in a loop.
Hello everyone i am working on a dynamic php website with bootstrap 4 and php to practice the language. Unfortunately i am now stuck since i dont know how to create something that kindoff looks like an algorith. Allright enough talking and lets get down to the code:
nav.php file
<ul class="navbar-nav">
<li class='nav-item dropdown'>
<a class='nav-link dropdown-toggle' data-toggle='dropdown' datatarget='dropdown_target' href='#'>
<span class='caret'></span>Dropdown
</a>
<div class="dropdown-divider"></div>
<div class="dropdown-menu" aria-labelledby="dropdown_target">
<!-- <a class="dropdown-item">Dropdown</a> -->
<?php
$i=0;
foreach ($dropItems as $item ) {
echo "<a class='dropdown-item' href=\"$item[slug]\">$item[title] </a>";
$i++;
if($i==1) break;
echo "<a class='dropdown-item'>Dropdown</a>";
}
?>
</div>
</li>
</li>
<?php
foreach ($navItems as $item ) {
echo "<li class='nav-item'> <a class='nav-link' href=\"$item[slug]\">$item[title]</a> </li>";
}
?>
</ul>
arrays.php
<?php
//Navigatie menu items
$navItems = array(
array(
"slug" => "index.php",
"title" => "home"
),
array(
"slug" => "contact.php",
"title" => "Contact"
),
array(
"slug" => "market.php",
"title" => "Marketplace"
),
);
$dropItems = array(
array(
"slug" => "#",
"title" => "Lps"
),
array(
"slug" => "#",
"title" => "Sps"
),
array(
"slug" => "market.php",
"title" => "Marketplace"
),
);
?>

You can use array_chunk to split the array in the chunks of three and foreach nested with echo "something";.
Can't see in your code where this echo of three is so I just made an example of how to do it.
$arr = range(1,12); //example array
$chunks = array_chunk($arr, 3);
Foreach($chunks as $chunk){
Foreach($chunk as $val){
Echo $val ." ";
}
Echo "\nsomething\n";
}
https://3v4l.org/0VlN0

Thanks to Andreas i got it working using his method
$chunks = array_chunk($dropItems, 2);
Foreach($chunks as $chunk){
Foreach($chunk as $item){
echo "<a class='dropdown-item' href=\"$item[slug]\">$item[title] </a>";
}
Echo "<div class='dropdown-divider'></div>";
}

Related

PHP: Help in Retrieving and Echoing Multidimensional Array ID

I'm looking to display a list of available weapons of a game, based on items listed in an array. I'm able to display information suck as 'Attribute Values' and 'Price' perfectly via a foreach loop, however being a PHP newbie, I'm having a lot of trouble working out how to echo each item's ID, such as 'Short Sword', 'Middle Sword', 'Long Sword', etc. I thought I was getting close using key($sword)... but no dice. Here's what I'm working with:
<?php $item_swords = Array();
$item_swords["Short Sword"] = Array (
"Attribute Value" => 5,
"Price" => 100,
);
$item_swords["Middle Sword"] = Array (
"Attribute Value" => 8,
"Price" => 250,
);
$item_swords["Long Sword"] = Array (
"Attribute Value" => 12,
"Price" => 750,
); ?>
<?php foreach ($item_swords as $sword) { ?>
<li>
<img src="<?php echo $sword["Item Sprite"]; ?>">
<span><?php echo $sword; ?></span>
<div>ATT +<?php echo $sword["Attribute Value"]; ?></div>
<div><?php echo $sword["Price"]; ?>G</div>
</li>
<?php } ?>
If anyone could lend a hand and help demonstrate how best to echo an item's ID or a per item basis throughout my loop, then that would be very awesome indeed. Thank you for your time.
try with this code
use key attr in foreach loop
and echo $key value instead of array like $sword
<?php $item_swords = Array();
$item_swords["Short Sword"] = Array (
"Attribute Value" => 5,
"Price" => 100,
);
$item_swords["Middle Sword"] = Array (
"Attribute Value" => 8,
"Price" => 250,
);
$item_swords["Long Sword"] = Array (
"Attribute Value" => 12,
"Price" => 750,
); ?>
<?php foreach ($item_swords as $key=>$sword) { ?>
<li>
<img src="
<?php //echo $sword["Item Sprite"]; ?>">
<span><?php echo $key; ?></span>
<div>
ATT +
<?php echo $sword["Attribute Value"]; ?></div>
<div>
<?php echo $sword["Price"]; ?>G
</div>
</li>
<?php } ?>

Need HTML design of Tree view using PHP Array

I've one Array in PHP with limited data and static HTML view related to it which gives me tree view.
However; I need help to develop dynamic HTML code to view the same Data in Tree view.
Static Array code as follow
<?php
echo '<pre>';
$array = array(59336 => array(
array('parent_id' => 40503, 'child_id' => array()),
array('parent_id' => 20098, 'child_id' => array(array('parent_id' => 43849, 'child_id' => array(array('parent_id' => 43850, 'child_id' => array()), array('parent_id' => 43851, 'child_id' => array()))), array('parent_id' => 81542, 'child_id' => array()))),
array('parent_id' => 20099, 'child_id' => array(array('parent_id' => 43850, 'child_id' => array(array('parent_id' => 43851, 'child_id' => array()))), array('parent_id' => 81543, 'child_id' => array()))))
);
print_r($array);
?>
Static HTML
<ul id="tree-data" style="display:none">
<li id="root">
59336
<ul>
<li>
40503
</li>
<li>
20098
<ul>
<li>
43849
<ul>
<li>
43850
</li>
<li>
43851
</li>
</ul>
</li>
<li>
81542
</li>
</ul>
</li>
<li>
20099
<ul>
<li>
43850
<ul>
<li>
43851
</li>
</ul>
</li>
<li>
81543
</li>
</ul>
</li>
</ul>
</li>
</ul>
Need Dynamic HTML code to print the data using Static Array instead of my static HTML code. Kindly help me.
Try with below function which help to generate array as per your requirement.
<?php
function nested2ul($data) {
$result = array();
if (sizeof($data) > 0) {
$result[] = '<ul>';
foreach ($data as $entry) {
$result[] = sprintf(
'<li>%s %s</li>', $entry['parent_id'], nested2ul($entry['child_id'])
);
}
$result[] = '</ul>';
}
return implode($result);
}
?>
The html code as below :
<?php
$html = '<ul id="tree-data" style="display:none"><li id="root">59336';
$html .= nested2ul($array['59336']);
$html .= '</li></ul>';
echo $html;
?>

Echo datas from an array with php

I have the following array in php:
$stats = array(
"Item 1" => 20,
"Item 2" => 30,
"Item 3" => 66,
"Item 4" => 1
);
I need to echo these values, so I try this:
<?
foreach ($stats as $stat => $data) {
echo '
<div class="col-sm-6">
<div class="widget">
<div class="widget-body p-t-lg">
<div class="clearfix m-b-md">
<h1 class="pull-left text-primary m-0 fw-500"><span class="counter" data-plugin="counterUp">'.$data.'</span></h1>
<div class="pull-right watermark"><i class="fa fa-2x fa-tv"></i></div>
</div>
<p class="m-b-0 text-muted">'.$stats[$stat].'</p>
</div>
</div>
</div>
';
}
?>
But I have only the numerical values echoed.
Do you have the solution ?
Thanks.
Since you are using the foreach construct, $stat holds the keys and $data holds the values. So when saying echo $stats[$stat] is equivalent to echoing the value that has the key $stat. If you want to echo the keys you should do this : echo $stat.
Here you are trying to print the index of the array,
$array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red');
$key = array_search('green', $array); // $key = 2;
echo $key;
Use this code to print the key of the array

php create navigation menu from multidimensional array dynamically

I did research on this, and wasn't able to find an exact answer. Most of the questions/answers on here pertaining to this seem to be unfinished. If anyone knows of a finished solution similar to my question, please point me in that direction!
Here is my array:
Array
(
['home'] => Array
(
[0] => sub-home1
[1] => sub-home2
)
['about'] => Array
(
[0] => sub-about
['about2'] => Array
(
[0] => sub-sub-about
)
)
['staff'] => Array
(
[0] => sub-staff1
[1] => sub-staff2
)
['contact'] => contact
)
And here is what I would like to turn it into:
<ul>
<li><a href="">home<a/>
<ul>
<li>sub-home1</li>
<li>sub-home2</li>
</ul>
</li>
<li><a href="">about<a/>
<ul>
<li>sub-about</li>
<li>about2
<ul>
<li><a href="">sub-sub-about<a/></li>
</ul>
</li>
</ul>
</li>
<li><a href="">staff<a/>
<ul>
<li>sub-staff1</li>
<li>sub-staff2</li>
</ul>
</li>
<li><a href="">contact<a/></li>
</ul>
The array will be dynamically generated, but will have a limit of 3 levels ex: about->about2->sub-sub-about. I tried going off of this question: PHP/MySQL Navigation Menu but they didn't really seem to come to a conclusion? I am familiar with foreach's whiles and for loops but I just can't seem to wrap my head around this one.
EDIT: Enzino, your code works!
Here is my solution:
<?php
function MakeMenu($items, $level = 0) {
$ret = "";
$indent = str_repeat(" ", $level * 2);
$ret .= sprintf("%s<ul>\n", $indent);
$indent = str_repeat(" ", ++$level * 2);
foreach ($items as $item => $subitems) {
if (!is_numeric($item)) {
$ret .= sprintf("%s<li><a href=''>%s</a>", $indent, $item);
}
if (is_array($subitems)) {
$ret .= "\n";
$ret .= MakeMenu($subitems, $level + 1);
$ret .= $indent;
} else if (strcmp($item, $subitems)){
$ret .= sprintf("%s<li><a href=''>%s</a>", $indent, $subitems);
}
$ret .= sprintf("</li>\n", $indent);
}
$indent = str_repeat(" ", --$level * 2);
$ret .= sprintf("%s</ul>\n", $indent);
return($ret);
}
$menu = Array(
'home' => Array("sub-home1", "sub-home2"),
'about' => Array("sub-about", "about2" => Array("sub-sub-about")),
'staff' => Array("sub-staff1", "sub-staff2"),
'contact' => "contact"
);
print_r($menu);
echo MakeMenu($menu);
?>
Calvin's solution worked for me. Here's the edited version. We can use more nested loops to get sub - sub menu items.
echo '<ul>';
foreach ($menu as $parent) {
echo '<li>' . $parent . '';
if (is_array($parent)) {
echo '<ul>';
foreach ($parent as $children) {
echo '<li>' . $children . '';
}
echo '</ul>';
}
echo '</li&gt';
}
echo '</ul>';
I think you can use recursion? Here is some pseudocode, not very familiar with php.
function toNavMenu(array A){
for each element in A{
echo "<li>" + element.name + ""
if (element is an array){
echo "<ul>"
toNavMenu(element)
echo "</ul>"
}
echo "</li>"
}
}
I would probably slightly adapt the array to be something like the following:
Array(
0 => Array(
'title' => 'Home',
'children' => Array()
),
1 => Array(
'title' => 'Parent',
'children' => Array(
0 => Array(
'title' => 'Sub 1',
'children' => Array(),
),
1 => Array(
'title' => 'Sub 2',
'children' => Array(
0 => Array(
'title' => 'Sub sub 2-1',
'children' => Array(),
),
),
),
)
)
)
With a structure like this you could use recursion to build your menu HTML:
function buildMenu($menuArray)
{
foreach ($menuArray as $node)
{
echo "<li><a href='#'/>" . $node['title'] . "</a>";
if ( ! empty($node['children'])) {
echo "<ul>";
buildMenu($node['children']);
echo "</ul>";
}
echo "</li>";
}
}

php data structure - xml or tree?

i want to write a generalized function that can output something like this.
<span class="side_title">By Collection</span>
<ul class="menu">
<li class="top">
<a class="top_menu" href="#url">home</a>
</li>
<li class="sub">
collectionA
<ul>
<li>collectionA-1</li>
<li>collectionA-2</li>
</ul>
</li>
<li class="sub">
collectionB
<ul>
<li>collectionB-1</li>
</ul>
</li>
<li class="top">CollectionE</li>
</ul>
<span class="side_title">By Functions</span>
<ul class="menu">
<li class="top">
<a class="top_menu" href="#url">home</a>
</li>
<li class="sub">
functionA
<ul>
<li>functionA-1</li>
<li>functionA-2</li>
</ul>
</li>
<li class="sub">
functionB
<ul>
<li>functionB-1</li>
<li>functionB-2</li>
</ul>
</li>
<li class="top">functionE</li>
</ul>
but the problem is i can't find a data structure to represent this structure, i tried the follwoing(using array - where the key is link's content, value is the address. and recursion):
$a = array (
"By collection" => '',
'Base' => 'thisBase',
"expand" => array (
'Home' => 'home',
'collectionA' => 'collectionA',
'expand' => array (
'collectiona-1' => 'collectiona-1',
'collectiona-2' => 'collectiona-2'
),
'collectionB' => 'collectionb',
'expand' => array (
'collectionb-1' => 'collectionb-1',
'collectionb-2' => 'collectionb-2'
),
'collectionB' => 'collectionb',
'expand' => array (
'collectionc-1' => 'collectionc-1',
'collectionc-2' => 'collectionc-2'
),
),
"by Function" => ''
);
function expand($a=NULL)
{
foreach ($a as $key => $value)
{
if ($value == '')
{
echo '<span class="side_title">'.$key.'</span>';
echo '<ul class="menu">';
} elseif (is_array($value))
{
echo '<ul class="sub_menu">';
expand($value);
} else {
echo '<li><a href="'.$value.'>'.$key.'</a></li>';
}
}
echo '</ul>';
}
print_r($a);
expand($a);
i thought about using xml/tree to represent it, but the data structure is pass between different function, so i thought it will be to much effort, so i want to know what's wrong with this? or is there better way?
I don't know, if this solves your problem, but you forgot a closing " for the href attribute.
echo '<li>'.$key.'</li>';

Categories