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
}
Related
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.
I am fetching data from mysql database table in codeigniter using following code
$result = $this->db->get("shipping");
$data = $result->result_array();
But returns no data.
When I apply the limit as
$this->db->get("shipping",1,30)
The code works.
But I want to fetch all result and not by limiting it.
Could anyone please let me know how do I resolve this issue.
Thanks in advance.
have you tried using query rather than get?
$sql = "
SELECT
*
FROM
shipping
";
$query = $this->db->query($sql);
$data = $query->result();
You have not put a return
https://www.codeigniter.com/user_guide/database/results.html
// This will get every result from shipping
public function somefunction() {
$result = $this->db->get("shipping");
return $result->result_array();
}
Controller function
public function somecontrollerfunction() {
$this->load->model('some_model');
$data['shipping'] = array();
$data['shipping'] = $this->some_model->somefunction();
// var_dump($data['shipping']);
$this->load->view('your_view', $data);
}
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;
}
sorry for the confusing title as I really did not know what to type. I'm new to CI and now I'm trying to convert my code to CI and stuck here.
here is my original code:
$query_domain = $konek->prepare("SELECT * FROM `domain` WHERE `id` = :id");
$query_domain->bindParam(":id", $id);
$query_domain->execute();
$data_domain = $query_domain->fetch();
$query_owner = $konek->query("SELECT * FROM `people` WHERE `id` = $data_domain->ownerid");
$data_owner = $query_owner->fetch();
So basically it request a domain where the domain id is X
and then it request data of the owner based on an owner id that is in domain table.
I'm not really sure what to put in controller or model
but here is my current model:
public function get_domain($id){
$this->db->get_where('domain', array('id' => $id));
}
public function get_domain_owner($ownerid){
$this->db->get_where('client', array('id' => $ownerid));
}
In your (Client) model:
public function get_owner_with_domain($domain)
{
$query = $this->db->select('c.*')->
from('client c')->
join('domain d', 'd.ownerid = c.id')->
where('d.name',$domain)->get();
if ($query) {
return $query->row_array();
// Or, ideally return a client if you have a Client model
// return $query->row(0,'Client');
} else {
// log error?
return false;
}
}
In your controller:
public function client($domain) // or whatever your function might be called
{
$this->load->model('Client');
$client = $this->Client->get_owner_with_domain($domain)
// Do something with the client
// var_dump($client);
$view_data['client'] = $client;
$this->load->view('client_info',$view_data);
}
Further reading:
CodeIgniter ActiveRecord reference
CodeIgniter Results reference
You can do simply by using a single query
SELECT people.* FROM people, domain WHERE people.id = domain.ownerId AND domain.id = :id
I have tried a few different ways to make this work to no avail.
Basically, I have these 4 lines that are in many functions:
function getID($URL){
//These 3 plus this comment line
$parentQ = "select * from cdi_content where URL=\"$URL\"";
$parentResult = mysql_query($parentQ); // Run the Query
$link = mysql_fetch_assoc($parentResult); // Query Result
...continues...
That basically tells the database to check $URL and if it matches the URL string in the database, it grabs all of the data associated with that URL, and I grab what I need with
$link['ID'];
Which would give me the ID associated with the $URL URL.
The function checks a list of conditionals that either print the 'override' variable ($ID), the default variable ($defaultID) or pulls from the server ($link['ID']), like below. The 'override' is a variable outside of the function that is currently called in by global $ID.
$ID = ''; //Overrides if set
function parseData($URL){
//Initialize Query for Table Data
$parentQ = "select * from cdi_content where URL=\"$URL\"";
$parentResult = mysql_query($parentQ); // Run the Query
return mysql_fetch_assoc($parentResult); // Query Result
};
function getID($URL){
global $ID;
$serverID = parseData($URL);
if(empty($ID)){
if(empty($serverID)){
echo $defaultID;
} else { echo $serverID['ID']; }
}//end of ifs
else
{ echo $ID; }
};
So you you want to parse the data row you would need search criteria $criterion and a field you you want it to return:
function getField($criterion, $returnField){
$parentQ = "select * from cdi_content where URL='".$criterion."' LIMIT 1"; // also make sure you that $criterion is safe to use in a query
$parentResult = mysql_query($parentQ); // Run the Query
if ($parentResult)
{
$row = mysql_fetch_assoc($parentResult);
return $row[$returnField];
}
else
{
return FALSE;
}
}
Now you can call it:
$linkId = getField($url, 'ID');
If I'm understanding you correctly, you're just trying to refactor that function so you don't have to keep on re-writing it. Is that correct?
If so, you can extract a new function like this:
function getLink($URL){
$parentQ = "select * from cdi_content where URL=\"$URL\"";
$parentResult = mysql_query($parentQ); // Run the Query
return mysql_fetch_assoc($parentResult);
}
And then call it like this:
function getID($URL){
$link = getLink($URL);
}