show all categories in ecommerce - deesoft service

show all categories in ecommerce

Deepak Tailor Image
Deepak Tailor - Jan 18 2021
show all categories in ecommerce

In this article, we will show the categories for the products in the database on the home page. Also, the products will also count how many products are available in which category. We will set the category in the 4 4 column in the card.

Call fetch_all_records() model function in controller

The fetch_all_records model function is called the index function in the controller. This function has to pass three parameters. Table name, order type, limit etc. The data that is coming in this function is passed to the view file.

$data['categories'] = $this->cm->fetch_all_records('categories','asc','limit');
$this->load->view('home/index',$data);
Write fetch category query in model

In the model fetch_all_records function, the data is fetched in the ascending order from the category table. And have returned the result.

public function fetch_all_records($tablename,$order,$limit)
{
	if($limit == "limit"){

	}
	else{
		$this->db->limit($limit);
	}
	$fetch_data = $this->db->select()
			 ->from($tablename)
			 ->order_by('id',$order)
			 ->get();
	if($fetch_data->num_rows() > 0){
		return $fetch_data->result();
	}
	else{
		return $fetch_data->result();
	}
}
Show all categories in view file

In the foreach loop, a function is called to count the category product from the helper on each category.

<div class="row" style="margin-bottom: 0px;margin-top: 10px;">
	<?php if(count($categories)): 
		foreach($categories as $cate): 
			$count_all = fetch_category_products('products',$cate->id,'limit'); ?>
	<div class="col l3 m4 s12" style="margin-bottom: 0px;">
		<div class="card waves-effect waves-dark" style="box-shadow: none;width: 100%;">
			<div class="card-content" style="padding: 0px;">
				<div class="row" style="margin-bottom: 0px;">
					<div class="col l7 m7 s7" style="margin: auto;padding: 10px;">
						<h6 style="font-weight: 500;font-size: 16px;"><?= $cate->name; ?></h6>
						<h6 style="font-size: 14px;color: grey;margin-top: 5px;"><?= count($count_all); ?> Items</h6>
						<a href="<?= base_url('home/category/'.$cate->id); ?>" class="btn btn-small waves-effect waves-light" style="background: #ff3d00;box-shadow: none;">Show Now</a>
					</div>
					<div class="col l5 m5 s5" style="padding: 0px;padding-bottom: 0px;">
						<img src="<?= base_url().'uploads/category_image/'.$cate->image; ?>" class="responsive-img" style="border:none;margin-bottom: -5px;width: 100%;">
					</div>
				</div>
			</div>			
		</div>
	</div>
<?php endforeach;
else: ?>
<?php endif; ?>
</div>
Helper function call for count specific category products

In this function, single category products are counted. How many products have a single category used

function fetch_category_products($tablename,$id,$limit)
{
	$CI =& get_instance();
	if($limit == "limit"){

	}
	else{
		$CI->db->limit($limit);
	}
	$fetch_data = $CI->db->select()
		   ->from('products')
		   ->where('category_id',$id)
		   ->order_by('id','desc')
		   ->get();
	if($fetch_data->num_rows() > 0){
		return $fetch_data->result();
	}
	else{
		return $fetch_data->result();
	}
}
Deepak Tailor Image
Deepak Tailor

My name is Deepak tailor as a fullstack developer. I have been in the IT industry (PHP, Nodejs, flutter) for the last 5 years. For professional and customize web development & app development, you can send inquiry on our email.
----
You can contact him at deepaktailor10@yahoo.in