Here is my code:
<div class="search-menu">
<div class="btn-group fc">
<a class="btn dropdown-toggle" data-toggle="dropdown" href="#">
<?php
$currencies = explode(',', hana_get_opt('_cc'));
foreach ($currencies as $currency) {
if ($currency == get_option('woocommerce_currency')){
echo $currency;
break;
}else{
echo "Select Currency";
break;
}
}
?>
<span class="caret"></span>
</a>
<ul class="dropdown-menu currency_switcher">
<?php
foreach ($currencies as $currency) {
if ($currency == get_option('woocommerce_currency'))
echo '<li>' . $currency . '</li>';
else
echo '<li>' . $currency . '</li>';
}
?>
</ul>
</div>
</div>
Which works great it returns a list of my currencies and updates the page accordingly, I have just on question if the user has yet to set a value I would like it to say either select currency or just apply the first option from my hana_get_opt('_cc') array as the default.
Here is the html generated code:
<div class="btn-group fc">
<a class="btn dropdown-toggle" data-toggle="dropdown" href="#">undefined<span class="caret"></span></a>
<ul class="dropdown-menu currency_switcher">
<li>SEK</li>
<li>EUR</li>
</ul>
</div>
I am no php coder and much appreciate any help provided
Chris
if(get_option('woocommerce_currency')==' ')
{
$currencies = explode(',', hana_get_opt('_cc'));
foreach ($currencies as $currency)
{
if $currency == get_option('woocommerce_currency'){
{
echo $currency;
break;
}
else{
echo "Select Currency";
break;
}
}
}
else
{
echo "Select Currency";
$currencies = explode(',', hana_get_opt('_cc'));
$currency=$currencies [0];
}
based on the assumption that get_option('woocommerce_currency') would return null if no currency is selected
<div class="search-menu">
<div class="btn-group fc">
<a class="btn dropdown-toggle" data-toggle="dropdown" href="#">
<?php
$currencies = explode(',', hana_get_opt('_cc'));
$currentCurrency = get_option('woocommerce_currency')
if ($currentCurrency === false) {
echo "select currency";
}
else{
echo $currentCurrency;
}
?>
<span class="caret"></span>
</a>
<ul class="dropdown-menu currency_switcher">
<?php
foreach ($currencies as $currency) {
if ($currency == $currentCurrency)
echo '<li>' . $currency . '</li>';
else
echo '<li>' . $currency . '</li>';
}
?>
</ul>
replace the if clause if ( $currentCurrency != null ) with whatever applies, if get_option('woocommerce_currency') returns anything other as null if not yet set
Related
<?php
$q = $pdo->prepare("SELECT * FROM tbl_menu WHERE menu_parent=? ORDER BY menu_order ASC");
$q->execute(array(0));
$res = $q->fetchAll();
foreach ($res as $row) {
$r = $pdo->prepare("SELECT * FROM tbl_menu WHERE menu_parent=?");
$r->execute(array($row['menu_id']));
$total = $r->rowCount();
echo ' <li class="header_nav-list_item dropdown">';
if($row['page_id'] == 0) {
if($row['menu_url'] == '') {
$final_url = 'javascript:void(0);';
} else {
$final_url = $row['menu_url'];
}
?>
<a class="nav-link nav-item dropdown-toggle d-inline-flex align-items-center"
href="<?php echo $final_url; ?>"><?php echo $row['menu_name']; ?><?php if($total) {echo ' <i class="icon-angle-down icon"></i>';}; ?></a>
<?php
} else {
$r = $pdo->prepare("SELECT * FROM tbl_page WHERE page_id=? ");
$r->execute(array($row['page_id']));
$res1 = $r->fetchAll();
foreach ($res1 as $row1) {
?>
<a class="nav-link nav-item dropdown-toggle d-inline-flex align-items-center"
data-bs-toggle="collapse"
data-bs-target="#Menu"
data-trigger="dropdown"
aria-expanded="false"
aria-controls="Menu"
data-page="" href="<?php echo BASE_URL.URL_PAGE.$row1['page_slug']; ?>"><?php echo $row1['page_name']; ?>
<?php if($total) {echo '<em></em>';}; ?></a>
<?php
}
}
// Checking for sub-menu
$r = $pdo->prepare("SELECT * FROM tbl_menu WHERE menu_parent=?");
$r->execute(array($row['menu_id']));
$total = $r->rowCount();
if($total) {
echo ' <div class="dropdown-menu collapse"><ul>';
$res1 = $r->fetchAll();
foreach ($res1 as $row1) {
//
echo '<li class="dropdown-list">';
if($row1['page_id'] == 0) {
if($row1['menu_url'] == '') {
$final_url1 = 'javascript:void(0);';
} else {
$final_url1 = $row1['menu_url'];
}
?>
<a class="dropdown-item nav-item" href="<?php echo $final_url1; ?>"><?php echo $row1['menu_name']; ?></a>
<?php
} else {
$s = $pdo->prepare("SELECT * FROM tbl_page WHERE page_id=?");
$s->execute(array($row1['page_id']));
$res2 = $s->fetchAll();
foreach ($res2 as $row2) {
?>
<a class="dropdown-item nav-item" href="<?php echo BASE_URL.URL_PAGE.$row2['page_slug']; ?>"><?php echo $row2['page_name']; ?></a>
<?php
}
}
echo '</li>';
//
}
echo '</ul></div>';
}
echo '</li>';
}
?>
This is my header code can any one tell me or updATE THIS CODE FOR ME
I WANT TO SOLVE MY PROBLEM
I am trying to create a hoverable mega menu, all works fine except the active one.
I checked in source codes and active class is added, but it doesn't show up the content until mouseenter on it.
see image please :
And this is when I go on it with mouse, it show up with no problem.
I gues I need to set main (FINANCE is main menu in pic) menu active, but dont know how to pass it to submenu.
Here is my full code :
$stmt_menu = $pdo->prepare("SELECT * FROM categories WHERE parentId = ? LIMIT 5");
$stmt_menu->execute([0]);
if ($stmt_menu->rowCount() > 0) {
$query = $stmt_menu->fetchAll();
foreach ($query as $row) {
if ($row['cat_type'] == 'mega') {
?>
<li class="dropdown megamenu-fw mega-li-<?php echo $row['catId']; ?>">
<?php echo $row['catName']; ?><span class="caret"></span>
<ul class="dropdown-menu megamenu-content dropdown-top" role="menu" aria-expanded="true" data-mega-ul="<?php echo $row['catId']; ?>">
<li>
<?php
$stmt_menu = $pdo->prepare("SELECT * FROM categories WHERE parentId = ? LIMIT 5");
$stmt_menu->execute([$row['catId']]);
if ($stmt_menu->rowCount() > 0) {
$subquery = $stmt_menu->fetchAll();
?>
<div class="sub-menu-left">
<ul class="nav-sub-categories">
<?php
$i = 0;
foreach ($subquery as $subq) {
$actives = '';
if($i == 0){
$actives = ' active';
}
?>
<li data-category-filter="<?php echo $subq['cat_seo_url']; ?>-<?php echo $subq['catId']; ?>" class="li-sub-category<?php echo $actives; ?>"><?php echo $subq['catName']; ?></li>
<?php
$i++;
}
?>
</ul>
</div>
<div class="sub-menu-right">
<?php
$i = 0;
foreach ($subquery as $subcats) {
$actives = '';
if($i == 0){
$actives = ' active';
}
?>
<div class="sub-menu-inner filter-<?php echo $subcats['cat_seo_url']; ?>-<?php echo $subcats['catId']; ?><?php echo $actives; ?>">
<div class="row row-menu-right">
<div class="col-sm-3 menu-post-item">
<div class="post-item-image">
<a href="is-allowance-instantly-strangers-applauded">
<img src="assets/img/img_bg_md.png" data-src="uploads/images/202203/image_380x226_624239afd7295.jpg" alt="Is allowance instantly strangers applauded" class="lazyload img-responsive img-post" width="1" height="1"/>
</a>
</div>
<h3 class="title"><?php echo $subcats['cat_seo_url']; ?> Is allowance instantly strangers applauded</h3>
<p class="post-meta">
admin
<span>Mar 28, 2022</span>
<span><i class="icon-comment"></i>2</span>
<span class="m-r-0"><i class="icon-eye"></i>894</span>
</p>
</div>
</div>
</div>
<?php
$i++;
}
?>
</div>
<?php
}
?>
</li>
</ul>
</li>
<?php
} elseif ($row['cat_type'] == 'dropdown') {
echo ' <li class="nav-item"><a class="nav-link" href="' . $row["cat_seo_url"] . '">test</a></li>';
} else {
echo ' <li class="nav-item"><a class="nav-link" href="' . $row["cat_seo_url"] . '">' . $row["catName"] . '</a></li>';
}
}
}
I want to display <li class = "active"> in my current URL when I click any menu.
Here is what I have tried:
<?php
$uri_string = $this->uri->uri_string();
$id_user = $usr->id;
$get_group = $this->db->get_where('users_groups', array('user_id'=> $id_user));
$hasil = $get_group->result();
foreach($hasil as $h)
if(isset($h->group_id)){
$in_group = $this->ion_auth->in_group($h->group_id);
if(isset($in_group)){
$get_menu = $this->db->get_where('menu',array('parent_menu' => 0, 'menu_users_groups' => $h->group_id));
$menu = $get_menu->result();
foreach($menu as $m){
$cekSub = $this->db->get_where('menu',array('parent_menu' => $m->id));
if($cekSub->num_rows() > 0){
echo '<li>';
echo '
<a href="javascript:void(0);" class="menu-toggle">
<i class="material-icons">'.$m->icon.'</i> <span>'.$m->menu_name.'</span>
</a>
<ul class="ml-menu">
<li>';
foreach($cekSub->result() as $c)
echo anchor(''.$c->controller_link.'','<i class="material-icons">'.$c->icon.'</i><span> '.$c->menu_name.'</span>');
echo '</li>
</ul>
</li>';
} else {
echo '<li>';
echo anchor(''.$m->controller_link.'','<i class="material-icons">'.$m->icon.'</i><span> '.$m->menu_name.'</span>');
echo '</li>';
}
}
}
}
?>
The problem is, that if not in the curent URL class, active is hidden - just <li> is displayed.
How to fix this?
it's solved with :
<?php
$getUri = $this->uri->uri_string();
$id_user = $usr->id;
$get_group = $this->db->get_where('users_groups', array('user_id'=> $id_user));
$hasil = $get_group->result();
foreach($hasil as $h)
if(isset($h->group_id)){
$in_group = $this->ion_auth->in_group($h->group_id);
if(isset($in_group)){
$get_menu = $this->db->get_where('menu',array('parent_menu' => 0, 'menu_users_groups' => $h->group_id));
$menu = $get_menu->result();
foreach($menu as $m){
$cekSub = $this->db->get_where('menu',array('parent_menu' => $m->id));
$getSub = $cekSub->result();
if($cekSub->num_rows() > 0){
echo '<li';
foreach($getSub as $c)
if($c->controller_link==$getUri){echo ' class="active"';}
echo '>
<a href="javascript:void(0);" class="menu-toggle">
<i class="material-icons">'.$m->icon.'</i> <span>'.$m->menu_name.'</span>
</a>
<ul class="ml-menu">
<li>';
foreach($getSub as $c)
echo anchor(''.$c->controller_link.'','<i class="material-icons">'.$c->icon.'</i><span> '.$c->menu_name.'</span>');
echo '</li>
</ul>
</li>';
} else {
echo '<li';
if($m->controller_link==$getUri){echo ' class="active"';}
echo '>';
echo anchor(''.$m->controller_link.'','<i class="material-icons">'.$m->icon.'</i><span> '.$m->menu_name.'</span>');
echo '</li>';
}
}
}
}
?>
Ok, I have done lots of research and looked at lots of stackoverflow questions but none have answered my question.
I'm building a simple blog and am at the moment i'm trying to build a simple categories/subcategories system but have hit an obstacle in making the subcategories load under their parent. If you know an easier method to the one i am doing below please let me know.
Here is how my db is structured:
id || name || parent_id || status
------------------------------------------
1 || category1 || NULL || 1
2 || subcategory1 || 1 || 1
Then i have my model code:
public function getCategories()
{
$results = $this->db->select('msi_items_categories','status = 1 AND parent_id = NULL');
if( !is_array($results[0]) ) {
$new_results = array();
array_push($new_results, $results);
return $new_results;
} else {
return $results;
}
}
public function getSubCategories($parent)
{
$bind = [':parent' => $parent];
$results = $this->db->select('msi_items_categories','status = 1 AND parent_id = :parent');
if( !is_array($results[0]) ) {
$new_results = array();
array_push($new_results, $results);
return $new_results;
} else {
return $results;
}
}
This is my controller code:
public function error()
{
$getSettings = $this->setting->getAll();
$getCategories = $this->setting->getCategories();
$getSubCategories = $this->setting->getSubCategories();
if(is_array($getCategories[0]) ) {
$isCategory = true;
} else {
$isCategory = false;
}
if(is_array($getSubCategories[0]) ) {
$isSubCategory = true;
} else {
$isSubCategory = false;
}
$data = [
'settings' => $getSettings,
'mainCategory' => $getCategories,
'subCategory' => $getSubCategories,
'isCategory' => $isCategory
'isSubCategory' => $isSubCategory
];
$this->view('index', $data);
}
Then I have my templatecode which is where im trying to do the foreach and the top category works fine but i just can't figure out how i do the subcategories. Before i started using the MVC model, i would simply just put the class inside the main category foreach and then put the id but now its all done in the controller so i cannot figure out how i do subcategories using the MVC model.
<?php foreach($data['mainCategory'] as $category) : ?>
<li class="dropdown">
<a class="dropdown-toggle nav-link dropdown-toggle pl-0" data-toggle="dropdown" aria-expanded="false" href="#"><?php echo $category['name']; ?></a>
<div class="dropdown-menu" role="menu">
<a class="dropdown-item" role="presentation" href="#"><i class="text-black-50 fas fa-box"></i> All <?php echo $category['name']; ?></a>
<div class="dropdown-divider" role="presentation"></div>
<?php foreach($data['subCategory'] as $scategory) : ?>
<a class="dropdown-item" role="presentation" href="#"><i class="text-black-50 <?php echo $scategory['icon']; ?>"></i> <?php echo $scategory['name']; ?></a>
<?php endforeach; ?>
</div>
</li>
<?php endforeach; ?>
Thanks very much!
$query = 'SELECT id, parent_id, name, icon FROM categories ORDER BY name';
$result = mysqli_query($conn, $query) OR trigger_error($query.'<br>'.mysqli_error($conn),E_USER_ERROR);
$refs = Array();
$categories = Array();
while($row = mysqli_fetch_assoc($result))
{
$thisref = &$refs[$row['id']];
$thisref['name'] = $row['name'];
$thisref['icon'] = $row['icon'];
if($row['parent_id']) $refs[$row['parent_id']]['children'][$row['id']] = &$thisref;
else $categories[$row['id']] = &$thisref;
}
echo '<ul class="nav navbar-nav mr-auto">';
foreach($categories as $category)
{
echo '<li class="dropdown">';
echo is_array($category['children'])
? '<a class="dropdown-toggle nav-link dropdown-toggle pl-0" data-toggle="dropdown" aria-expanded="false" href="#">'.$category['name'].'</a>'.sub($category['children'])
: $category['name'];
echo '</li>';
}
echo '</ul>';
function sub(&$subCat)
{
echo '<div class="dropdown-menu" role="menu">
<a class="dropdown-item hide-me" role="presentation" href="#"><i class="fas fa-fire mr-2"></i> Most Popular </a>
<div class="dropdown-divider hide-me" role="presentation"></div>';
foreach($subCat as $id => $subcategory)
{
echo '<a class="dropdown-item" role="presentation" href="'.FULL_ROOT.'/category/'.$id.'/"><i class="'.$subcategory['icon'].' mr-2"></i>'.$subcategory['name'].'</a>';
}
echo '</div>';
}
I have issues displaying a dropdown menu generated by a recursive function using a database. The parents are shown correctly below the items, but not as nav items, but just as a basic list.
Here's the image with how it actually looks:
And here's how it SHOULD look:
Some relevant code:
Model:
<?php
class New_menu_model extends CI_Model {
function get_domains() {
$result = $this->db->get ( 'domenii' );
return $result->result_array();
}
}
function recursive($parent, $result) {
$has_children = false;
foreach ( $result as $key => $value ) {
if ($value ['parent'] == $parent) {
if ($has_children === false && $parent) {
$has_children = true;
echo '<ul>' . "\n" ;
}
echo '<li>' . "\n";
echo '' . $value ['nume_domeniu'] . '' . " \n";
echo "\n";
recursive ( $key + 1, $result );
echo "</li>\n";
}
}
if ($has_children === true && $parent)
echo "</ul>\n";
}
?>
View:
<li class="dropdown"><?php echo recursive(0, $menu); ?></li>
Thanks!
It should be something like:
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" href="#" role="button" aria-haspopup="true" aria-expanded="false">
Dropdown <span class="caret"></span>
</a>
<ul class="dropdown-menu">
<?php echo recursive(0, $menu); ?>
</ul>
</li>
...and check echo '<ul>' . "\n" ; , it could be echo '<ul class="dropdown-menu">' . "\n" ; so the code should be:
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" href="#" role="button" aria-haspopup="true" aria-expanded="false">
Dropdown <span class="caret"></span>
</a>
<?php echo recursive(0, $menu); ?>
</li>