I want to make a menu list based on roles, but when I want to display them to my home view it doesnt show up the list.. how can I fix it? the problem is that the menu list is not showing with separates files.
controller
public function getModules($id_module){
if($this->session->userdata('log')){
$data = $this->session->userdata('log');
$menu = array();
$seccions = $this->module->get_rows();
foreach ($seccions as $index => $seccion){
$modules = $this->module->query("SELECT CONCAT('".$seccion['id']."',storelte_modulo.id) AS id,CONCAT('".base_url('assets/img/sidebar')."','/',storelte_modulo.icon) as icon, storelte_modulo.modulo AS value,storelte_modulo.seccion_id,CONCAT('".base_url()."',storelte_modulo.url) AS url FROM storelte_modulo INNER JOIN storelte_modulo_perfil ON storelte_modulo_perfil.modulo_id = storelte_modulo.id WHERE seccion_id = $seccion[id] AND storelte_modulo_perfil.perfiles_id = $data[id] AND storelte_modulo_perfil.STATUS = 1");
$seccions[$index]['data']= $modules;
if (!count($seccions[$index]['data']))
unset($seccions[$index]);
}
foreach ($seccions as $item)
array_push($menu,$item);
$this->data['fields'] = $menu;
$this->load->view('modules_view',$this->data);
}
}
model
public function get_rows(){
$this->db->select('id,seccion');
$this->db->from('storelte_seccion');
return $this->db->get()->result_array();
}
public function query($query){
return $this->db->query($query)->result_array();
}
view
home.php
<section class="content">
<!-- Small boxes (Stat box) -->
<div class="row">
<h3 class="text-center">Welcome to storeLTE, click a module below to get started!</h3>
<div class="home_module_list">
<div class="module_item">
<?php $this->load->view('modules_view'); ?>
</div>
</div>
</div>
<!-- Main row -->
<div class="row">
<!-- Left col -->
<section class="col-lg-12 connectedSortable">
</section>
</div>
</section>
modules
<?php $this->load->view('header'); ?>
<div class="module_item">
<?php foreach ($fields as $session) : ?>
<?php foreach ($session['data'] as $itemData) : ?>
<div class="module_item" title="<?= $itemData['value'] ?>">
<img src="<?= $itemData['icon'] ?>"/>
<?= $itemData['value']?>
</div>
<?php endforeach ?>
<?php endforeach ?>
</div>
try this at your model
public function get_rows(){
$this->db->select('id,seccion');
$this->db->from('storelte_seccion');
$this->db->get();
return $query->result();
}
Related
I'm making a website in CodeIgniter and I'm trying to make a menu bar with all the categories, and when you click on a certain category it must show all the products that are in that category but its not really working.
The categories are showing on the category side menu bar, but when I click on a certain category link its going to the right view page but instead of the product information I'm getting this error:
A PHP Error was encountered
Severity: Error
Message: Cannot use object of type stdClass as array
Filename: views/category.php
Line Number: 41
Backtrace:
Line 41 in category.php is this line:
<a href="<?php echo base_url() ?>/Product/details/<?php echo $product['product_id']; ?>">
So I'm guessing it can't echo product detail information?
Here is my Allecadeaus controller to echo products:
<?php
class AlleCadeausController extends CI_Controller {
public function index()
{
$this->load->model('Product_model');
$this->load->model('allecadeaus_model');
$data['products'] = $this->allecadeaus_model->get_products();
$this->load->view('allecadeaus', $data);
}
}
My Allecadeaus_model model file which I use to echo products:
<?php
class Allecadeaus_model extends CI_Model {
public function __construct()
{
parent::__construct();
}
public function get_products()
{
$query = $this->db->query('SELECT * FROM products');
$result = $query->result_array();
return $result;
}
}
category.php view file:
<?php include_once ('templates/header.php'); ?>
<!-- Alle cadeaus gele title bovenaan pagina -->
<div class="container-fluid">
<div class="row">
<div class="col-lg-12 bg-warning" style="font-size:25px">
<center>Alle cadeaus</center>
</div>
</div>
</div>
<hr />
<br>
<!-- Cadeau categorie side menu -->
<div class="container-fluid">
<div class="row">
<div class="col-md-4">
<div id="categorymenu">
<center> <h3>Categorieën</h3> </center>
<ul class="list-group">
<?php foreach (get_categories_h() as $category) : ?>
<li class="list-group-item">
<?php echo $category->name; ?>
</li>
<?php endforeach; ?>
</ul>
</div>
</div>
<!-- Laat cadeau zien op alle cadeaus pagina -->
<div class="col-md-8">
<?php foreach($products as $product) : ?>
<div class="col-md-2">
<div id="product">
<a href="<?php echo base_url() ?>/Product/details/<?php echo $product['product_id']; ?>">
<img src="<?php echo base_url(); ?>upload/<?php echo $product['product_foto_thumb']; ?>">
</a>
<div class="product_naam"><?php echo $product['product_naam']; ?></div>
<div class="ophaal_plaats">
<?php echo $product['ophaal_plaats']; ?>
</div>
<div class="aangeboden_door">
<p>Aangeboden door: Peter</p>
</div>
</div>
</div>
<?php endforeach; ?>
</div>
<div class="clearfix"></div>
<?php include_once ('templates/footer.php'); ?>
My whole allecadeaus.php view file:
<?php include_once ('templates/header.php'); ?>
<!-- Alle cadeaus gele title bovenaan pagina -->
<div class="container-fluid">
<div class="row">
<div class="col-lg-12 bg-warning" style="font-size:25px">
<center>Alle cadeaus</center>
</div>
</div>
</div>
<hr />
<br>
<!-- Cadeau categorie side menu -->
<div class="container-fluid">
<div class="row">
<div class="col-md-4">
<div id="categorymenu">
<center> <h3>Categorieën</h3> </center>
<ul class="list-group">
<?php foreach (get_categories_h() as $category) : ?>
<li class="list-group-item">
<?php echo $category['name']; ?>
</li>
<?php endforeach; ?>
</ul>
</div>
</div>
<!-- Laat cadeau zien op alle cadeaus pagina -->
<div class="col-md-8">
<?php foreach($products as $product) : ?>
<div class="col-md-2">
<div id="product">
<a href="<?php echo base_url() ?>/Product/details/<?php echo $product['product_id']; ?>">
<img src="<?php echo base_url(); ?>upload/<?php echo $product['product_foto_thumb']; ?>">
</a>
<div class="product_naam"><?php echo $product['product_naam']; ?></div>
<div class="ophaal_plaats">
<?php echo $product['ophaal_plaats']; ?>
</div>
<div class="aangeboden_door">
<p>Aangeboden door: Peter</p>
</div>
</div>
</div>
<?php endforeach; ?>
</div>
<div class="clearfix"></div>
<?php include_once ('templates/footer.php'); ?>
My Product.php controller file:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Product extends CI_Controller {
var $data = array();
public function index()
{
//Laad kado uploaden view
$this->load->view('product_form', $this->data);
}
public function details($product_id)
{
//load the Product_model
$this->load->model('Product_model');
//call function getdata in de Product_model
$data['userdetail_list'] = $this->Product_model->getdata();
//get product details
$data['product'] = $this->Product_model->get_product_details($product_id);
//laad view
$data['main_content'] = 'details';
$this->load->view('details',$data);
}
//categorie product function
public function category($id)
{
$data['title'] = 'Category';
$data['page'] = 'Product/category';
$data['category'] = $this->Category_model->findByCategory($id);
$data['products'] = $this->Product_model->findByCategory($id);
$this->Category_model->findByCategory($id);
$this->load->view('category', $data);
}
public function __construct()
{
parent::__construct();
$this->load->model('Product_model');
$this->load->helper(array('form', 'url'));
}
My Product_model file:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Product_model extends CI_model {
public function saveProduct($data) {
$this->db->insert('products', $data);
$product_id = $this->db->insert_id();
return $product_id;
}
public function getdata()
{
$this->db->select('users.user_id,users.email,users.voornaam,products.product_id,products.category_id,products.product_naam');
$this->db->from('users');
$this->db->join('products','products.user_id = users.user_id');
$query = $this->db->get();
if($query->num_rows()>0)
{
return $query->result_array();
}
}
public function get_product_details($product_id) {
$arrReturn = array();
$this->db->select('*');
$this->db->from('products');
$this->db->where('product_id', $product_id);
$query = $this->db->get();
$result = $query->result_array();
if (!empty($result)) {
$arrReturn = $result[0];
}
return $arrReturn;
}
/*
Get categories
*/
public function get_categories(){
$this->db->select('*');
$this->db->from('categories');
$query = $this->db->get();
$result = $query->result();
return $result;
}
public function findAll(){
return $this->db->get('products')->result();
}
public function findByCategory($id){
$this->db->where('id', $id);
return $this->db->get('categories')->result();
}
}
?>
My Category_model file:
<?php
class Category_model extends CI_Model {
public function findAll(){
$this->db->get('categories')->result();
}
public function findByCategory($id)
{
$this->db->where('id', $id);
$this->db->get('categories')->result();
return $this->db->get('categories')->row();
}
}
?>
My db_helper.php
<?php if (!function_exists('get_categories_h')) {
function get_categories_h(){
$CI = get_instance();
$categories = $CI->Product_model->get_categories();
return $categories;
} } ?>
Some database information:
Table 1: 5 columns: products
-product_id
-product_naam
-product_beschrijving
-user_id
-category_id
table 2: categories: 2 columns:
1.id
2.name
You can try this solution for your problem:
Whole allecadeaus.php view file:
<ul class="list-group">
<?php foreach (get_categories_h() as $category) : ?>
<li class="list-group-item">
<?php echo $category->name; ?>
</li>
<?php endforeach; ?>
</ul>
is your database already have the filed category_id, is the name correct? can you show screen from your categories and products table?
I'm trying to create a search form in codeigniter. This is my controller and model,
Shop.php [ Controller ]
public function s(){
$q = $this->input->get('q');
if( $q == null ){
$this->index();
}
$data['products'] = $this->product_model->search_products( $q );
$this->load->view('shop', $data);
}
Product_model.php
public function search_products( $keyword ){
$this->db->select('product.id, product.product_name, product.product_description, product.product_image1,
brand.brand_name, category.category_name, type.type_name, stock.selling_price,
(SELECT COALESCE(ROUND(SUM(rate)/( COUNT(rate)*5 )*100), 0) FROM rating WHERE rating.product_id = product.id)AS rating,
(SELECT COALESCE(COUNT(rating.id), 0) FROM rating WHERE rating.product_id = product.id)AS rate_count');
$this->db->from('product');
$this->db->join('stock','stock.product_id = product.id');
$this->db->join('brand', 'brand.id = product.brand_id');
$this->db->join('category', 'category.id = product.category_id');
$this->db->join('type', 'type.id = product.type_id');
$this->db->join('color', 'color.id = product.color_id');
$this->db->where('MATCH(product.product_name) AGAINST("'.$keyword.'")');
$this->db->or_where('MATCH(brand.brand_name) AGAINST("'.$keyword.'")');
$this->db->or_where('MATCH(category.category_name) AGAINST("'.$keyword.'")');
$this->db->or_where('MATCH(type.type_name) AGAINST("'.$keyword.'")');
$this->db->where('stock.qty >', 0);
$this->db->group_by('product.id');
$query = $this->db->get();
return $query->result_array();
}
But return value in null. So I tried calling search_product( $keyword ) function in my view and it worked. but when I print $data['product'] variable and its empty.
What is the problem here? Thank you.
Edit
The search result shows here.
<div class="row product-list-item">
<?php
// This is just used for check the query
$p = $this->product_model->search_products( 'mesh' );
echo '<br>';
print_r($products);
if( $products != null ){
foreach ( $products as $product ){
?>
<!-- item.2 -->
<div class="product-item-element col-sm-6 col-md-6 col-lg-4">
<!--Product Item-->
<div class="product-item">
<div class="product-item-inner">
<div class="product-img-wrap">
<img src="<?php echo base_url($product['product_image1']);?>" alt="">
</div>
<div class="product-button">
<i class="fa fa-eye"></i>
</div>
</div>
<div class="product-detail">
<p class="product-title"><a href="<?php echo site_url('shop/view/'.$product['id'])?>">
<?php echo $product['product_name']; ?>
</a></p>
<div class="product-rating">
<div class="star-rating" itemprop="reviewRating" itemscope="" itemtype="" title="Rated 4 out of 5">
<span style="width: <?php echo $product['rating'];?>%"></span>
</div>
<a href="#" class="product-rating-count">
<span class="count"><?php echo $product['rate_count'];?></span> Reviews
</a>
</div>
<p class="product-description">
<?php echo $product['product_description']; ?>
</p>
<h5 class="item-price"><?php echo $product['selling_price']; ?></h5>
</div>
</div>
<!-- End Product Item-->
</div>
<?php
}
}else{
?>
<div class="text-center no-items">
<h4>No items</h4>
</div>
<?php } ?>
</div>
I have an error in the product detail, I do not know what else to change where again. when I change $detail into $categories succeed but is the same as function submenu. It was not according to my wishes
My View
<!-- navbar -->
<div class="row">
<?php foreach ($categories as $row) : ?>
<div class="col-sm-3">
<h5><?php echo $row->categoriesName; ?></h5>
<ul><li><a href="<?php echo base_url();?>member/detail">
<?php echo $row->productName; ?></a> </li></ul>
</div>
<?php endforeach; ?>
</div>
<!-- product detail -->
<?php foreach ($detail as $details_new) : ?>
<div class="box">
<h1 class="text-center"><?php echo $details_new->productName;?></h1>
<p class="price"><?php echo $details_new->price;?></p>
</div>
<?php endforeach; ?>
My Controller
public function home() {
$data=array('title'=>'Pasar Online | Ayo Belanja Sekarang',
'username'=> $this->session->userdata('username'),
'product' => $this->product_model->all(),
'categories' => $this->categories_model->get_main(),
'isi' =>'member/index');
$this->load->view('template_home/wrapper',$data);
}
public function detail() {
$data['username'] = $this->session->userdata('username');
$data['categories'] = $this->categories_model->get_main();
$data['detail'] = $this->categories_model->get_details();
$this->load->view('member/coba',$data);
}
My Model
public function get_main(){
$this->db->select('product.*,categories.*');
$this->db->from('product');
$this->db->join('categories','product.categoriesID=categories.categoriesID');
$this->db->limit(5);
$query = $this->db->get();
if($query->num_rows() > 0) {
$results = $query->result();
} return $results;
}
public function get_details() {
$query = $this->db->query("SELECT * FROM product WHERE productID");
$row = $query->row();
}
still can not display a single product
I do not know where my mistake... please help me, thanks
you are iterating details array in wrong format.
public function get_details() {
$query = $this->db->query("SELECT * FROM product WHERE productID");
$row = $query->row();
return $row;
}
This will return only single row without any indexing like - 0,1,2...
and on view page you are using foreach loop and foreach loop use indexing to iterate value you can iterate like that --
<div class="box">
<h1 class="text-center"><?php echo $detail->productName; ?></h1>
<p class="price"><?php echo $detail->price; ?></p>
</div>
please use this way hope it will work..
In codeigniter row() selects only first row of your result in object format.So no need to use foreach loop.Just try like this...
<div class="box">
<h1 class="text-center"><?php echo $detail->productName;?></h1>
<p class="price"><?php echo $detail->price;?></p>
</div>
And
In model
public function get_details() {
$query = $this->db->query("SELECT * FROM product WHERE productID");
$row = $query->row();
return $row;
}
it is because of $detail variable is empty, no data available in this variable, always check for data in variable before looping
<?php if(!empty($detail)){
foreach ($detail as $details_new) : ?>
<div class="box">
<h1 class="text-center"><?php echo $details_new->productName;?></h1>
<p class="price"><?php echo $details_new->price;?></p>
</div>
<?php endforeach; } ?>
I tried adding a cart in my application, but how you can while in order to add a new cart page does not reload, but the cart increases.
my controllers
function add_cart($code) {
$produk = $this->Model->get_id($code);
$data = array(
'id' => $produk->code
'qty' => 1,
'name' => $produk->name );
$this->cart->insert($data);
}
Model
function get_id($code) {
$query = $this->db->select('*')
->from('mytable')
->where('code', $code)
->get();
if ($query->num_rows() > 0) {
foreach ($query->result() as $data) {
$hasil[] = $data;
}
return $hasil;
}
}
View
<?php foreach($product as $row) ?>
<div class="col-sm-4">
<div class="col-item">
<div class="info">
<div class="row">
<div class="price col-md-12">
<h5> <?php echo $row->name_product ;?></h5>
</div>
</div>
<div class="separator clear-left">
<p class="btn-add">
class="hidden-sm" href="<?php echo site_url('home/add_cart/'.$row->code);?>">Add</a></p>
</div>
</div>
</div>
</div>
<?php endforeach ?>
Use ajax to replace the href to request the add_chart operation, and get the response to refresh the page content without reload the page.
how can I fix it the values from fields because when I write in url http://localhost/storeLTE/login/getmodules/1 everything works well, but when I want to pass them through my home with <?php $this->load->view('modules_view'); ?> it isnt work as separate files. how can I fix it?
controller
public function getModules($id_module){
if($this->session->userdata('log')){
$data = $this->session->userdata('log');
$menu = array();
$seccions = $this->module->get_rows();
foreach ($seccions as $index => $seccion){
$modules = $this->module->query("SELECT CONCAT('".$seccion['id']."',storelte_modulo.id) AS id,CONCAT('".base_url('assets/img/sidebar')."','/',storelte_modulo.icon) as icon, storelte_modulo.modulo AS value,storelte_modulo.seccion_id,CONCAT('".base_url()."',storelte_modulo.url) AS url FROM storelte_modulo INNER JOIN storelte_modulo_perfil ON storelte_modulo_perfil.modulo_id = storelte_modulo.id WHERE seccion_id = $seccion[id] AND storelte_modulo_perfil.perfiles_id = $data[id] AND storelte_modulo_perfil.STATUS = 1");
$seccions[$index]['data']= $modules;
if (!count($seccions[$index]['data']))
unset($seccions[$index]);
}
foreach ($seccions as $item)
array_push($menu,$item);
$this->data['fields'] = $menu;
$this->load->view('modules_view',$this->data);
}
}
view
<div class="row">
<h3 class="text-center">Welcome to storeLTE, click a module below to get started!</h3>
<div class="home_module_list">
<div class="module_item">
<?php $this->load->view('modules_view'); ?>
</div>
</div>
</div>
view_modules
<?php $this->load->view('header'); ?>
<div class="module_item">
<?php foreach ($fields as $session) : ?>
<?php foreach ($session['data'] as $itemData) : ?>
<div class="module_item" title="<?= $itemData['value'] ?>">
<img src="<?= $itemData['icon'] ?>"/>
<?= $itemData['value']?>
</div>
<?php endforeach ?>
<?php endforeach ?>
</div>
Try changing the following line:
<?php $this->load->view('modules_view'); ?>
to:
<?php $this->load->view('modules_view', ['fields' => $fields]); ?>
This should give modules_view.php access to your $fields variable from the controller. Hope this helps!