I am new learner of CI.
I want to display data from database. but it doesn't work.
Code in controller :
function __construct()
{
parent::__construct();
$this->load->database();
$this->load->model("user_model");
$this->lang->load('basic', $this->config->item('language'));
// redirect if not loggedin
if(!$this->session->userdata('logged_in')){
redirect('login');
}
$logged_in=$this->session->userdata('logged_in');
if($logged_in['base_url'] != base_url()){
$this->session->unset_userdata('logged_in');
redirect('login');
}
}
public function index($limit='0')
{
$logged_in=$this->session->userdata('logged_in');
if($logged_in['su']!='1'){
exit($this->lang->line('permission_denied'));
}
$data['limit']=$limit;
$data['title']=$this->lang->line('userlist');
// fetching user list
$data['result']=$this->user_model->user_list($limit);
$this->load->view('header',$data);
$this->load->view('user_list',$data);
$this->load->view('footer',$data);
}
public function teachercontrol()
{
$logged_in=$this->session->userdata('logged_in');
$sut='2';
$this->db->where('su',$sut);
// fetching user list
$data['teacher']=$this->user_model->teachermodel();
$this->load->view('header',$data);
$this->load->view('user_list',$data);
$this->load->view('footer',$data);
}
my model :
function teachermodel(){
$this->db->order_by('savsoft_users.uid','desc');
$this -> db -> join('savsoft_group', 'savsoft_users.gid=savsoft_group.gid');
$query=$this->db->get('savsoft_users');
return $query->result_array();
}
I try tro print in view, but it doesn't show any data :
<?php print_r($teacher)?>
What is the mistake?
while, the index function in controller (above the teachercontroller function) works well
Thanks
Put where condition in modal from teacher controller:
public function teachercontrol()
{
$logged_in=$this->session->userdata('logged_in');
$sut='2';
// fetching user list
$data['teacher']=$this->user_model->teachermodel($sut);
$this->load->view('header',$data);
$this->load->view('user_list',$data);
$this->load->view('footer',$data);
}
my model :
function teachermodel($sut = ""){
if($sut != "") {
$this->db->where('savsoft_users.su',$sut);
}
$this->db->join('savsoft_group', 'savsoft_users.gid=savsoft_group.gid', 'left');
$this->db->order_by('savsoft_users.uid','desc');
$query=$this->db->get('savsoft_users');
return $query->result_array();
}
Related
could ya'll help me on this one please. I'm trying to count how many are there in my table as it doesn't show up the result on my view.
Controller
public function enrollment_summary()
{
$data['title'] = 'Enrollment Summary';
$this->load->model('report_model');
$data['query'] = $this->report_model->get_students();
$this->load->view('report_enrollment_summary', $data);
}
Model
<?php
class report_model extends CI_Model
{
public function __construct()
{
parent::__construct();
$this->load->database();
}
public function get_students()
{
$query = $this->db->count_all('students');
}
}
View
<body>
<h1>Enrollment Summary</h1>
<?php echo $query; ?>
</body>
Your get_students() is not returning anything?
So you need to add in a return.
Your current method...
public function get_students()
{
$query = $this->db->count_all('students');
}
becomes
public function get_students()
{
$query = $this->db->count_all('students');
return $query;
}
OR
public function get_students()
{
return $this->db->count_all('students');
}
I'm new in codeigniter, I'm having a challenge with num_rows on view page
Here's the sample of the code - Updated
Model
public function __construct()
{
parent::__construct();
}
public function total_referred_in()
{
$query = $this->db->query('SELECT * FROM daily_out_patient');
return $query->num_rows();
}
Controller
public function __construct()
{
parent::__construct();
$this->load->helper('url');
$this->load->model('Referrals_form_model', 'referral'); /* LOADING MODEL * Referral_form_model as referral */
}
public function index()
{
$this->load->helper('url');
$this->load->view('referrals_form_view');
}
public function referral_in()
{
$data['total_referred_in'] = $this->load->referral->total_referred_in(); //get referral data
$this->load->view("referral_form_view", $data);
}
}
View
<span><?php
echo $query->num_rows();
?></span>
when i run the code, it tell me undefined variable "query"
Please help.
Thank you
Please change your code as below.
MODEL:
public function total_referred_in()
{
$query = $this->db->query('SELECT * FROM daily_out_patient');
return $query->num_rows();
}
Controller
public function referral_in()
{
$data['total_referred_in'] = $this->load->referral->total_referred_in(); //get referral data
$this->load->view("referral_form_view", $data);
}
View :
<span>
<?php
echo $total_referred_in;
?>
</span>
As you can see in Controller line :
$this->load->view("referral_form_view", $data);
$data here is the data array you are passing to your view. In your view you can access data in $data variable by using $data variable's KEY as php variable(In our case its "total_referred_in").
You aren't returning anything from your function, you're just echoing. Try this:
public function total_referred_in()
{
$query = $this->db->query('SELECT * FROM daily_out_patient');
return $query->num_rows();
}
Controller:
public function referral_in()
{
$data['num_rows'] = $this->load->referral->total_referred_in(); //get referral data
$this->load->view("referral_form_view", $data);
}
And then in your view:
<span><?=$num_rows?></span>
I have table in my database named settings
and it have 3 Fields id, key, value
and i need to update all settings in one page using one query
this is my settings model
class Settings_model extends CI_Model{
/*
* Get global settings
*/
public function get_global_settings() {
$query = $this->db->get('settings');
$result = $query->result();
return $result;
}
/* Update setting */
public function update($data, $id) {
$this->db->where('id', $id);
$this->db->update('settings', $data);
return true;
}
}
and this is my controller
class Settings extends MY_Controller{
public function __construct() {
parent::__construct();
//Access control
if(!$this->session->userdata('logged_in')) {
redirect('admin/login');
}
}
public function index(){
// Get Categories
$data['settings'] = $this->Settings_model->get_global_settings();
// Load View
$data['main_content'] = 'admin/settings/index';
$this->load->view('admin/layouts/main',$data);
}
public function edit($id) {
// Calidation Rules
$this->form_validation->set_rules('value','Name','trim|required|xss_clean');
$data['settings'] = $this->Settings_model->get_group($id);
if($this->form_validation->run() == FALSE) {
// View
$data['main_content'] = 'admin/settings/edit';
$this->load->view('admin/layouts/main',$data);
} else {
// Create Articles Data Array
$data = array (
'value' => $this->input->post('value')
);
// Articles Table Insert
$this->Settings_model->update($data, $id);
// Create message
$this->session->set_flashdata('setting_saved', 'Your setting has been saved');
// Redirect page
redirect('admin/settings');
}
}
}
but it only update one row by id
i need to make a foreach loop to update all rows
I am struggling with flash data in codeigniter. My edit function is working properly. But problem is with delete function.My controller is as follows
public function rmRole()
{
$data = array();
$roleId = $this->uri->segment(4,0);
if ($this->_delete($roleId)) {
$this->session->set_flashdata('flash_message' ,'data_deleted');
redirect('adminroles');
}
}
My model includes
public function _delete($id) {
$table = $this->get_table();
$this->db->where('id', $id);
$this->db->delete($table);
}
My data is got deleted. But it is redirecting to a blank page with the url. What shall i do to display a flash data like data deleted successfully and redirect to adminroles module?
try this in model
function delete($id) {
if ($id)
{
if ($this->db->where("id", $id)->delete("table name")){
return TRUE;
}
else{
return FALSE;
}
}else{
return FALSE;
}
}
model
This is my model.using last query I get value in the model,but I don't get the value in the controller
class Login_model extends CI_Model {
function __construct()
{
parent::__construct();
}
public function email()
{
$this->db->select('email');
$this->db->from('change_password');
$result=$this->db->get();
return $result;
}
}
controller
class Login extends CI_Controller {
function __construct()
{
parent::__construct();
}
function checking()
{
$email=$this->input->post('email');
$this->load->model('login_model');
$dbemail=$this->login_model->email();
echo $dbemail;
}
}
CI is a MVC Framework. So you need to Command from Controller and get data from the models and finely you need to pass them to view. This is a best Practice
Controller
function checking()
{
$email=$this->input->post('email');
$this->load->model('login_model');
$data['dbemail']=$this->login_model->email();// assign your value to CI variable
$this->load->view('home', $data); //passing your value to view
}
Model
public function email()
{
$query = $this->db->query("SELECT email FROM change_password");
$result = $query->result_array();
return $result;
}
View
Extra Knowledge
View will be create under View folder/ Ex im using view name
as home.php
you can use your own stylings(its also created as normal html page.)
foreach ( $dbemail as $new_dbemail )
{
echo $new_dbemail['database_field'];//in here you can get your table header.
//Ex if your table has name field and you need to sho it you can use $new_dbemail['name']
}