CRUD product menu on the admin page [Bagian 5]

At manufacture website like shop on-line, CRUD (Create, read, update and Extinguish) is the basis for production website dynamic. Here users or administrators can add, view, change and delete data on the website.

There are 2 menus on the store admin page on-line These are products and invoices. In the product menu, the administrator can set the articles to be sold. While the invoice is an invoice letter issued by the seller to the customer in accordance with the agreement specified in the order or purchase order. So that the admin has a record of the transaction of goods purchased by members.

Also read:

This article explains how to create a CRUD product menu on the admin page for administrators to view, add, and change-To update and delete products to be sold. Just check out the reviews from the following authors:

READ (SHOW) PRODUCTS ON THE ADMIN PAGE

1. Create a new folder named Backend by doing C: xampp htdocs online-shop application views.

2. Create files admin_menu.php in the folder C: xampp htdocs online-shop application views backend.

3. Create a folder named admin in it C: xampp htdocs online-shop applications controller, then create a file Products.php in this folder.

session->userdata('group') != '1'){			$this->session->set_flashdata('error','Sorry, you are not logged in!');			redirect('login');		}				//load model -> model_products		$this->load->model('model_products');	}		public function index()	{		$data['products'] = $this->model_products->all();		$this->load->view('backend/view_all_products', $data);	}}

Program logic:

  • public function __construct () Function for defining user sessions and calling model_products.php
  • public function index () is used to display a list of products for sale by getting data in model_products.php

4. Create the Model_products.php file in the folder C: xampp htdocs online-shop application models Then enter the following program section:

db->get('products');		if($hasil->num_rows() > 0){			return $hasil->result();		} else {			return array();		}	}		public function find($id){		//Query mencari record berdasarkan ID-nya		$hasil = $this->db->where('id', $id)						  ->limit(1)						  ->get('products');		if($hasil->num_rows() > 0){			return $hasil->row();		} else {			return array();		}	}}

Program logic:

  • public functions all () is used to retrieve the records in the products table and then display them in the views folder with the file name Products.php.
  • public function find (id) Used to search for records based on the ID field in the product table.

Screenshot of the display of sales products on the admin page

Displaying a list of online store selling products on the admin page

CREATE (ADD) PRODUCTS ON THE Admin PAGE

1. Create an upload folder in it C: xampp htdocs onlineshop. This folder is used to save uploaded images when adding new products.

2. Add function in the Products.php file contained in the folder C: xampp htdocs online-shop application controllers.

public function create(){		//form validation sebelum mengeksekusi QUERY INSERT		$this->form_validation->set_rules('name', 'Product Name', 'required');		$this->form_validation->set_rules('description', 'Product Description', 'required');		$this->form_validation->set_rules('price', 'Product Price', 'required|integer');		$this->form_validation->set_rules('stock', 'Available Stock', 'required|integer');		//$this->form_validation->set_rules('userfile', 'Product Image', 'required');		if ($this->form_validation->run() == FALSE)		{			$this->load->view('backend/form_tambah_product');		} else {			//load uploading file library			$config['upload_path'] = './uploads/';			$config['allowed_types'] = 'jpg|png';			$config['max_size']	= '300'; //KB			$config['max_width']  = '2000'; //pixels			$config['max_height']  = '2000'; //pixels			$this->load->library('upload', $config);						if ( ! $this->upload->do_upload())			{				//file gagal diupload -> kembali ke form tambah				$this->load->view('backend/form_tambah_product');			} else {				//file berhasil diupload -> lanjutkan ke query INSERT				// eksekusi query INSERT				$gambar = $this->upload->data();				$data_product =	array(					'name'			=> set_value('name'),					'description'	=> set_value('description'),					'price'			=> set_value('price'),					'stock'			=> set_value('stock'),					'image'			=> $gambar['file_name']				);				$this->model_products->create($data_product);				redirect('admin/products');			}					}	}

Program logic:

  • The public function create () is used to create Form validation before execution Insert query to add data. In addition to the configuration of theUpload and save images with a maximum image capacity of 300 kb.

3. Add function in the Model_products.php file located in C: xampp htdocs online-shop application models.

public function create($data_products){		//Query INSERT INTO		$this->db->insert('products', $data_products);	}

Program logic:

  • public function create () Function to enter or add record new in the product table.

4. Create files form_add_product.php in the folder C: xampp htdocs online-shop application views backend.

			Admin Page | Add New Product																								

Add New Product

'form-horizontal']) ?>

Screenshot of adding sales products on the admin page

Add sales products in the online shop

UPDATING (EDITING) PRODUCTS ON THE ADMIN PAGE

1. Add function Update the Product.php file in the folder C: xampp htdocs online-shop application controllers admin.

public function update($id){		$this->form_validation->set_rules('name', 'Product Name', 'required');		$this->form_validation->set_rules('description', 'Product Description', 'required');		$this->form_validation->set_rules('price', 'Product Price', 'required|integer');		$this->form_validation->set_rules('stock', 'Available Stock', 'required|integer');		if ($this->form_validation->run() == FALSE)		{			$data['product'] = $this->model_products->find($id);			$this->load->view('backend/form_edit_product', $data);		} else {			if($_FILES['userfile']['name'] != ''){				//form submit dengan gambar diisi				//load uploading file library				$config['upload_path'] = './uploads/';				$config['allowed_types'] = 'jpg|png';				$config['max_size']	= '300'; //KB				$config['max_width']  = '2000'; //pixels				$config['max_height']  = '2000'; //pixels				$this->load->library('upload', $config);										if ( ! $this->upload->do_upload())				{					$data['product'] = $this->model_products->find($id);					$this->load->view('backend/form_edit_product', $data);				} else {					$gambar = $this->upload->data();					$data_product =	array(						'name'			=> set_value('name'),						'description'	=> set_value('description'),						'price'			=> set_value('price'),						'stock'			=> set_value('stock'),						'image'			=> $gambar['file_name']					);					$this->model_products->update($id, $data_product);					redirect('admin/products');				}			} else {				//form submit dengan gambar dikosongkan				$data_product =	array(					'name'			=> set_value('name'),					'description'	=> set_value('description'),					'price'			=> set_value('price'),					'stock'			=> set_value('stock')				);				$this->model_products->update($id, $data_product);				redirect('admin/products');			}		}	}

Program logic:

  • The public function update () is used to get the primary key value from the product table, id, and then becomes an object to do form validation on changing data. And connect to model_products to invoke the query update table command.

2. Add the update function to the Model_product.php file in the folder C: xampp htdocs online-shop application models.

public function update($id, $data_products){		//Query UPDATE FROM ... WHERE id=...		$this->db->where('id', $id)				 ->update('products', $data_products);	}

Program logic:

  • The update of the public function makes changes to the record by applying the value Primary key ie the ID in the product table.

3. Display the form_edit_product.php file in the folder C: xampp htdocs online-shop application views backend.

input->post('is_submitted')){	$name			= set_value('name');	$description	= set_value('description');	$price			= set_value('price');	$stock			= set_value('stock');} else {	$name			= $product->name;	$description	= $product->description;	$price			= $product->price;	$stock			= $product->stock;}?>			Admin Page | Edit Product																								

Edit Product

'form-horizontal']) ?>

Screenshot for editing sales products on the admin page

Edit online store sales products

DELETE PRODUCTS ON THE ADMIN PAGE

1. Add the delete function to the products.php file in the folder C: xampp htdocs online-shop application controllers admin.

public function delete($id){		$this->model_products->delete($id);		redirect('admin/products');	}

2. Add the delete function in the Model_products.php file in the folder C: xampp htdocs online-shop application models.

public function delete($id){		//Query DELETE ... WHERE id=...		$this->db->where('id', $id)				 ->delete('products');	}

Well that is the article about Selling CRUD Products on the Admin Page of this online store website. The next article is about creating an invoice for the sales product. Keep up to date with the creation of this online shop website. Have a good rest.






Leave a Reply

Your email address will not be published.