creating a active url class - php

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 ?>

Related

Puzzled about php navigation

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

PHP return <ul> list as a function result

I am an absolute beginner in PHP.
I have a file main.php where I have a call to a php function:
<div id="navigation">
<nav>
<?php echo navigation('exclude'=>array('/about.php')) ?>
</nav>
</div>
I have a file navigation.php which is required in main.php, it has a small constructor to build the right structure of the navigation, it looks like this:
<ul class="navi naviTop">
<?php foreach($page as $key=>$singlepage){
if(!isset($singlepage['submenu'])){ ?>
<li class="navIem <?php echo $singlepage['name'];?>">
<a href="<?php echo $key; ?>">
<?php echo $singlepage['name'];?>
</a>
</li>
<?php } else { ?>
<ul class="hasSubs">
<?php foreach($singlepage['submenu'] as $subkey=>$subpage){ ?>
<li class="navItem subitem">
<a href="<?php echo $subkey; ?>">
<?php echo $subpage['name'];?>
</a>
</li>
<?php } ?>
</ul>
<?php } ?>
<?php } ?>
</ul>
This works completely fine for me, but I want to have some options, that's why I establish a function:
function navigation($arguments=array()){
$defaults = array(
'class' => '',
'exclude'=> array()
);
$opts = array_merge($defaults, $arguments);
What is the right way for me to return this HTML structure that I generate by calling the navigation function?
To keep your code clean, you should seperate the template (html) from the business logic (php). Therefore your approach is great. Here 's a short example, how you could solve your issue.
First think about all the data you want to use in your function. As far as we can see, you need the pages and the options. Both arguments are arrays.
function navigation(array $pages = [], array $options = [])
{
$defaults = [
'class' => '',
'exclude' => [],
];
$options = array_merge($defaults, $options);
include __DIR__ . '/templates/navigation.phtml';
}
As you can see, the function does nothing more, as you have defined already. It takes another array parameter, where the pages are stored in. At the end of the function it includes the navigation template. There 's nothing more to do in PHP side.
The template navigation.phtml should look something like you already have.
<ul class="navigation<?php if (!empty($options['class'])):?><?= $options['class'] ?><?php endif; ?>">
<?php foreach ($pages as $url => $page): ?>
<?php if (!in_array($url, $options['excludes']): ?>
<li><?= $page['name'] ?></li>
<?pgp endif; ?>
<?php endforeach ?>
</ul>
I 've shortened the code example. To keep the logic in your template as small as possible you could filter the exluded pages before in your function to avoid the in_array if condition in your template.
In your main.php file, you can simple call the navigate function.
<nav>
<?php navigation($pages, $options) ?>
</nav>
The $pages and $options variables have to be declared before the call of the navigation function.

Foreach Nested Loops

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;

How to give hyperlink to items in CodeIgniter

I need give some hyperlink to my items category in codeigniter.
$this->data['itemdata'] = array(
array(
'title' => 'Printers / Accessories',
'items' => ['Printers', 'Printer Cartridges', 'Compatible Toners'],
'brands' => ['hp', 'cannon', 'brother', 'toshiba', 'sharp']
),
how can I create it?
Updated
view file
<div class="items">
<ul class="floated">
<?php foreach ($item['items'] as $key => $value): ?>
<li><?php echo $value; ?></li>
<?php endforeach; ?>
</ul>
</div>
You have to do it something like below:-
<li><?php echo $value;?></li>
Note:- I hope through URL you want to send user to some fruitful link. So change controller/function/uri to corresponding values.
Reference:-CodeIgniter - Correct way to link to another page in a view
Note:- if you want to remove link then do like below:-
<li><?php echo $value;?></li>

Detecting The Page & Then Doing Something

I need the "Pages" link to be able to detect if the user is on any of the pages that are included in the dropdown menu for "Pages". Detecting whether they are on only 1 page is easy, I need to add multiple instances to the "Pages" item. Hopefully that makes a little more sense.
Here is what I have:
<li class='dropdown' id="menu1">
<a class="dropdown-toggle" data-toggle="dropdown" href="#menu1">Pages</a>
<ul class="dropdown-menu">
<li>
Home
</li>
<li>
About
</li>
</ul>
</li>
A example of what the code would look like for JUST index.php would be:
<li <?php if (stripos($_SERVER['REQUEST_URI'],'index.php') !== false) {echo 'class="active"';} ?>>
Home
</li>
Like I said before, I need to add multiple instances in basically one PHP block to detect MORE THAN if the user is JUST ON [filename].php
Hopefully this makes sense.
First you need to extract just the page name, then you need to compare it to the array of posible pages:
<?php if (in_array(preg_replace('|.*/(.+\.php).*|i','\\1',$_SERVER['REQUEST_URI']), array('index.php','about.php'))) {echo 'class="active"';}
You'll have to put your list items in an array. And then loop over them to create the menu.
$pages = array(
'Home' => 'home.php',
'About' => 'about.php'
);
<?php foreach ($pages as $name => $url): ?>
<li <?php if (stripos($_SERVER['REQUEST_URI'], $url) !== false):
echo 'class="active"';
endif ?>>
<?php echo $name?>
</li>
<?php endforeach ?>
This will check each result in the array to see if it matches the uri. If it does it will add the active class to the li.

Categories