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
}
}
Related
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');
}
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;
}
}
}
?>
I'm beginner in codeigniter.I'm trying to delete a row from my table.I tried the following code.But it's not working.
This is url i'm passing.
Delete User
This is my controller (Delete_user.php).
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Delete_user extends CI_Controller {
public function delete_row($id) {
$this->load->model('Delete_selecteduser');
$where = array('id' => $id);
$this->Delete_selecteduser->delete_user('users', $where);
}
And here is my model (Delete_selecteduser.php).
<?php
class Delete_selecteduser extends CI_Model {
public function __construct() {
$this->load->database();
}
public function delete_user($table, $where = array()) {
$this->db->where($where);
$res = $this->db->delete($table);
if ($res)
return TRUE;
else
return FALSE;
}
}
And also I want to display a message that record has been deleted successfully.Please help me to fix this.
i suggest that the function must be
public function delete_user($table, $where = array()) {
$this->db->where($where);
$this->db->delete($table);
$res = $this->db->affected_rows();
return $res;
}
in this way you always have a answer, 0 for a no deleted row or 1 if you delete only a row
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);
I have the following.
controllers/customers.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
session_start(); //we need to call PHP's session object to access it through CI
class customers extends CI_Controller {
function __construct()
{
parent::__construct();
}
public function view($id) {
$this->load->model('customers');
$news = $this->customers->view_customer($id);
$data['title'] = $news['title'];
$data['body'] = $news['body'];
$this->load->view('customers_customer_view', $data);
}
function index()
{
if($this->session->userdata('logged_in'))
{
$session_data = $this->session->userdata('logged_in');
$data['username'] = $session_data['username'];
$this->load->view('customers_view', $data);
}
else
{
//If no session, redirect to login page
redirect('login', 'refresh');
}
}
function logout()
{
$this->session->unset_userdata('logged_in');
session_destroy();
redirect('dashboard', 'refresh');
}
}
?>
models/customer.php
<?php
class customers_model extends CI_Model {
public function __construct() {
$this->load->database();
}
public function view_customer($id) {
if($id != FALSE) {
$query = $this->db->get_where('news', array('id' => $id));
return $query->row_array();
}
else {
return FALSE;
}
}
}
?>
views/customers_customer_view.php
<?php print $title; ?>
<?php print $body; ?>
I am very new to code igniter, I have followed this tutorial from the web, No matter what i do i cannot get the database info to display when loading root/customers/view/1
All i get is a blank page. Even if i change the view to include a some static text it wont display, From this i believe it to be something wrong with loading the view, But all looks ok to me.
Please can somebody assist.
You wrote:
$this->load->model('customers');
But model file is named: customer.php.
And class name is: customers_model.
Please check it again.
I will give you an example:
$this->load->model('customers');
Your model file have to be: customers.php.
And your class name have to be: class Customers {}