<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class dept_officer extends CI_Model{
function __construct()
{
parent::__construct();
}
function get_sponsorship_list()
{
$this->db->from('tbl_donor');
$this->db->join('tbl_sponsorship', 'tbl_donor.DonorId = tbl_sponsor.DonorId');
$query = $this->db->get();
return $query->result();
}
function get_programme_list()
{
$this->db->from('tbl_program');
$this->db->join('tbl_sponsorship', 'tbl_program.SponsorshipId = tbl_sponsor.ponsorshipId');
$query = $this->db->get();
return $query->result();
}
function get_child_list()
{
$this->db->from('tbl_child');
$this->db->join('tbl_family', 'tbl_child.FamilyID = tbl_family.FamilyID');
$this->db->join('tbl_school', 'tbl_child.SchoolID = tbl_school.SchoolID');
$query = $this->db->get();
return $query->result();
}
function get_crpo_list()
{
$this->db->from('tbl_crpo');
$this->db->join('tbl_district', 'tbl_ds_division.DistrictI = tbl_district.DistrictId');
$this->db->join('tbl_dsdivision', 'tbl_crpo.ds_divisionId = tbl_ds_division.ds_divisionId');
$query = $this->db->get();
return $query->result();
}
}
?>
I've created this class to contains all the queries to display list of different users in the system. i get a compile error saying cannot redeclare class dept_officer even though i haven't tried to declare any classes.
The above is the code for the model in codeigniter
Related
I have the code like below, but when I update the data, the data is not updated but add new data in the database
How can I make updates / edit data and not add new data when saved?
Here I use CodeIgniter version 3.1.8, php version 7.2.4
model_anggota.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Model_anggota extends CI_model {
public function getdata($key){
$this->db->where('nama',$key);
$hasil = $this->db->get('tb_anggota');
return $hasil;
}
public function getupdate($key,$data){
$this->db->where('nama',$key);
$this->db->update('tb_anggota',$data);
}
public function getinsert($data){
$this->db->insert('tb_anggota',$data);
}
public function getdelete($key){
$this->db->where('nim',$key);
$this->db->delete('tb_anggota');
}
}
data_anggota.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Data_anggota extends CI_Controller {
public function index()
{
$this->model_security->getsecurity();
$isi['content'] = 'data_anggota/tampil_dataanggota';
$isi['data'] = $this->db->get('tb_anggota');
$this->load->view('tampilan_home',$isi);
}
public function tambah(){
$this->model_security->getsecurity();
$isi['content'] = 'data_anggota/form_tambahanggota';
$this->load->view('tampilan_home',$isi);
}
public function edit(){
$this->model_security->getsecurity();
$isi['content'] = 'data_anggota/form_editanggota';
$key = $this->uri->segment(3);
$this->db->where('nim',$key);
$query = $this->db->get('tb_anggota');
if($query->num_rows()>0){
foreach ($query->result() as $row) {
$isi['nama'] = $row->nama;
$isi['nim'] = $row->nim;
$isi['jurusan'] = $row->jurusan;
$isi['tempat_lahir'] = $row->tempat_lahir;
$isi['tanggal_lahir'] = $row->tanggal_lahir;
$isi['alamat'] = $row->alamat;
$isi['agama'] = $row->agama;
$isi['kelamin'] = $row->kelamin;
$isi['angkatan'] = $row->angkatan;
$isi['status'] = $row->status;
}
}
$this->load->view('tampilan_home',$isi);
}
public function simpan(){
$this->model_security->getsecurity();
$key = $this->input->post('nim');
$data['nama'] = $this->input->post('nama');
$data['nim'] = $this->input->post('nim');
$data['jurusan'] = $this->input->post('jurusan');
$data['tempat_lahir'] = $this->input->post('tempat_lahir');
$data['tanggal_lahir'] = $this->input->post('tanggal_lahir');
$data['alamat'] = $this->input->post('alamat');
$data['agama'] = $this->input->post('agama');
$data['kelamin'] = $this->input->post('kelamin');
$data['angkatan'] = $this->input->post('angkatan');
$data['status'] = $this->input->post('status');
$this->load->model('model_anggota');
$query = $this->model_anggota->getdata($key);
if($query->num_rows()>0){
$this->model_anggota->getupdate($key,$data);
$this->session->set_flashdata('info','Data sukses diupdate');
}else{
$this->model_anggota->getinsert($data);
$this->session->set_flashdata('info','Data sukses disimpan');
}
redirect('data_anggota');
}
public function delete(){
$this->model_security->getsecurity();
$this->load->model('model_anggota');
$key = $this->uri->segment(3);
$this->db->where('nim',$key);
$query = $this->db->get('tb_anggota');
if($query->num_rows()>0){
$this->model_anggota->getdelete($key);
$this->session->set_flashdata('info','Data sukses dihapus');
}
redirect('data_anggota');
}
}
The problem is you are getting nim value as $key, and sending that to check in the name column. so, it's returning 0. as per your code, if you the 0 return data will be inserted instead of update. as well as in the update part also, you are checking nim value in name field.
update your codes like following
public function getdata($key){
$this->db->where('nim',$key);
$hasil = $this->db->get('tb_anggota');
return $hasil;
}
public function getupdate($key,$data){
$this->db->where('nim',$key);
$this->db->update('tb_anggota',$data);
}
I am trying to execute the stored procedure from my SQL server database and make it print a list, what is the best way to print it out?
This is my Model:
class Categories_Model extends CI_Model
{
public function __construct()
{
parent::__construct();
}
function get_categories()
{
$sql = 'exec spGetCategoriesByID';
$query = $this->db->query($sql);
$result = $query->result();
return $result;
}
}
This is my Controller:
class Categories_Controller extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->helper('url');
$this->load->database();
}
public function categories_list()
{
$this->load->model('Categories_Model');
$cat_result = $this->Categories_Model->get_categories();
$data['catlist'] = $cat_result;
$this->load->view('categories',$data);
}
}
Try getting the error like this:
$query = $this->db->query($sql);
echo 'Error #'.$this->db->_error_number().': '.$this->db->_error_message();
die();
I am new in codeigniter framework of PHP. I want display the news headlines in scrolling marquee I tried this
view page
<marquee bgcolor="black"><font color="white">
<?php
foreach($news as $row)
{
$heading=$row->st_head;
echo $heading;
}
?>
</font ></marquee>
controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Headlines extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->model('Headlines_model');
}
public function index()
{
$where_cond = "flg_is_active = 1";
$select_cond = "st_head,in_news_id";
$data['news'] = $this->Headlines_model->select_records($where_cond,$select_cond );
$this->load->view('Headlines',$data);
}
}
*****Model **************
<?php
class Headlines_model extends CI_Model
{
function __construct()
{
parent::__construct();
$this->load->database();
$this->table_name = 'tbl_news';
}
public function select_records($where=NULL,$select=NULL,$jointable=NULL,$joinkey=NULL,$per_page=NULL,$limit=NULL,$order_by=NULL, $count = false)
{
if($where!=NULL)
$this->db->where($where);
if($select!=NULL)
$this->db->select($select);
if($jointable!=NULL && $joinkey!=NULL)
$this->db->join($jointable, $joinkey);
if($limit!=NULL || $per_page!=NULL)
$this->db->limit($limit, $per_page);
if($order_by!=NULL)
$this->db->order_by($order_by);
$query = $this->db->get($this->table_name);
if($count==true)
{
return $query->num_rows();
}
else
{
return $query->result();
}
}
}
?>
if I display without marquee the show the data properly. But when I use marquee then it gives following error
PHP Error was encountered
Severity: Notice
Message: Undefined variable: news
Filename: views/Headlines.php
Line Number: 2
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class checklist extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->helper('url');
$this->load->helper('form');
$this->load->model('checklist_model');
}
public function index()
{
$data['check_list'] = $this->checklist_model->get_all_details();
$this->load->view('show_checklist', $data);
}
public function form_dropdown()
{
$this->load->view('insert_checklist');
}
public function data_submitted()
{
$data = array('dropdown_single' => $this->input->post('Technology'));
$this->load->model('checklist_model');
$this->checklist_model->insert_in_db($data);
$this->load->model("checklist_model");
$result = $this->checklist_model->read_from_db($data);
$data['result'] = $result[0];
$this->load->view('insert_checklist', $data);
}
public function add_form()
{
$this->load->view('insert_checklist');
}
public function edit()
{
$id = $this->uri->segment(3);
$data['details'] = $this->checklist_model->getById($id);
$this->load->view('edit', $data);
}
public function delete($id)
{
$this->checklist_model->delete_a_detail($id);
$this->index();
}
public function insert_new_details()
{
$udata['technology_name'] = $this->input->post('technology_name');
$udata['questions'] = $this->input->post('questions');
$udata['status'] = $this->input->post('status');
$udata['comments'] = $this->input->post('comments');
$res = $this->checklist_model->insert_details_to_db($udata);
if($res)
{
header('location:'.base_url()."/index.php/checklist/".$this->index());
}
}
public function update()
{
$mdata['technology_name']=$_POST['technology_name'];
$mdata['questions']=$_POST['questions'];
$mdata['status']=$_POST['status'];
$mdata['comments']=$_POST['comments'];
$res=$this->checklist_model->update_info($mdata, $_POST['id']);
if($res)
{
header('location:'.base_url()."/index.php/checklist/".$this->index());
}
}
}
?>
it means you call for an array variable's index 'question', and that index does not exist in that array. Maybe in function update(), $_POST['question']. Not sure. You should provide the full error msg, like file and line.
Also: the view if that's the case. And make this sound more like a question.
I am trying to learn code igniter and creating helper files. I have this as my functions_helper.php file located in my applications/helpers folder:
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
function __construct()
{
parent::__construct();
$this->load->model('user','',TRUE);
}
if (!function_exists('check_status')){
function check_status()
{
$CI = & get_instance();
$result=$CI->user->get_status(); //this is line 14!!
if($result)
{
$status_array=array();
foreach($result as $row)
{
$status_array=array(
'source' => $row->status,
'current' => $row->id
);
if ($status_array['current'] == 1) {
$status_array['current'] = "Live";
} else {
$status_array['current'] = "Amazon is Down";
}
$CI->session->set_userdata('status',$status_array);
}
return TRUE;
}
else
{
return false;
}
} // END OF CHECK_STATUS FUNCTION
}
From everything i can find, I am doing it correctly, yet I am getting this error:
PHP Fatal error: Call to a member function get_status() on a non-object in function_helper.php on line 14. exactly what am i doing wrong? Is this the best way to call a function? Ultimately I am trying to get information returned from a db query.
My get_status function is:
//FUNCTION TO SEE THE STATUS OF AMAZON
function get_status() {
$query = $this->db->query("select * from amz where active = 1");
if($query->num_rows()==1) {
return $query->row_array();
}else {
return false;
}
} //END OF GET_STATUS FUNCTION
Remove this code
function __construct()
{
parent::__construct();
$this->load->model('user','',TRUE);
}
There is no OO in helper files. You do not have a class -__construct() are used to initialize classes.
Then move
$this->load->model('user','',TRUE); to the check_status function
$CI =& get_instance();
$CI->load->model('user','','TRUE');