Get current url in ci - php

I am making a breadcrumb for my codeigniter using bootstrap but would like to know how to get the current url and so it show the page on my breadcrumbs current_url() not sure how to use this
Displayed On Controller Index
$data['breadcrumbs'] = array();
$data['breadcrumbs'][] = array(
'text' => $this->lang->line('language_key'),
'href' => $this->url->link
);
Displayed On View
<div class="breadcrumb">
<?php foreach ($breadcrumbs as $breadcrumb) { ?>
<li><?php echo $breadcrumb['text']; ?></li>
<?php } ?>
</div>

Working now
Controller file
$data['breadcrumbs'] = array();
$data['breadcrumbs'][] = array(
'text' => $this->lang->line('heading_title'),
'href' => $this->uri->uri_string()
);
View File
<div class="breadcrumb">
<?php foreach ($breadcrumbs as $breadcrumb) { ?>
<li><?php echo $breadcrumb['text']; ?></li>
<?php } ?>
</div>

yep for example what i would do is create a helper "breadcrumb_helper.php" and place it in your helpers folder (application/helpers/breadcrumb_helper.php). Load it with the auto load config file so that it is loaded via the entire site and can be called inside a view file.
<?php
/* This is breadcrumb_helper.php */
function breadcrumbs() {
//get codeigniter instance as object
$ci =& get_instance();
$linkBuild = '';
$breadcrumbs = '';
//http://ellislab.com/codeigniter/user-guide/libraries/uri.html
//the uri class is loaded by the system automatically
$count = 1;
$segs = $ci->uri->segment_array();
//no need to run this on the main page and only show a link to the main page.
if(count($segs) >= 1) :
$breadcrumbs .= '<ol class="breadcrumb">';
$breadcrumbs .= '<li class="home"><i class="fa fa-home"></i></li>';
foreach ($segs as $segment)
{
$linkBuild .= $ci->uri->segment($count) . '/';
if($count <= count($segs)) :
$breadcrumbs .= '<li>' . $segment . '</li>';
else :
$breadcrumbs .= '<li>' . $segment . '</li>';
$count++;
endif;
}
$breadcrumbs .= '</ol>';
endif;
return $breadcrumbs;
}
Then in your view file you would just need to run the function like this:
<?= breadcrumbs(); ?>
And this will give you the current breadcrumb to the page you are on.

Related

How to format long echo expressions in PHP?

I'm writing my function in PHP for listing post categories (just look at code style, not logic):
function list_categories() {
// $html = '';
echo '<div class="post-categories post-items__categories">';
echo '<span class="post-categories__title">Categories:</span>';
echo '<ul class="post-categories__list">';
echo '<li class="post-categories__item"><a class="post-categories__link">All categories</a></li>';
$categories = get_categories([
'taxonomy' => 'category',
'type' => 'post',
'orderby' => 'name',
'order' => 'DESC',
'hierarchical' => false,
]);
foreach ($categories as $category) {
$cat_class = ($category->term_id == get_queried_object_id()) ? 'active' : '';
echo '<li class="post-categories__item">
<a class="post-categories__link" href="'.get_category_link($category->term_id).'">'.
$category->name.
'</a>
</li>';
}
echo '</ul></div>';
}
And I want my tag echoing expressions to be more readable, not just in one line like here:
echo '<li class="post-categories__item"><a class="post-categories__link">All categories</a></li>';
I'm using new lines in foreach loop, but it's still ugly.
I found out about heredoc syntax, but I can't call functions inside it.
How do you write this kind of things? I really don't want to write all in one line
If you just need to echo HTML why not break out of PHP, then back into it so
function list_categories() {
// $html = '';
echo '<div class="post-categories post-items__categories">';
echo '<span class="post-categories__title">Categories:</span>';
echo '<ul class="post-categories__list">';
echo '<li class="post-categories__item"><a class="post-categories__link">All categories</a></li>';
$categories = get_categories([....
becomes...
function list_categories() {
// $html = '';
//break out of php
?>
<div class="post-categories post-items__categories">
<span class="post-categories__title">Categories:</span>
<ul class="post-categories__list">
<li class="post-categories__item"><a class="post-categories__link">All categories</a></li>
<!--carry on with php-->
<?php
$categories = get_categories([....
And if you need to call a variable in the middle of html code you can like so..
<?php $demo = "hello"; ?>
<span><?php echo $demo; ?> World</span>
HTML in a variable...
<?php $html = ' ?>
<!--HTML HERE-->
<?php '; ?>

Replace PHP code in Wordpress functions.php

I'm trying to add a last item to the wordpress primary menu. So here is my question:
I have a code in functions.php that works well:
function add_last_nav_item($items) {
return $items .= '<li><a href="#" >Contact</a></li>';
}
add_filter('wp_nav_menu_items','add_last_nav_item');
I need to replace which is inside the <li> tags with this:
<li><?php if(function_exists(wp_forecast)) { wp_forecast( wp_forecast("A") ); } ?></li>
I tried but it doesn't work.
Try this:
function add_last_nav_item($items) {
ob_start();
echo '<li>';
if (function_exists('wp_forecast')) {
wp_forecast( wp_forecast("A") );
}
echo '</li>';
$end = ob_get_clean();
$items = $items . $end;
return $items;
}
add_filter('wp_nav_menu_items','add_last_nav_item');

get third level category in opencart

I want to foreach third level in OpenCart category module,
here is code which only generates 2 level category, please help and modify so that it will genarate third level:
<ul id="menu">
<?php foreach ($categories as $category) { ?>
<li><?php echo $category['name']; ?>
<?php if ($category['children']) { ?>
<?php for ($i = 0; $i < count($category['children']);) { ?>
<ul>
<?php $j = $i + ceil(count($category['children']) / $category['column']); ?>
<?php for (; $i < $j; $i++) { ?>
<?php if (isset($category['children'][$i])) { ?>
<li><?php echo $category['children'][$i]['name']; ?></li>
<?php } ?>
<?php } ?>
</ul>
<?php } ?>
<?php } ?>
</li>
<?php } ?>
</ul>
For this first you need to edit the Header Controller:
Go to Catalog->controller->common->header.php
Edit the section that create the $categories variable. update by following script:
$categories = $this->model_catalog_category->getCategories(0);
foreach ($categories as $category) {
if ($category['top']) {
$children_data = array();
$children = $this->model_catalog_category->getCategories($category['category_id']);
foreach ($children as $child) {
$sec_children_data = array();
$sec_children = $this->model_catalog_category->getCategories($child['category_id']);
foreach ($sec_children as $sec_child) {
$sec_children_data[] = array(
'name' => $sec_child['name'] . ($this->config->get('config_product_count') ? ' (' . $product_total . ')' : ''),
'href' => $this->url->link('product/category', 'path=' . $child['category_id'] . '_' . $sec_child['category_id'])
);
}
$data = array(
'filter_category_id' => $child['category_id'],
'filter_sub_category' => true
);
$product_total = $this->model_catalog_product->getTotalProducts($data);
$children_data[] = array(
'name' => $child['name'] . ($this->config->get('config_product_count') ? ' (' . $product_total . ')' : ''),
'href' => $this->url->link('product/category', 'path=' . $category['category_id'] . '_' . $child['category_id']), 'children' => $sec_children_data
);
}
// Level 1
$this->data['categories'][] = array(
'name' => $category['name'],
'children' => $children_data,
'column' => $category['column'] ? $category['column'] : 1,
'href' => $this->url->link('product/category', 'path=' . $category['category_id'])
);
}
}
Then Update the view file for displaying the third level categories.
You could try this, it's not elegant but should work:
<ul id="menu">
<?php foreach ($categories as $category) :
echo '<li>'.$category['name'].'';
if (!empty($category['children'])) :
echo '<ul>';
foreach ($category['children'] as $category_level2) :
echo '<li>'.$category_level2['name'].'';
if (!empty($category_level2['children'])) :
echo '<ul>';
foreach ($category_level2['children'] as $category_level3) :
echo '<li>'.$category_level3['name'].'</li>';
endforeach;
echo '</ul>';
endif;
echo '</li>';
endforeach;
echo '</ul>';
endif;
echo '</li>';
endforeach;
echo '</ul>';
?>
Supposing you have administration side already done then you should check whether there are some categories under 2nd level. If yes, then make for construction (or foreach) and display them as 3rd level.
You will generate triply <ul></ul> construction. It should be stylized under CSS.
You can even make infinity subcategories using recursive function.
Let me know if you don't get it.
First download vqmod from here then extract it.Now vqmod folder keep in your site root directory. Then go to browser write your site url then "/vqmod/install" and press Enter then you get a message that you can install vqmod in your site successfully. Now you download a extension form here and extract it. and keep extract file in your site indicate foulder exm: menu3rdlevel-opencart-2_2\vqmod\xml/Menu3rdLevel.xml file in your site eg : vqmod\xml/Menu3rdLevel.xml them other file with "menu3rdlevel" folder.From your extension folder "javascript" to your site folder "javescript", extension folder "image" to site folder "image" extension folder "stylsheet" to site folder "stylsheet". then refresh your site in browser its ok now.
NB: Transfer xml file only file and other file with folder.
For editing the third level menu please make the following changes in the header.php controller file.
foreach ($sec_children as $sec_child) {
$sec_children_data[] = array(
'name' => $sec_child['name'] . ($this->config->get('config_product_count') ? '' : ''),
'href' => $this->url->link('product/category', 'path=' . $child['category_id'] . '_' . $sec_child['category_id'])
);
}
And also make the following changes to the header.tpl file.
?php if (isset($category['children'][$i]['level3'])) {
$level3menus = $category['children'][$i]['level3'];
?>
<ul class="level3">
<?php
foreach( $level3menus as $level3menu) {
?>
<li><?php echo $level3menu['name'];?></li>
<?php } ?>
Please refer my tutorial for step by step explanation and demo .
http://www.pearlbells.co.uk/third-level-category-menu-opencart/

Current menu item - foreach

Consider this:
$menuItems = array (
"page1" => "1.php",
"page2" => "2.php",
"page3" => "3.php",
"page4" => "4.php",
"page5" => "5.php",
);
and now I create the menu like this:
<ul class="menu">
<?php
foreach($menuItems as $name => $url) {
echo "<li><a href='$url' class='$class'>$name</a></li>";
}
?>
</ul>
Works great. But now I need to add a class .current on the current page.
To get the current page I do this:
<?php
function curPageName() {
return substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/")+1);
}
$curpage = curPageName();
?>
And it also works great.
SO I guess the question is how do I assign $curpage to the ... current page? :)
Thank you.
Alex
<ul class="menu">
<?php
foreach($menuItems as $name => $url) {
if ($url === $curpage){
$class.=' .current';
}
echo "<li><a href='$url' class='$class'>$name</a></li>";
}
?>
</ul>
Easy, inside your loop, check if $url == $curpage, and append appropriately.
Example:
<?php
$menuItems = array(
"page1" => "1.php",
"page2" => "2.php",
"page3" => "3.php",
"page4" => "4.php",
"page5" => "5.php",
);
$current_item = "2.php"; //Assume we got this from the function
?>
<ul class="menu">
<?php
foreach ($menuItems as $name => $url) {
echo "<li><a href='$url'";
if ($url == $current_item) {
echo " class='current'";
}
echo ">$name</a></li>\n";
}
?>
</ul>
<ul class="menu">
<?php
foreach($menuItems as $name => $url) {
$class = 'default';
if (curPageName() == $name) {
$class.='.current';
}
echo "<li><a href='$url' class='$class'>$name</a></li>";
}
?>
</ul>
So I made it work with your help! Thanks guys.
Special thanks for Adel - although you had a mistake there I fixed it like this:
foreach($menuItems as $name => $url) {
$class = 'default';
if (curPageName() == $url) {
$class.=' \. current';
}
echo "<li><a href='$url' class='$class'>$name</a></li>";
}
Awesome! Thanks again!
<?php
$menuItems = array(
'page1' => '1.php',
'page2' => '2.php',
'page3' => '3.php',
'page4' => '4.php',
'page5' => '5.php',
);
$current_page = curPageName();
?>
<ul class="menu">
<?php
foreach ($menuItems as $name => $url) {
?>
<li><a href="<?php=$url?>"
<?php
if ($url == $current_page) {
?>
class="current"
<?php
}
?>
><?php=$name?></a></li>
<?php
}
?>
</ul>
Why use the curPageName () at each iteration?
You can use a ternary syntax to solve your problem. It consists in this syntax:
(if condition ? true : false)
So we can use this way on your answer comparing the curPageName with the url inside the foreach loop.
$url == curPageName() ? 'current' : 'notCurrent'
Now you just need to use this in your favor.
I like the syntax of printf. So the code is more clean. Try this:
<?php
/**
* curPageName() is defined in question
* my_links() is a shortcut to array defined in question
*/
$template = "<li%s>%s</li>";
$links = my_links();
$current = curPageName();
print '<ul class="menu">';
foreach ($likns as $name => $url) {
$class = $current == $url ? ' class="current"' : '';
printf($template, $class, $url, $name);
}
print '</ul>';
Each %s item in $template will be changed by sequential variable. So, if you need to change the template the code is clean and easyest to give a maintance.
Or, if you want another easiest template way, use the php template syntax. Note the use of : after the foreach statement and the use of endforeach. You can see that I am using the short code of echo <?=$var?> that is more compreensible than <?php echo $var ?>.
<?php
/**
* curPageName() is defined in question
* my_links() is a shortcut to array defined in question
*/
$links = my_links();
$current = curPageName();
?>
<ul class="menu">
<?php foreach ($links as $name => $url): ?>
<li<?=($current == $url ? ' class="current"' : '')?>>
<?=$name?>
</li>
<?php endforeach ?>
</ul>

Better dynamic navigation menu

I have this approach for now:
ctop = $cnew = $cmine = '';
if($actual == 'top') $ctop = 'class="active"';
if($actual == 'last') $new = 'class="active"';
if($actual == 'mine') $cmine = 'class="active"';
$html = '<aside class="panel_derecho">
<div class="tabs"><h4>$donde</h4>
<ul>';
$js = "refrescar_submenu('last_$donde')";
$js_t = "refrescar_submenu('top_$donde')";
$js_r = "refrescar_submenu('mine_$donde')";
$html .= '<li '.$ctop.'>top</li>';
$html .= '<li '.$cnew.'>ultimos</li>';
$html .= '<li '.$mine.'>like</li>';
$html .= ' </ul>
</aside>';
return $html;
wich works as expected:
It generates a list with the desired copy, the desired javascript function parameter, and the active class (for the wanted one)
But i feel it could be less repetitive; and i can already see that it will be expensive to add/edit/remove copys, params, elements, etc.. i just don't know where to beggin..
In case it helps:
$donde represents the type of data (articles, songs, videos, ..)
$actual represents one atribute (new articles, top articles,
articles i like)
// Menu: link => text
$menu = array(
"top" => "top",
"last" => "ultimos",
"mine" => "like"
);
$html = '<aside class="panel_derecho">
<div class="tabs"><h4>$donde</h4>
<ul>';
foreach ($menu as $link => $text)
{
$html .= '<li '.( $link==$actual ? 'class="active"' : '').'>'.$text.'</li>';
}
$html .= ' </ul>
</aside>';
return $html;
$attributes = array("top" => "top", "last" => "ultimos", "mine" => "like");
$html = "<aside class=\"panel_derecho\">
<div class=\"tabs\"><h4>{$donde}</h4>
<ul>";
foreach ($attributes as $key=>$value)
$html .= " <li ". ($actual == $key ? "class=\"active\"" : "") .">{$value}</li>";
$html .= " </ul>
</div>
</aside>";
return $html;

Categories