Edit and delete data by id with codeigniter - php

I have a problem with edit and delete data in CodeIgniter.
I've tried some optional but it wasn't work. can somebody help me?
here I attached controller and model.
View
data bahan baku
kode_bahan_baku = kbb
nama_bahan_baku = hps
edit bahan baku = edit_dbb
delete bahan baku = delete_dbb
Model:
<?php defined('BASEPATH') OR exit('No direct script access allowed');
Class Model_data extends CI_Model{
public function __construct(){
parent::__construct();
}
// Data Bahan Baku
public function set_dbb(){
$data = array(
'bb_code' => $this->input->post('kbb'),
'bb_price' => $this->input->post('hps')
);
return $this->db->insert('data_bahan_baku',$data);
}
public function get_dbb(){
$query = "select * from data_bahan_baku";
$sql = $this->db->query($query);
return $result = $sql->result();
}
public function edit_dbb(){
$data = array(
'bb_code' => $this->input->post('kbb'),
'bb_price' => $this->input->post('hps')
);
$this->db->where('kbb',$id);
$edit = $this->db->update('data_bahan_baku',$data);
return $edit;
}
public function delete_dbb($id){
$data = array(
'bb_code' => $this->input->post('kbb'),
'bb_price' => $this->input->post('hps')
);
$this->db->where('kbb',$id);
return $this->db->delete('data_bahan_baku');
}
Controller
<?php defined('BASEPATH') OR exit('No direct script access allowed');
Class Control_data extends CI_Controller{
public function __construct(){
parent::__construct();
$this->load->model("model_data");
}
// Entry Data
/*Input Data Bahan Baku*/
public function data_bahan_baku(){
$data = array();
$data['title'] = "Data Bahan Baku";
$data['result'] = $this->model_data->get_dbb();
$this->form_validation->set_rules('kbb','Kode Bahan Baku','required');
$this->form_validation->set_rules('hps','Harga Per Satuan','required');
if($this->form_validation->run() == FALSE){
$this->load->view('data/data_bahan_baku',$data);
}else{
$this->model_data->set_dbb();
$this->load->view('data/data_bahan_baku',$data);
}
}
/*Edit Data Bahan Baku*/
public function edit_data_bahan_baku($id){
$this->model_data->edit_dbb($id);
}
/*Delete Data Bahan Baku*/
public function delete_data_bahan_baku($id){
$this->model_data->delete_dbb($id);
redirect('control_data/data_bahan_baku');
}

Related

How to call model in controller on constructor because I need write only one time

I create a model for fetching data then write code
model..
function get_courses(){
$this->db->from(TABLE_COURSE);
$this->db->where('name !=', '');
$query = $this->db->get();
$result = '';
if($query){
if($query->num_rows() > 0)
$result = $query->result();
}
return $result;
}
Controller
defined('BASEPATH') OR exit('No direct script access allowed');
class Quiz extends CI_Controller {
public function __construct(){
parent::__construct();
$this->load->model('Mastermodel','',TRUE);
}
public function index()
{
$data['courses'] = $this->Mastermodel->get_courses();
$data['view_file'] = "content/quiz/quiz_list";
$this->load->view('layout/dashboard/layout', $data);
}
public function quiz_of_day()
{
$data['courses'] = $this->Mastermodel->get_courses();
$data['view_file'] = "content/quiz/quiz_of_day";
$this->load->view('layout/dashboard/layout', $data);
}
public function quiz_edit()
{
$data['courses'] = $this->Mastermodel->get_courses();
$data['view_file'] = "content/quiz/quiz_edit";
$this->load->view('layout/dashboard/layout', $data);
}
}
How to call model in controller on constructor because I need write only one time?
To achieve what you want you can do the following...
You need to make the $data array a property of the class
Reference the new $this->data throughout the class
You need to move your call to the model in the constructor.
So what you get is this
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Quiz extends CI_Controller {
protected $data = array(); // Old school definition of an array (instead of [])for safety
public function __construct() {
parent::__construct();
$this->load->model('Mastermodel', '', TRUE);
$this->data['courses'] = $this->Mastermodel->get_courses();
}
public function index() {
$this->data['view_file'] = "content/quiz/quiz_list";
$this->load->view('layout/dashboard/layout', $this->data);
}
public function quiz_of_day() {
$this->data['view_file'] = "content/quiz/quiz_of_day";
$this->load->view('layout/dashboard/layout', $this->data);
}
public function quiz_edit() {
$this->data['view_file'] = "content/quiz/quiz_edit";
$this->load->view('layout/dashboard/layout', $this->data);
}
}
Tim's method would work just find, but I tend to do it more this way.
class Quiz extends CI_Controller {
public $courses; // can also be set to private
public function __construct(){
parent::__construct();
$this->load->model('Mastermodel','',TRUE);
$this->courses = $this->Mastermodel->get_courses();
}
public function index()
{
$data['courses'] = $this->courses;
$data['view_file'] = "content/quiz/quiz_list";
$this->load->view('layout/dashboard/layout', $data);
}

How to pass an array to the model from the controller in codeIgniter

I am trying to pass an array to the model from the controller in codeIgnitor I know this question have some answers but I tried all of them and no solution works with me. So this my code:
Controller:
<?php
if(! defined('BASEPATH')) exit('No direct script access allowed');
class SearchController extends CI_Controller{
var $controller = "user";
public function index(){
$this->home();
}
public function home(){
$this->load->view("search_view");
}
public function search(){
if(isset($_POST['submit'])){
$data['type']=$this->input->post('type');
$data['value']=$this->input->post('value');
$this->load->model("SearchModel");
$this->load->SearchModel->getCadre($data);
}
}
}
?>
Model:
<?php
class SearchModel extends CI_Model{
function construct(){
parent::__construct();
$this->getCadre();
}
function getCadre(){
$query = $this->db->get('cadres');
$query = $this->db->where($type,$value);//the argument witch i want to take from array
if($query->num_rows()>0){
$values = result();
return $query->result();
}
else{
return NULL;
}
}
}
?>
Try below code in your model::
class SearchModel extends CI_Model{
function construct(){
parent::__construct();
}
function getCadre($data = array()){
$this->db->where($data);//the argument witch i want to take from array
$query = $this->db->get('cadres');
if($query->num_rows()>0){
// $values = result();
return $query->result();
}
else{
return NULL;
}
}
}
OR you can pass individual data as below and return result...Also try as below
Controller:
if(! defined('BASEPATH')) exit('No direct script access allowed');
class SearchController extends CI_Controller{
var $controller = "user";
public function index(){
$this->home();
}
public function home(){
$this->load->view("search_view");
}
public function search(){
if(isset($_POST['submit'])){
$type=$this->input->post('type');
$value=$this->input->post('value');
$this->load->model("searchmodel");
$data = $this->searchmodel->getCadre($type,$value);
if($data==NULL){
echo 'No records found!!';
}
else
{
//send data to view
$this->load->view('view_name',$data);
}
}
}
}
Model
class SearchModel extends CI_Model{
function construct(){
parent::__construct();
$this->load->database();
}
function getCadre($type,$value){
$this->db->where('type',$type);//the argument witch i want to take from array
$this->db->where('value',$value);
$query = $this->db->get('cadres');
if($query->num_rows()>0){
// $values = result();
return $query->result();
}
else{
return NULL;
}
}
}
?>

Approving a message using Codeigniter

When on of the admin clicks an approve button its supposed to add the user_id of the person approving to a field in approval table
This is the button
<?php echo anchor('admin/messages/approve/'.$message->id.'', 'Approve', 'class="btn btn-success"'); ?>
This is the approve function in Controller
public function approve($id) {
$data = array(
'first_approval' => $this->session->userdata('user_id')
);
$this->Message_model->approve($id, $data);
//isset Message
$this->session->set_flashdata('success', 'Your approval was send');
//Redirect
redirect('admin/messages');
}
This is the approve method inside the Model
public function approve($data) {
$this->db->select('*');
$this->db->from('approval');
$this->db->join('messages', 'messages.id = approval.sms_id');
$this->db->where('id', 'sms_id');
$this->db->set('first_approval', $data);
$this->db->update('approval');
}
I have sms_id, first_approval, second_approval and third_approval columns in my approval table but for now I'm just testing if I can put session->user_id in the column 'first_approval'
Change your approve method as following in MODEL
public function approve($id, $data) {
$this->db->where('sms_id', $id);
$this->db->update('approval',$data);
}
Please check below view model and controller
View
<?php echo anchor('admin/messages/approve/'.$message->id.'', 'Approve', 'class="btn btn-success"'); ?>
Controller
class Messages extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('message_model');
}
function approve(){
$id = $this->uri->segment(3); //Get message id
$data = array(
'first_approval' => $this->session->userdata('user_id')
);
$res = $this->Message_model->approve($id, $data);
if($res >0){
//Redirect
redirect('admin/messages');
}else{
//some error accured
}
}
}
Model
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Message_model extends CI_Model {
public function __construct() {
$this->load->database(); //If database not set in autoload
}
public function approve($id, $data) {
$this->db->where('sms_id', $id);
return $this->db->update('approval',$data); //If table update return no of rows update
}
}

CodeIgniter, pass data from model to controller to display sql operations results in controller

This is my Login controller
<?php if (! defined('BASEPATH')) exit('No direct script access allowed');
class Login extends CI_Controller
{
function __construct()
{
parent::__construct();
}
public function index($msg = NULL)
{
$this->load->helper('url');
// Load the model
$this->load->model('login_model');
// Validate the user can login
$this->login_model->loginAction();
}
}
?>
And my Model Login_model is
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Login_model extends CI_Model
{
function __construct()
{
parent::__construct();
}
public function loginAction()
{
// grab user input
$email = $this->input->get('email');
$password = $this->input->get('password');
// Prep the query
$this->db->where('email', $email);
$this->db->where('password', $password);
// Run the query
$query = $this->db->get('users');
foreach ($query->result() as $row)
{
$dbid= $row->id;
$dbemail= $row->email;
}
$value=$query->num_rows();
if($value == 1)
{
$details = array(
'status'=>'sucess',
'message'=>'sucessfully logedin ',
'id' => $dbid,
'email' => $dbemail,
);
echo json_encode($details);
}
else
{
$detail = array(
'status'=>'failed',
'message'=>'invalid email password ',
);
echo json_encode($detail);
return false;
}
}
}
?>
This is working, but I need to use $details & $detail arrays in my Login controller to encode json data. for that what all modifications that I need to put in both my model and controller. I am new to codeIgniter.
Make changes as below
<?php if (! defined('BASEPATH')) exit('No direct script access allowed');
class Login extends CI_Controller
{
function __construct()
{
parent::__construct();
}
public function index($msg = NULL)
{
$this->load->helper('url');
// Load the model
$this->load->model('login_model');
// Validate the user can login
$result = $this->login_model->loginAction();
if($result['status'])
{
$details = array(
'status'=>'sucess',
'message'=>'sucessfully logedin ',
'id' => $result['data']->id,
'email' => $result['data']->email,
);
}else{
$detail = array(
'status'=>'failed',
'message'=>'invalid email password '
);
}
echo json_encode($detail);
}
}
?>
And my Model Login_model is
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Login_model extends CI_Model
{
function __construct()
{
parent::__construct();
}
public function loginAction()
{
// grab user input
$email = $this->input->get('email');
$password = $this->input->get('password');
// Prep the query
$this->db->where('email', $email);
$this->db->where('password', $password);
// Run the query
$query = $this->db->get('users');
$value=$query->num_rows();
if($value == 1)
{
return ['status'=>true, 'data'=>$query->row_object()];
}
else
{
return ['status'=>false];
}
}
}
?>

Codeigniter: passing whole database from model to view

Hi i am not sure why are my codes not working.
This is the Controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Admin extends CI_Controller {
public function __construct(){
parent::__construct();
if (! $this->session->userdata('is_logged_in')){
redirect('main/restricted');
} else {
$privilege = $this->session->userdata('privilege');
if ($privilege == 'member'){
redirect('main/restricted');
}
}
}
public function index() {
$this->load->model("model_books");
$data2 = $this->model_books->select_book();
$data = array(
'title' => 'Admin Page'
);
$this->load->view("header", $data);
$this->load->view("admin", $data2);
$this->load->view("footer");
}
This is the Model:
<?php
class Model_books extends CI_Model {
public function select_book(){
$query = $this->db->get('books');
if ($query){
return $query->result();
} else {
return false;
}
}
}
This is the View:
<?php
echo $data2->content;
?>
I got around 10 books inside the database however the books in the database are not showing up.
Try this
public function index() {
$this->load->model("model_books");
$data = array(
'title' => 'Admin Page',
'books' => $this->model_books->select_book()
);
$this->load->view("header", $data);
$this->load->view("admin", $data);
$this->load->view("footer");
}
In view
<?php
print_r($books);
?>
try the following in constructor function in model
$db = $this->load->database('default', TRUE);

Categories