Crud Controller in CodeIgniter 3
<?php
defined("BASEPATH") or exit("No direct script access allowed");
class Crud extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model("Crud_model");
}
public function index()
{
$this->load->model("Crud_model");
$data["product_details"] = $this->Crud_model->getAllProducts();
$this->load->view("crud_view", $data);
}
public function addProduct()
{
$this->form_validation->set_rules("name", "Product Name", "trim|required");
$this->form_validation->set_rules("price", "Product Price", "trim|required");
$this->form_validation->set_rules("quantity", "Product Quantity", "trim|required");
if ($this->form_validation->run() == false) {
$data_error = [
"error" => validation_errors()
];
$this->session->set_flashdata($data_error);
} else {
$result = $this->Crud_model->insertProduct([
"name" => $this->input->post("name"),
"price" => $this->input->post("price"),
"quantity" => $this->input->post("quantity")
]);
if ($result) {
$this->session->set_flashdata("inserted", "Your data has been successfully inserted!");
}
}
redirect("Crud");
}
public function editProduct($id)
{
echo $id;
$data["singleProduct"] = $this->Crud_model->getSingleProduct($id);
$this->load->view("edit_view", $data);
}
public function update($id)
{
echo $id;
$this->form_validation->set_rules("name", "Product Name", "trim|required");
$this->form_validation->set_rules("price", "Product Price", "trim|required");
$this->form_validation->set_rules("quantity", "Product Quantity", "trim|required");
if ($this->form_validation->run() == false) {
$data_error = [
"error" => validation_errors()
];
$this->session->set_flashdata($data_error);
} else {
$result = $this->Crud_model->updateProduct([
"name" => $this->input->post("name"),
"price" => $this->input->post("price"),
"quantity" => $this->input->post("quantity")
], $id);
if ($result) {
$this->session->set_flashdata("updated", "Your data has been Successfully updated!");
}
}
redirect("Crud");
}
public function deleteProduct($id)
{
echo $id;
$result = $this->Crud_model->deleteItem($id);
if ($result == true) {
$this->session->set_flashdata("deleted", "Your data has been Successfully deleted!");
}
redirect("Crud");
}
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
Comments
Post a Comment