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>';
}
Related
So I've been struggling at how to echo this set of HTML with users that are set as admin from my database. I've looked at quite a few places for information but I'm struggling to get it to work. Perhaps I'm doing something really stupid. Thanks for your help.
<?php
$steamidb =&$steamprofile['steamid'];
$steamhextoid=dechex($steamidb);
$steamstart = 'steam:';
$steamhextoidfin = $steamstart . '' . $steamhextoid;
$sql = "SELECT group FROM users WHERE identifier='".$steamhextoidfin."'";
$result = $conn->query($sql);
if ($result->num_rows > 0)
// output data of each row
while($row = mysql_fetch_assoc($result))
{
if($row['group'] == 'admin')
{
echo '<li class="sub-menu">
<a href="javascript:;" >
<i class="fa fa-cogs"></i>
<span>Admin</span>
</a>
<!--<ul class="sub">
<li>COMING SOON</li>
<li>Buttons</li>
<li>Panels</li>
</ul>-->
</li>';
}
else {
echo "Error";
}
}
?>
I think that you forgot to put the while between {}. Here is the correct code:
<?php
$steamidb =&$steamprofile['steamid'];
$steamhextoid=dechex($steamidb);
$steamstart = 'steam:';
$steamhextoidfin = $steamstart . '' . $steamhextoid;
$sql = "SELECT group FROM users WHERE identifier='".$steamhextoidfin."'";
$result = $conn->query($sql);
if ($result->num_rows > 0){
// output data of each row
while($row = mysql_fetch_assoc($result))
{
if($row['group'] == 'admin')
{
echo '<li class="sub-menu">
<a href="javascript:;" >
<i class="fa fa-cogs"></i>
<span>Admin</span>
</a>
<!--<ul class="sub">
<li>COMING SOON</li>
<li>Buttons</li>
<li>Panels</li>
</ul>-->
</li>';
}
else {
echo "Error";
}
}
}
?>
Hello guyyz i need help in design,i don't have ideas about design, i have created dynamic menu in codeigniter i want to set that menu in treeview.
First click on parent menu after open child menus, so how to implement it help me
Here is My View Code:
<?php
foreach ($test as $val) {
$array = explode(",", $val->category_id);
}
foreach ($get_cat as $key => $value) {
if (in_array($value->category_id, $array)) {
echo $value->category_name . ' '; //Here My Menu Print
}
}
?>
You need to fetch all category with parent_id=0 then fetch subCategory. Try something like given below.
public function get_categories(){
$this->db->select('*');
$this->db->from('categories');
$this->db->where('parent_id', 0);
//Add here role condition
$parent = $this->db->get();
$categories = $parent->result();
$i=0;
foreach($categories as $p_cat){
$categories[$i]->sub = $this->sub_categories($p_cat->cat_id);
$i++;
}
return $categories;
}
Your subcategory function.
public function sub_categories($id){
$this->db->select('*');
$this->db->from('categories');
$this->db->where('parent_id', $id);
//add here role condition
$child = $this->db->get();
$categories = $child->result();
$i=0;
foreach($categories as $p_cat){
$categories[$i]->sub = $this->sub_categories($p_cat->cat_id);
$i++;
}
return $categories;
}
And your controller.
public function categories(){
$this->load->model('model_categories');
$data = $this->model_categories->get_categories();
print_r($data);
}
Here I got solution of dynamic menu design in CodeIgniter:
<?php
//GET CATEGORY ID FROM USER REGISTARTION
foreach ($test as $val) {
$array = explode(",", $val->category_id);
//CATEGORY ID MATCH WITH CATEGORY NAME FROM CATEGORY TABLE
foreach ($listMenuLevel1 as $key => $value) {
if (in_array($value->category_id, $array)) {
?>
<ul class="sidebar-menu">
<li class="treeview">
<a href="#">
<i class="fa fa-share"></i> <span><?php echo $value->category_name; ?></span>
<i class="fa fa-angle-left pull-right"></i>
</a>
<ul class="treeview-menu">
<?php foreach ($this->main_model->listchildMenus($value->category_id) as $menu2) : ?>
<li class="treeview">
<i class="fa fa-circle-o"></i><?php echo $menu2->category_name; ?><i class="fa fa-angle-left pull-right"></i>
<?php foreach ($this->main_model->listchildMenus($menu2->category_id) as $menu3): ?>
<ul class="treeview-menu">
<li class="treeview">
<i class="fa fa-circle-o"></i><?php echo $menu3->category_name; ?>
</li>
</ul>
<?php endforeach; ?>
</li>
<?php endforeach; ?>
</ul>
</li>
</ul>
<?php } ?>
<?php
}
}
?>
I have 2 tables: mb_category and mb_items, I'm trying to select items from mb_items with the same ID as the category they are assigned to in mb_category.
mb_category has 2 rows: id and category.
mb_items has 4 rows: id, item_name, item_price, and mb_category_id
What I want to do is select every item in mb_items with their corresponding category ID and echo them in the correct dropdown.
Here's what I have currently:
<ul class="nav">
<?php
$category = $db->read('mb_category', '*', '', 'ORDER BY id ASC');
// echo $category["count"] . "<br />";
// print_r($category);
foreach ($category["results"] as $row) {
?>
<li class="button-dropdown">
<a href="javascript:void(0)" class="dropdown-toggle">
<?php echo $row->category; ?> <span>▼</span>
</a>
<ul class="dropdown-menu">
<?php
$cid = $db->read('mb_category', 'id', '', '');
$item = $db->read('mb_items', '*', 'mb_category_id="$cid"', '');
// var_dump($item);
// echo $item["count"] . "<br />";
// print_r($item);
foreach ($item["results"] as $irow) {
?>
<li>
<a href="#">
<?php
echo $irow->item_name;
?>
</a>
</li>
<?php
}
?>
</ul>
</li>
<?php
}
?>
And this is my SELECT:
public function read($table_name, $cols, $where = NULL, $limit = NULL) {
//GET $cols FROM $table_name WHERE $where returns 0 for false and fetch_object() for true
$query = "SELECT " . $cols . " FROM " . $table_name;
if ($where) {
$query .= " WHERE " . $where;
}
if ($limit) {
$query .= " " . $limit;
}
//echo $query;
$sql = $this->query($query);
if ($sql) {
$data = array();
$data['count'] = $sql->num_rows;
while ($row = $sql->fetch_object()) {
$data['results'][] = $row;
}
return $data;
}
return false;
}
}
I can suggest you 2 things (I didn't test the code I'm giving you, it's just to show you the idea).
1) You are doing too many requests, you only need one :
SELECT c.id AS category_id, c.category AS category_name, i.id AS item_id, i.item_name
FROM mb_category c
LEFT JOIN mb_items i ON c.id = i.mb_category_id
2) format your data and use it to display your list :
<?php
// format data
$categories = [];
foreach ($category["results"] as $row) {
if (empty($categories[$row[category_id]])) {
$categories[$row[category_id]] = [
'id' => $row[category_id],
'name' => $row['category_name'],
'items' => [],
];
}
if (!empty($categories[$row[item_id]])) {
$categories[$row[category_id]]['items'][] = [
'id' => $row['item_id'],
'name' => $row['item_name'],
];
}
}
?>
<ul class="nav">
<?php
// display data
foreach ($categories as $categoryId => $category) {
?>
<li class="button-dropdown">
<a href="javascript:void(0)" class="dropdown-toggle">
<?php echo $category['name']; ?> <span>▼</span>
</a>
<ul class="dropdown-menu">
<?php foreach ($category['items'] as $item) { ?>
<li>
<a href="#">
<?php
echo $item['name'];
?>
</a>
</li>
<?php } ?>
</ul>
</li>
<?php } ?>
</ul>
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>
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