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/
Related
I have a sitemap that I'm trying to group by URL and stack them based on URL. Here is the sitemap https://www.sitecentre.com.au/sitemap
Basically, I need this to be dynamic. I can make this easily by doing a case, or if statements, like I've done on the Blog section, but I need it dynamic. It needs to look like this:
<li>Web Design
<ul>
<li>Nested in Web Design</li>
<li>Nested in Web Design</li>
</ul>
</li>
All based on the URL. the URL is /design then the nested would be design/nested
Then the next group would be like branding and nested on branding so it's all organised and laid out perfectly, then alphabetically ordered based on something else.
Here is our current PHP:
<?php
$pages = json_decode(file_get_contents('data/pages.json'));
foreach ($pages as $data){
foreach ($data as $key => $values){
if ($values->status === '1' && $values->visible === '1' && $values->slug !== 'home'){
if ($values->slug !== 'blog'){
echo '<li>' . $values->heading . ' - Last Updated: ' . date('d/m/Y', strtotime($values->date_modified)) . '</li>';
} else {
// Get the latest blogs
$blogs = json_decode(file_get_contents('data/blogs.json'));
echo '<li>' . $values->heading . ' - Last Updated: ' . date('d/m/Y', strtotime($values->date_modified)) . '';
echo '<ul>';
foreach ($blogs as $data){
foreach ($data as $key => $values){
if ($values->status === '1' && $values->visible === '1'){
echo '<li>' . $values->heading . ' - Last Updated: ' . date('d/m/Y', strtotime($values->date_modified)) . '</li>';
}
}
}
echo '</li></ul>';
}
}
}
}
?>
It clearly needs a bit of work in order to make this work, but I cannot for the life of me work out where to start.
I've got a list of "sites" in an SQL database that I'd like to display on a web page. I need to create a div ID for every site. For the time being I'm doing this (fetch the table into a $_SESSION array, get and display the name of the sites in a loop and do an echo):
<div id='leftnavigation1' class="leftnavigation">
<ul>
<?php
$maxKeys = max(array_keys($_SESSION['usersmeter'])); //check how many sites are in the usersmeter table
for ($i = 0; $i <= $maxKeys; $i++) { //loop every key in the array
$siteName = $_SESSION['usersmeter'][$i]['siteName'];
echo "<li id='siteID$i'><a href='#'><span>" . $siteName . "</span></a></li>"; //display the name of the sites
}
?>
</ul>
</div>
It works well but I think it is a bad practice to include php code into the View.
How else can I do? An AJAX in Javascript ?
Thanks,
Like that you will br try:
<?php
$my_array = array(
'GSU4300' => 'Lili Markina',
'GSU4301' => 'John Kokina',
'GSU4304' => 'Bill Clinong',
'GSU4305' => 'Obamark Chiko'
);
echo '<ul id="somethig">';
foreach ($my_array as $k => $v) {
echo '<li id="' . $k . '">' . $v . '</li>';
}
echo '</ul>';
?>
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.
I know this is a Wordpress based question, but I think the answer is definitely more stand-alone PHP.
On almost every site I do for a client I have to add some social media links, which come from an options page. I currently use this kind of snippet;
$twt = of_get_option('twitter');
$fcb = of_get_option('facebook');
$ins = of_get_option('instagram');
if ($twt) {
echo '<li class="twitter">Twitter</li>';
}
if ($fcb) {
echo '<li class="facebook">Facebook</li>';
}
if ($ins) {
echo '<li class="instagram">Instagram</li>';
}
This is fine if there are only a couple of links, but recently one of my main clients seems to be including every social media link under the sun to their designs so doing it this way can be a bit clunky.
Is there a way I can combine everything into a foreach or something?
Identify commonalities:
echo '<li class="twitter">Twitter</li>';
^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^
Identify varying parts:
echo '<li class="twitter">Twitter</li>';
^^^^^^^ ^^^^^^^
Identify dependencies and relationships:
$twt depends on of_get_option(...) whose parameter is the same as the twitter in 2.
The class and the name of the service are dependent on each other. Their relationship is apparently that the name is just the first-letter-uppercased version of the class, but I wouldn't rely on that necessarily.
Unify:
$services = array(
'twitter' => 'Twitter',
'facebook' => 'Facebook'
...
);
foreach ($services as $service => $name) {
if ($url = of_get_option($service)) {
printf('<li class="%s">%s</li>', $service, $url, $name);
// or, if you can't be sure that the variables are safe for HTML interpolation:
// printf('<li class="%s">%s</li>', htmlspecialchars($service), htmlspecialchars($url), htmlspecialchars($name));
}
}
Don't know if you mean exactly that, but check this code:
foreach(array('twitter', 'facebook', 'instagram') as $source)
{
$data = of_get_option($source);
if($data)
{
echo '<li class="'.$source.'">'.ucfirst($source).'</li>';
}
}
You could try something like this:
$social_media = array(
array( 'name' => 'Twitter', 'href' => of_get_option('twitter') ),
array( 'name' => 'Facebook', 'href' => of_get_option('facebook') ),
array( 'name' => 'Instagram', 'href' => of_get_option('instagram') )
);
foreach( $social_media as $s )
echo '<li class="' . strtolower( $s["name"] ) . '">' . $s["name"] . '</li>';
Then simply add the new social media site into the $social_media array.
Try the following:
$social = array('Twitter', 'Facebook', 'Instagram');
for($i=0;$i<length($social); ++$i) {
$twt = of_get_option($social[$i]);
if ($twt) {
echo '<li class="' . strtolower($social[$i]) . '">' . $social[$i]. '</li>';
}
}
You can add additional social networks by adding them to the array. It's a compact solution.
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>