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
Related
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);
}
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;
}
}
}
?>
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
}
}
so I believe I have it right (im new to active records).
the problem I am having I keep getting error
Message: Call to a member function getbank() on a non-object
My code is simple
Controller
class Profile extends CI_Controller
{
function index()
{
$data = array();
if($query = $this->profile_model->getbank())
{
$data['records'] = $query;
}
$this->load->view('profile_view', $data);
}
}
model
class Profile_model extends CI_Model {
function getbank()
{
$query = $this->db->get('bank');
return $query->results();
}
Its so simple, I cant see a error, thank you.
Try this
Controller
class Profile extends CI_Controller
{
function index()
{
$data = array();
$this->load->model('profile_model'); # Added
$query = $this->profile_model->getbank(); # Changed
if(!empty($query)) # Changed
{
$data['records'] = $query;
}
$this->load->view('profile_view', $data);
}
}
Model
class Profile_model extends CI_Model {
public function getbank() # Changed
{
$query = $this->db->get('bank');
return $query->result(); # Changed
}
}
Update
Simple answer, thanks to all those who tried helping. I had the model file name with a small letter not a capital.
Eg profile_model.php
should be Profile_model.php
However, this solution is elegant and simple, so it improved my code so il accept this one.
It may be because you didn't load your model?
Try changing your controller to:
class Profile extends CI_Controller
{
function index()
{
$data = array();
$this->load->model('Profile_model');
if($query = $this->Profile_model->getbank())
{
$data['records'] = $query;
}
$this->load->view('profile_view', $data);
}
}
What is the best way to create my model so I can call it from my controller like this?
$this->model_model->function->dbname
I have the following model but its rubbish:
Model:
function systemName()
{
$query = $this->db->query("SELECT cms_name FROM options");
return $query->result();
}
Update:
function systemOptions($options)
{
$this->db->select($options);
$query = $this->db->get('options');
return $query->num_rows();
}
Why would you want to? Why not call it like this?:
$this->model_model->functionname('dbname');
A complete skeleton of a model is like this:
<?php
class Mymodel extends Model
{
function get_some_entries($number_rows)
{
return $result = $this -> db -> query("SELECT id, name
FROM tablename
LIMIT $number_rows");
}
}
?>
Best way? You can probably read on CodeIgniter general guide and alter to your situation. But the basic idea is that, you create the model class then load from your controller. Then just call the model function accordingly. For example,
class Custom_model extends CI_Model {
function __construct()
{
parent::__construct();
}
function systemName()
{
$query = $this->db->query("SELECT cms_name FROM options");
return $query->result();
}
...
...
function systemOptions($options)
{
$this->db->select($options);
$query = $this->db->get('options');
return $query->num_rows();
}
}
<?php
class CustomController extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('custom_model', 'fubar');
}
public function index()
{
$result = $this->fubar->systemName('dbname');
print_r ($result);
}
}
?>