I am trying to turn this link into a php foreach loop without success and new to Stack overflow. I have used an array to generate the code but unsure how to turn this code into a foreach loop
<ul class="nav navbar-nav navbar-nav-first">
<li>About</li>
</ul>
I am just wondering how to create the li code for the foreach loop
foreach ($navItems as $item) {
echo "<li>$item[title]</li>";
}
<?php
$array = array(
"Home" => array("link" => "#home", "title" => "Take me home!"),
"About" => array("link" => "#about", "title" => "Learn more about us")
);
?>
<ul class="nav navbar-nav navbar-nav-first">
<? php
foreach ($array as $navTitle => $data) {?>
<li>
<?php echo $navTitle?>
</li>
<?php } ?>
</ul>
<?php $items = array( 'About' => 'about' ); ?>
<ul class="nav navbar-nav navbar-nav-first">
<?php foreach ( $items as $i_name => $i_ref ): ?>
<li><?= $i_name ?></li>
<?php endforeach; ?>
</ul>
please try
echo "<li><a href='".$item['slug']."' class='smoothScroll'>".$item['title']."</a></li>";
Related
So here is my code. The problem I am having is that I want the number from HP in my PHP code into my HP HTML code and the same thing with Cylinders. I have figured out the other stuff but when it comes to that part I am stuck
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$cars = array(
array(
"car" => "Ferrari",
"model" => "Testarossa",
"gearbox" => "Manual 5 Shift",
"designer" => "Battista Pininfarina",
"engine" =>
array(
"HP" => 390,
"Cylinders" => 12
),
),
);
?>
<?php foreach($cars as $cars_key => $car_val): ?>
<ul>
<div style="margin-bottom: 10px;">
<li><b>Car:</b> <?php echo $car_val["car"]; ?></li>
<li><b>Model:</b> <?php echo $car_val["model"]; ?></li>
<li><b>Gearbox:</b> <?php echo $car_val["gearbox"]; ?></li>
<li><b>Designer:</b> <?php echo $car_val["designer"]; ?></li>
<li><b>Engine</b></li>
<ul>
<li><b>HP:</b></li>
<li><b>Cylinders:</b></li>
</ul>
</div>
</ul>
<?php endforeach; ?>
I have a few concerns:
You lose the brilliance/utility of an associative array when you hardcode values into your script that you could otherwise just call from the array.
I don't like the look of the mid-list <div>. I can't think of any good reason to break up your unorder list flow with it.
I don't like the floating sub-list either. It logically belongs to Engine and good markup would dictate that the sub-list exist inside of its parent.
Here is what I would suggest considering my above points...
*Some Notes:
I'm not sure how you want to layout multiple lists as your array grows in size.
The echoing is just my personal preference. You can bounce in and out of php if you like.
ucfirst() allows you to avoid hardcoding the keys.
My snippet will make your task clean, DRY, and concise.
Code: (Demo)
$cars = array(
array(
"car" => "Ferrari",
"model" => "Testarossa",
"gearbox" => "Manual 5 Shift",
"designer" => "Battista Pininfarina",
"engine" => array(
"HP" => 390,
"Cylinders" => 12
)
)
);
foreach($cars as $details){
echo "<ul style=\"margin-bottom:10px;\">";
foreach($details as $key=>$item){
echo "<li><b>",ucfirst($key),":</b>";
if(!is_array($item)){
echo " $item</li>";
}else{
echo "<ul>";
foreach($item as $subkey=>$subval){
echo "<li><b>$subkey:</b> $subval</li>";
}
echo "</ul>";
echo "</li>";
}
}
echo "</ul>";
}
Source Code Output:
<ul style="margin-bottom:10px;">
<li><b>Car:</b> Ferrari</li>
<li><b>Model:</b> Testarossa</li>
<li><b>Gearbox:</b> Manual 5 Shift</li>
<li><b>Designer:</b> Battista Pininfarina</li>
<li><b>Engine:</b>
<ul>
<li><b>HP:</b> 390</li>
<li><b>Cylinders:</b> 12</li>
</ul>
</li>
</ul>
Rendered Output: (run my snippet # phptester.net to see this)
From you example, it seems to me that the list is static and consists of two elements, then you need not use forEach at all.
<?php foreach($cars as $cars_key => $car_val): ?>
<ul>
<div style="margin-bottom: 10px;">
<li><b>Car:</b> <?php echo $car_val["car"]; ?></li>
<li><b>Model:</b> <?php echo $car_val["model"]; ?></li>
<li><b>Gearbox:</b> <?php echo $car_val["gearbox"]; ?></li>
<li><b>Designer:</b> <?php echo $car_val["designer"]; ?>
</li>
<li><b>Engine</b></li>
<ul>
<li><b>HP:</b><?php echo $car_val["engine"]["HP"]; ?></li>
<li><b>Cylinders:</b><?php echo $car_val["engine"]["Cylinders"]; ?></li>
</ul>
</div>
</ul>
<?php endforeach; ?>
If you do need to use a nested forEach, here is how you would go about doing that:
foreach($cars as $cars_key => $car_val):
if($cars_key == "engine")
foreach($car_val["engine"] as $engine_key => $engine_val):
echo $engine_key.$engine_val;
endforeach;
endforeach;
<ul class="navdrop">
<li>category
<ul>
<li>subcategory
<ul>
<li>subcategory</li>
</ul>
</li>
</ul>
</li>
</ul>
how we creat a loop to show catgeory and sub category in structure using php data
if tried while loop to print the main catgeory
<li><a class="shopdrop"><?php echo stripslashes($catParent_result['Category_Name']); ?></a></li>
Using a nested array, you can simply write a recursive function that generates an html list:
<?php
// the example input data
$example = array(
"menu1" => "a",
"menu2" => "b",
"menu3" => array(
"submenu3.1" => "c",
"submenu3.2" => "d"
),
"menu4" => array(
"submenu4.1" => array ( "subsubmenu4.1.1" => "e"),
),
);
//
// print an ul menu out of a given (nested) array
//
function ulmenu($arr, $level=0)
{
if (empty($arr) || !is_array($arr) || sizeof($arr)<1) return;
// using a prefix is optional, for the purpose of learning,
// it makes the output more readable
$spaceprefix = str_repeat(" ", $level*4);
echo "\n${spaceprefix}<ul>\n";
foreach ($arr as $key => $value)
{
echo "${spaceprefix} <li>${key}";
if (is_array($value))
{
// recursive call of limenu
ulmenu($value, $level+1);
}
else
{
echo " : ${value}";
}
echo "</li>\n";
}
echo "${spaceprefix}</ul>";
}
ulmenu($example);
?>
This will generate
<ul>
<li>menu1 : a</li>
<li>menu2 : b</li>
<li>menu3
<ul>
<li>submenu3.1 : c</li>
<li>submenu3.2 : d</li>
</ul></li>
<li>menu4
<ul>
<li>submenu4.1
<ul>
<li>subsubmenu4.1.1 : e</li>
</ul></li>
</ul></li>
</ul>
i want to create in CakePHP a Elemnt that renders my Menu (Bootstrap).
My Database
My Element:
<?php
if (! isset ( $menus ) || empty ( $menus )) :
$menus = $this->requestAction('/menus/index');
endif;
foreach ( $menus as $menu ){
if($menu['Menu']['parent'] == 0){
?>
<li>
<?php
$inside = "<i class='".$menu['Menu']['icon']."'></i><span>".$menu['Menu']['name']."</span></a>";
echo $this->Html->link( $inside, array('controller' => $menu['Menu']['controller'],'action' => $menu['Menu']['action']), array( 'escape'=>false)); ?>
</li>
<?php } }
?>
How I render it:
<?php echo $this->element('Menus/main'); ?>
Example how a Dropdown should be:
<li class="dropdown">
<a href="javascript:;" class="dropdown-toggle" data-toggle="dropdown">
<i class="icon-long-arrow-down"></i>
<span>Drops</span>
<b class="caret"></b>
</a>
<ul class="dropdown-menu">
<li>Icons</li>
<li>FAQ</li>
<li>Pricing Plans</li>
<li>Login</li>
<li>Signup</li>
<li>404</li>
</ul>
</li>
I tried to render it in the View, but the Problem is I cant call Functions of the Controller in example to ask the has the Entrie Childs.
Some Ideas how to Render a Menu right with CakePHP?
I have 10 users in an array:
$array = array(
"aaa",
"bbb",
"ccc",
"ddd",
"eee",
"fff",
"ggg",
"hhh",
"iii",
"jjj",
);
And I want to display lists based on 5 or fewer users e.g.:
<ul>
<li>
<ul>
<li>aaa</li>
<li>bbb</li>
<li>ccc</li>
<li>ddd</li>
<li>eee</li>
</ul>
</li>
<li>
<ul>
<li>fff</li>
<li>ggg</li>
<li>hhh</li>
<li>iii</li>
<li>jjj</li>
</ul>
</li>
</ul>
At the moment I have:
<ul>
<?php foreach($users as $user): ?>
<li><?php echo $user ?></li>
<?php endforeach; ?>
</ul>
However I am not creating the inner uls. What is the best way to approach this? Using a for loop and counting out 5? Or is there a neater method?
Use array_chunk() to split an array into multiple arrays with a specified number of items.
<ul>
<?php
$users = array_chunk($array, 5);
foreach ($users as $user) {
echo "<li><ul>";
foreach ($user as $idv) {
echo "<li>" . $idv . "</li>";
}
echo "</ul></li>";
}
?>
</ul>
Look an alternative solution :)
<li>
<ul>
<?php
echo '<li>'.join('</li><li>',array_slice($array,0,5)).'</li>';
?>
</ul>
</li>
<li>
<ul>
<?php
echo '<li>'.join('</li><li>',array_slice($array,5,10)).'</li>';
?>
</ul>
</li>
<ul>
<?php
$lenght = count($users);
for( $i=0; $i<$lenght; $i++ ){
if( $i%5 == 0 ){
echo '<li><ul>';
}
echo '<li>'.$users[0].'</li>';
if( $i%5 == 4 || ($i+1==$lenght) ){
echo '</ul></li>';
}
}?>
</ul>
Hello I just started working with CI ( codeigniter) and everything went well till now, except my navigation part. I've used the url class for now with the anchor method to create the urls but I also want the current url to have a class="current" for example so that I can style it.
Can someone show me how to do this?
my link is created as follows:
$this->load->helper('url');
$menu_item = array(
'/home' => 'Home',
'/schiphol' => 'Schiphol Service',
'/tarieven' => 'Tarieven en Acties',
'/kwaliteit' => 'Kwaliteit',
//'/news' => 'news'
'/contact' => 'Contact'
);
and in my view
<nav role="navigation" class="mainnav">
<ul>
<?php foreach ($menu_item as $menu => $key): ?>
<li> <?php echo anchor($menu, $key) ?> </li>
<?php endforeach ?>
</ul>
</nav>
but in the anchor method I can give a 3rd method with the class but how can I do this only for the current url?
You can give a 3rd attribute, not a method.
You have to use the uri class to compare the current url with the one in the loop:
http://ellislab.com/codeigniter/user-guide/libraries/uri.html
<?php foreach ($menu_item as $menu => $key): ?>
<li>
<?php echo anchor($menu, $key, $this->uri->segment(1) == $menu ? 'class="active"' : '') ?>
</li>
<?php endforeach ?>