Im having an issue using CI4
I kept on getting error "Call to a member function get_courses() on null" even though this function exist on my model. Here's how I code it.
courseModel.php
public function get_courses() //get courses under the teacher
{
$builder = $this->db->table('tbl_course_access');
$builder->select('*');
// $builder->where('user_id', $userid);
// $builder->where('school_id', $schoolid);
//$builder->groupBy('course_id');
$query = $builder->get();
$results = $query->getResult();
return $results;
}
and on my controller I call it like this
Dashboard.php
$temp = $this->courseModel->get_courses();
var_dump($temp);
exit;
note that I called the model properly base on the CI manual
$this->courseModel = model('App\Models\CoursetModel', false);
What did I do wrong or any configuration I need to do on CI4?
My bad, I had typos on my codes that makes the error! Must be a headache doing all the work!
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 do not want to be asking a question that has already been answered, however I have done a ton of research and I am stuck. Any help would be appreciated.
I am trying to query and add database results to OpenCart's addproduct.tpl
In the MODEL file I have:
public function units() { //function that gets database info and returns it
$unit_variables = $this->db->query("SELECT unit FROM ". DB_PREFIX ."
weight_class_description");
if (!$unit_variables) {
die(mysql_error());
}
else {
foreach ($unit_variables->rows as $temp_var) {
print_r($temp_var['unit'], true);
}
}
}
In the CONTROLLER file I have:
$this->load->model('catalog/product'); //where my function is located
$this->model_catalog_product->units();
$weight_variables = units();
if (isset($this->request->post['weight_variables'])){
$this->data['weight_variables'] = $this->request->post['weight_variables'];
}
In the VIEW I have:
<?php echo $weight_variables ?>
I get the following error:
Call to undefined function units() in /path/to/controller/file on line etc.
Note: When I print_r($temp_var); instead of returning print_R($temp_var, true) and delete these lines of code $weight_variables = units(); if (isset($this->request->post['weight_variables'])){ $this->data['weight_variables'] = $this->request->post['weight_variables'] } in the controller file my model file will display the query results on the addproduct.tpl
units() is a METHOD of your object, yet you're calling it as a standalone regular function:
$weight_variables = units();
^^^^^^^
Shouldn't it be:
$weight_variables = $this->model_catalog_product->units();
instead? And note that as-written, your method doesn't actually return anything, so $weight_variables will simply get assigned null..
I am trying to display jobs won by a certain provider. What I did was to create function get_approved_job_proposals in my model. Then, I created function manage_job_contracts in my controller, but I never got to successfully run it.
Here's my code in model:
public function get_approved_job_proposals($status)
{
$this->db->select('*')->from('job_proposal')->where('status', $status);
$this->db->where("status" == "Awarded");
$query = $this->db->get();
return $query->result_array();
}
and this is what I have in my controller:
public function manage_job_contracts()
{
$this->validateRole('client');
$this->load->model('job_model');
$data['my_preference'] = $this->job_model->get_approved_job_proposals($status);
$data['job'] = $this->job_model->get_job($id);
$this->load->view('client/manage_job_contracts', $data);
}
Kindly help me fix this issue.
This is wrong:
$this->db->where("status" == "Awarded");
Correct:
$this->db->where("status", "Awarded");
Check documentation: http://ellislab.com/codeigniter/user-guide/database/active_record.html
my controller is not passing $data to my view and I don't know why not. I'm reusing code from a previous project which worked fine and I certainly understand the idea of how $data passing is meant to work. But maybe I missed something when copying code over?
I put in the variable $data['hello'] in there just for testing purposes. As you can see from the output $hello isn't even getting through. The if fails and the else code is run correctly which means the view file itself is being loaded.
Controller:
function users() {
$data['title'] = 'users';
$data['users'] = $this->main_m->get_users();
$data['hello'] = 5;
$this->load->view('users', $data);
}
View:
<?php
echo $hello;
if ($users->num_rows != 0) {
foreach ($users->result() as $user) {
}
} else {
echo "No users.";
}
Output (abridged):
A PHP Error was encountered
Message: Undefined variable: hello
Line Number: 2
A PHP Error was encountered
Message: Undefined variable: users
Line Number: 3
A PHP Error was encountered
Message: Trying to get property of non-object
Line Number: 3
No users.
Edit: more info on request:
Model:
public function get_users($amount = 0, $offset = 0) {
$this->db->from('users');
$this->db->order_by('l_name', 'desc');
if ($amount != 0)
$this->db->limit($amount, $offset);
return $this->db->get();
}
I always do like this
change your model to
$query = $this->db->get();
return $query->result();
And in view
if (count($users)> 0) {
foreach ($users as $user) {
echo $user['name'];
}
} else {
echo "No users.";
}
Hope this helps
Regards
iijb
Just write $data = array(); before you are sending some data into $data array.
function users() {
$data = array();
$data['title'] = 'users';
$data['users'] = $this->main_m->get_users();
$data['hello'] = 5;
$this->load->view('users', $data);
}
I think you could solve your issue with some simple var_dump() checks.
Check what's coming out of your model by var_dump()ing $data['users'] - is this an object? What happens when you var_dump() $data['users']->result()?
Then, var_dump() $data in your view - does it have all the pieces?
Thing is, even showing us your model function doesn't prove that your getting a real data result. Check that. Your code looks okay at a glance so I don't think that is where the issue exists.
There is something very basic that is wrong. So get out of that controller and do a sanity check. First confirm that your welcome view is working. If it is go to the welcome controller and put this in the index method
$data['here'] = 'we are here' ;
$this->load->view('welcome_message', $data);
and then somewhere in the welcome.php view file
<?php echo $here ?>
You do not need to set this: $data = array();
However some people suggest it because that way even if you dont create any data variables you wont get an error if its in the view call $this->load->view('welcome_message', $data);
finally i would suggest looking at this
function users() {
$data['title'] = 'users';
$data['users'] = $this->main_m->get_users();
$data['hello'] = 5;
$this->load->view('users', $data);
}
lets see you have method called users, returning an object called users, and a view called users -- that could get confusing ! :-)
I have two functions getCompanyDetails and getHostingDetails
The first database getCompanyDetails works fine but the getHostingDetails shows
Trying to get property of non-object
getCompanyDetails:
Controller: $data['companyName'] = $this->quote->getCompanyDetails()->companyName;
Model:
public function getCompanyDetails()
{
$this->db->select('companyName,companySlogan,companyContact,
companyEmail,companyWebsite,companyPhone,
companyFax,companyAddress');
$this->db->from('companyDetails');
$result = $this->db->get();
if($result->num_rows()<1)
{
return FALSE;
}else{
return $result->row();
}
}
getHostingDetails:
Controller:
$data['hostingRequired'] = $this->quote->getHostingDetails()->hostingRequired;
Model:
public function getHostingDetails()
{
$this->db->select('hostingRequired,domainRequired,domainToBeReged,
domaintoBeReged0,domainTransfer,domainToBeTransfered,
domainToBeTransfered0,currentHosting');
$this->db->from('hostingDetails');
$result = $this->db->get();
if($result->num_rows()<1)
{
return FALSE;
}else{
return $result->row();
}
}
Well, one method returns an object from $result->row() and the other false. You can't call a method on false.
false is returned when no record is found. So you need to check the return value before using it.
Well in your get functions chances is your code might return you false if there is no rows returned. You might want to check before retrieving the details. Example:
$details = $this->quote->getHostingDetails();
if($details){
$data['hostingRequired'] = $details->hostingRequired;
}
The problem is probably how you use those functions in your controller. If any of them returns FALSE, then
$this->quote->getHostingDetails()->hostingRequired;
is going to give you errors. Try
if ($row = $this->quote->getHostingDetails()) {
echo $row->hostingRequired;
}