EVOLUTION-NINJA
Edit File: Constituency.php
<?php namespace App\Controllers; use CodeIgniter\Controller; use App\Models\Constituency_model; class Constituency extends BaseController{ public function __construct() { $this->db = \Config\Database::connect(); date_default_timezone_set('Asia/Kolkata'); } public function create_constituency(){ $constituencyName=$this->request->getpost('Constituency_Name'); // $MLA_name=$this->request->getpost('MLA_Name'); $district=$this->request->getpost('district'); $taluk=$this->request->getpost('taluk'); // $address=$this->request->getpost('Address'); $created_at=date('y-m-d H-i-s'); $created_by = session()->get('username'); $data=['constituency_name'=> $constituencyName, // 'mla_name' => $MLA_name, 'taluk' =>$taluk, 'district' => $district, // 'address' =>$address, 'created_at'=>$created_at, 'created_by'=>$created_by ]; $model= new Constituency_model(); try { if($model->insert($data)){ return $this->response->setJSON(['result'=>1,'message'=>'Create Constituency successfully']); }else{ return $this->response->setJSON(['result'=>0,'message'=>'failed create project']); } }catch(\Exception $e){ return $this->response->setJSON(['result' => 0, 'message' => $e->getMessage()]); } } public function constituency_list_data(){ $model= new Constituency_model(); $data = $model->orderBy('created_at', 'DESC')->findAll(); if($data){ return $this->response->setJSON($data); }else{ return $this->response->setJSON(['result'=>0,'message'=>'Failed to load data']); } } public function constituency_delete($id){ try { $builder = $this->db->table('constituency'); $builder->where('id', $id); if ($builder->delete()) { return $this->response->setJSON(['result' => 1, 'message' => 'constituency deleted successfully']); } else { return $this->response->setJSON(['result' => 0, 'message' => 'Failed to delete constituency']); } } catch (\Exception $e) { return $this->response->setJSON(['result' => 0, 'message' => $e->getMessage()]); } } public function edit_constituency_data($id) { try { $builder = $this->db->table('constituency'); $builder->where('id', $id); $query = $builder->get(); $data = $query->getResult(); $allConstituencies = $this->db->table('constituency')->select('id, constituency_name')->get()->getResult(); if ($data) { return $this->response->setJSON([ 'result' => 1, 'data' => $data, 'allConstituencies' => $allConstituencies ]); } else { return $this->response->setJSON(['result' => 0, 'message' => 'No data found']); } } catch (\Exception $e) { return $this->response->setJSON(['result' => 0, 'message' => $e->getMessage()]); } } public function update_constituency(){ $id = $this->request->getPost('id'); $constituencyName = $this->request->getPost('Constituency_model'); // Correct field name $district = $this->request->getPost('district_model'); $taluk = $this->request->getPost('taluk_model'); $address = $this->request->getPost('address_model'); $created_at = date('Y-m-d H:i:s'); $created_by = session()->get('username'); // Prepare the data array $data = [ 'constituency_name' => $constituencyName, 'district' => $district, 'taluk' => $taluk, 'address' => $address, 'created_at' => $created_at, 'created_by' => $created_by ]; $builder = $this->db->table('constituency'); $builder->where('id', $id); $update = $builder->update($data); if ($update) { return $this->response->setJSON(['result' => 1, 'message' => 'Constituency updated successfully']); } else { return $this->response->setJSON(['result' => 0, 'message' => 'Failed to update Constituency']); } } public function constituency_master(){ $constituencyName=$this->request->getpost('constituency'); $MLA_name=$this->request->getpost('mla'); $created_at=date('y-m-d H-i-s'); $created_by = session()->get('username'); $data=['constituency_name'=> $constituencyName, 'mla' => $MLA_name, 'created_at' => $created_at, 'created_by' => $created_by ]; $builder = $this->db->table('constituency_master'); try { if($builder->insert($data)){ return $this->response->setJSON(['result'=>1,'message'=>'Create Constituency successfully']); }else{ return $this->response->setJSON(['result'=>0,'message'=>'failed create constituency']); } }catch(\Exception $e){ return $this->response->setJSON(['result' => 0, 'message' => $e->getMessage()]); } } } ?>