How to get users list in codeigniter? - php

I am trying to get list of users from Users table in codeigniter. My problem is that if am using following code than try to print out the result, it's giving me blank array while printing query using last_query() function and running this query in phpmyadmin is giving correct result.
My code in model
$data = $this->db->select('u.*')
->from('users u')
->join('users_groups ug','ug.user_id = u.id')
->join('groups g','g.id = ug.group_id')
->where('g.id',7)
->get()
->result();
this code is giving me blank array then what i did is print the query using following code
echo $this->db->last_query();
and this is what query was
SELECT `u`.* FROM `users` `u` JOIN `users_groups` `ug` ON `ug`.`user_id` = `u`.`id` JOIN `groups` `g` ON `g`.`id` = `ug`.`group_id` WHERE `g`.`id` = 7
which is working fine. but i am trying to print the data using print_r($data)
is giving me blank array.
Can anyone tell me what can be the issue ?
edited
My full model code.
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Technician_model extends CI_Model {
public function _consruct(){
parent::_construct();
}
function getData(){
$data = $this->db->select('u.*')
->from('users u')
->join('users_groups ug','ug.user_id = u.id')
->join('groups g','g.id = ug.group_id')
->where('g.id',7)
->get()
->result();
return $data;
}
function getRecord($user_id){
$record = $this->db->get_where('users',array('id'=>$user_id))->row();
echo '<pre>';
print_r($record);
exit;
return $record;
}
function addModel($data){
if($this->db->insert('users',$data)){
return $this->db->insert_id();
}
else{
return FALSE;
}
}
function editModel($data,$user_id){
$this->db->where('id',$user_id);
if($this->db->update('users',$data)){
return true;
}
else{
return false;
}
}
function deleteModel($user_id){
$this->db->where('id', $user_id);
if($this->db->delete('users')){
return true;
}
else{
return FALSE;
}
}
}
?>

You may try this simple way to join table. you may try this and can
change fields name table name and method according to your
require.
public function getUser() {
$this->db->select('users');
$this->db->from('Users');
$this->db->where('users_groups.user_id',1);
$this->db->join('users_groups', 'users.id = users_groups.user_id');
$query = $this->db->get();
$result = $query->result_array();
var_dump($result); // display data in array();
return $result;
}

Actually, at the end of 2 hrs of the journey, I found that I forgot to configure new database.
All the answers were correct in this case.

Related

i want to display value from database in codeigniter but i am getting errror

I want to display a value in view in CodeIgniter but I am getting several errors like trying to get the property on non-object. I think my code is correct but I am getting errors. Below is my code.
controller:
public function trainer($id)
{
$user_record = $this->db->query("select * from usr_data where usr_id=$id")->result();
$data['title'] = 'trainer Dashboard';
$data['user_record'] = null;
$data['active_courses'] = [];
$data['inprogress'] = [];
if(count($user_record)) {
$user_record = $user_record[0];
$data['title'] = ucwords($user_record->firstname).' Dashboard';
$data['user_record'] = $user_record;
$active_courses = $this->base_model->getTrainercourseAll($id);
$data['active_courses'] = $active_courses;
$inprogress = $this->base_model->getstaffinprogress($id);
$data['inprogress'] = $inprogress;
}
$this->load->view('trainer-dashboard', $data);
}
model:
public function getstaffinprogress($user_id) {
$result=$this->executeSelectQuery("select AVG(m.percentage) from object_data o, ut_lp_marks m where o.obj_id=m.obj_id and o.type='crs' and m.status=1 ");
return $result;
}
view:
<h3>Avg inprogress:<?php echo "<span style='color:#ff00ff;font-family:verdana;'>".$inprogress->percentage."</span>";?></h3>
I want to display the column percentage which is coming from database.above code is in the controller, model and view.i thought my controller code is wrong.
Anyone help me to get rid of this error. I want to display a value in view in CodeIgniter but I am getting several errors like trying to get the property on non-object. I think my code is correct but I am getting errors. Below is my code.
Try this in your view file,
if(isset($inprogress)){
echo $inprogress->percentage;
}
Then your code look like this,
<h3>Avg inprogress:<?php if(isset($inprogress)){ echo "<span style='color:#ff00ff;font-family:verdana;'>".$inprogress->percentage."</span>";}?></h3>
Then call the controller function. I think inprogress is not set at the first time.
If it doesn't work, try to var_dump($inprogress) in controller and check value and type.
And try this code in your model. Query also seems not correct
public function getstaffinprogress($user_id) {
$this->db->select_avg('ut_lp_marks.percentage');
$this->db->where('ut_lp_marks.obj_id', $user_id);
$this->db->where('object_data.type', 'crs');
$this->db->where('ut_lp_marks.status', 1);
$this->db->join('object_data', 'object_data.obj_id = ut_lp_marks.obj_id');
$query = $this->db->get('ut_lp_marks');
return $query->result_array();
}
I assume that your db is ut_lp_marks. Then var_dump array and check data is correct first. Then access array element.
public function getstaffinprogress($user_id) {
$result = array();
$query=$this->db->query("select AVG(m.percentage) from object_data o, ut_lp_marks m where o.obj_id=m.obj_id and o.type='crs' and m.status=1 ");
foreach($query->result() as $row){
$result = $row;
}
return $result;
}
Also check $inprogress->percentage exists before print in view.

Codeigniter not getting data though available in mysql database

I have a model which fetch the data from database is below
public function counselor() {
$inst_id = $this->session->userdata('user_id');
$submission_key=$this->session->userdata('submission_key');
$query = $this->db->query("SELECT * FROM counselor where USER_ID = $inst_id AND submission_key= $submission_key");
$data = $query->num_rows();
if ($data > 0) {
return $data;
} else {
return false;
}
}
I have tested the $inst_id and $submission_key by printing it and its set.
$inst_id=2 and $submission_key=2016-8 .BUT though I have one record in database with those two field set its not returning the data. What is the case. I have tried with codeigniter get() and where() method too. Still not giving me the result.
Just write your query using active record function. It will help you in escaping string
$this->db->select('*',FALSE);
$this->db->where('USER_ID',$inst_id);
$this->db->where('submission_key',$submission_key);
$query=$this->db->get('counselor');
$data = $query->num_rows();
if ($data > 0) {
return $data;
} else {
return false;
}

Cannot get data from mysql query to controller

Hello I am using codeigniter framework for a project, I have a controller which is calling the data from a model function. Here is the controller.
public function getThirdPartyRR($token){
if ($this->input->is_ajax_request()){
// $data = json_decode(file_get_contents('php://input'), true);
// Following is loaded automatically in the constructor.
//$this->load->model('user_profile');
$userid = $this->myajax->getUserByAuth($token);
if ($userid){
$this->load->model("riskrating_page");
/* If we have an impersonated user in the session, let's use him/her. */
if (isset($_SESSION['userImpersonated'])) {
if ($_SESSION['userImpersonated'] > 0) {
$userid = $_SESSION['userImpersonated'];
}
}
// $resultList value could be null also.
$result = $this->riskrating_page->getThirdPartydata($userid);
/* Little bit of magic :). */
$thirdpartylist = json_decode(json_encode($result), true);
$this->output->set_content_type('application/json');
$this->output->set_output(json_encode($thirdpartylist));
} else {
return $this->output->set_status_header('401', 'Could not identify the user!');
}
} else {
return $this->output->set_status_header('400', 'Request not understood as an Ajax request!');
}
}
And here is the query function in the model where I get the data from.
function getThirdPartydata($id){
$query = 'SELECT b.text_value as Company, a.third_party_rr_value
FROM user_thirdparty_rr a
inner join text_param_values b
on a.third_party_rr_type = b.text_code and
b.for_object = \'user_thirdparty_rr\'
WHERE a.Owner = '.$id. ' and
a.UPDATE_DT is null;';
}
But when I debug it using netbeans, its shows that in my controller in the $result function I get null meaning I couldnt grab any data from mysql.
Here is the search result from mysql.
You only write your query not fetch any data from your query result
function getThirdPartydata($id){
$query = "SELECT b.text_value as Company, a.third_party_rr_value
FROM user_thirdparty_rr a
inner join text_param_values b
on a.third_party_rr_type = b.text_code and
b.for_object = 'user_thirdparty_rr'
WHERE a.Owner = '$id' and
a.UPDATE_DT is null";
$this->db->query($query);// execute your query
return $query->result_array();// fetch data
}

Getting an empty array in codeigniter project

In the code below,I'm getting an empty array for the third if-else statement. What I'm trying is that get a range of date from user and showing the data from the table accordingly. My first if-else statement is returning desired results but the second one is returning empty array.
Controller:
public function bookings()
{
if($this->userlib->isLoggedIn())
{
if($this->userlib->isAdmin())
{
$this->load->view('admin_search_booking');
$date_posted_from = $this->input->post('date_posted_from');
$date_posted_till = $this->input->post('date_posted_till');
$date_posted_on = $this->input->post('date_posted_on');
if(is_null($date_posted_from) && is_null($date_posted_till) && is_null($date_posted_on))
{
$total_trails = $this->admin_panel_model->total_trail();
var_dump($total_trails);
}
elseif(!is_null($date_posted_on))
{
$total_trails = $this->admin_panel_model->filter_on_date($date_posted_on);
var_dump($total_trails);
}
elseif(!is_null($date_posted_from) && !is_null($date_posted_till))
{
$total_trails = $this->admin_panel_model->filter_by_date($date_posted_from, $date_posted_till);
var_dump($total_trails);
}
}
}
else
{
echo "User not Logged In";
}
}
Model filter_by_date :-
public function filter_by_date($date_posted_from, $date_posted_till)
{
$query = $this->db->select('*')
->where('date >=', $date_posted_from)
->where('date <=', $date_posted_till)
->get($this->search);
return $query->result();
}
isLoggedIn :-
public function isLoggedIn()
{
if($this->ci->session->email)
return true;
else
return false;
}
Change your model function like this
public function filter_by_date($date_posted_from="", $date_posted_till="")
{
$query = $this->db->select('*');
if($date_posted_from)
$this->db->where('date >=', $date_posted_from);
if($date_posted_till)
$this->db->where('date <=', $date_posted_till);
$this->db->get($this->search);
return $query->result();
}
//This may help you put your table name in your select statement and update where clause
public function filter_by_date($date_posted_from, $date_posted_till){
$date_posted_from='2015-10-10';
$date_posted_till='2015-10-16';
$query = $this->db->select('*')->from('Your_Table_Name')
->where("date >= '".$date_posted_from."'")
->where("date <= '".$date_posted_till."'")
->get($this->search);
return $query->result();
}
Use $this->db->last_query(); after filter_by_date() and check what query is actually being executed. And also make sure there are records exists in the date range you are searching for.
Or simply use straight forward query,
$Q = $this->db->query("select * from table_name where date >='".$date_posted_from."' and date <='".$date_posted_to."'");
return $Q->result_array();

Join does not work

I have write a function using SQL join but i do not know why it does not work
this is a model
public function get_contract_user($user_id)
{
$this->db->select('contracts.*');
$this->db->from('contracts');
$this->db->join('link', 'contracts.contract_id = link.contracts_contract_id');
$this->db->where('link.users_user_id', $user_id);
$query = $this->db->get();
return $query->result();
}
this is the app
$data['query'] = $this->admin_model->get_contract_user($contract_id);
this is a view
foreach($query as $row)
{
echo $row->contract_code;
echo $row->contract_num;
echo $row->contract_start;
echo $row->contract_end;
}
You defined method get_contract_user, but you are using get_contract in provided code.
You should be getting an error concerning undefined function/method.

Categories