I am getting an error in the bellow code and I am unable to find out what is the reason for it. I am glad if you can help me with this.
Public function () {
$query = $this->db->select_sum("cost_after_discount");
$query = $this->db->get('tbl_payment');
$data = $query;
return $data;
}
Object of Class could not be converted to string CI Error
There are several errors in your code. First you have to provide a name for your function then you do not use any result method to return result. So try following
Public function xyz() {
$this->db->select_sum("cost_after_discount");
$query = $this->db->get('tbl_payment');
$data = $query->result_array();
return $data;
}
now you may call this xyz function to retrieve data
Try this
public function retrive()
{
$this->db->select_sum("cost_after_discount");
$query = $this->db->get('tbl_payment');
$data = $query->result_array();
var_dump($data);
}
This should do :)
Related
I get this error form helper.php. I need sale prices of product. but I get this error.And I dont understand when I get this error.
helper.php
public static function getSalesPriceUsingProductIDCode($id, $customerId)
{
$data = PosCustomerProducts::valid()->where('complete_status',0)->where('customer_id', $customerId)->where('product_id', $id)
->select('product_id', 'sales_price')
->first()->sales_price;
return $data;
}
orderSheetExport.php
if(isset($results[$product->id]))
{
if (Helper::getSalesPriceUsingProductIDCode($product->id,$results['customer_id'])==0)
{
$excel_dynamic_data_values[$index][] = $product->whole_sale_price * $results[$product->id];
$productArray[$rsm_id][$product->id] += $results[$product->id] * $product->whole_sale_price;
$singleProductArray[$rsm_id][$product->id] = $productArray[$rsm_id][$product->id];
}else
{
$excel_dynamic_data_values[$index][] = Helper::getSalesPriceUsingProductIDCode($product->id,$results['customer_id']) * $results[$product->id];
$productArray[$rsm_id][$product->id] += $results[$product->id] * Helper::getSalesPriceUsingProductIDCode( $product->id,$results['customer_id']);
$singleProductArray[$rsm_id][$product->id] = $productArray[$rsm_id][$product->id];
}
}
If you just need the sales_price use value function
public static function getSalesPriceUsingProductIDCode($id, $customerId)
{
$data = PosCustomerProducts::valid()->where('complete_status',0)->where('customer_id', $customerId)->where('product_id', $id)
->select('product_id', 'sales_price')
->value('sales_price');
return $data;
}
Kindly refer value() in Laravel Queries
I solve this problem. I also understand Why I was getting this error. When I use this helper function on a loop. It get some values that were not any sale price data. So it though this error.
So, I write if else condition on it.
Helper.php
public static function getSalesPriceUsingProductIDCode($id, $customerId)
{
$data = PosCustomerProducts::valid()->where('complete_status',0)->where('customer_id', $customerId)->where('product_id', $id)
->select('product_id', 'sales_price')
->first();
if ($data) {
$data = $data->sales_price;
} else {
$data = 0;
}
return $data;
}
My program is not working properly, i do not know what should i do :S
I got this error message:
Take a look at this:
Here is my code:
My controller file (Home):
<?php
class Home extends CI_Controller{
public function __construct(){
parent::__construct();
$this->load->model("Crudmodel");
}
public function index(){
# get all data in Study table
$selectStudys = $this->Crudmodel->selectStudys();
foreach ($selectStudys as $key => $study)
{
# get UserNames
$user = $this->Crudmodel->getName($study['user_id']);
#get Subject Names
$subject = $this->Crudmodel->getSubName($study['subject_id']);
#append both NEW VALUES to same array
if(!empty($user[0]['username'])){
$data[$key]['user_id'] = $user[0]['username'];
// your main problem can be this. may be it is not getting value from query this is why we have put validation on model function and error handler condition here
}else{
$data[$key]['user_id'] = ''; // or anything as your else condition you can use as error handler
}
if(!empty($subject[0]['name'])){
$data[$key]['subject_id'] = $subject[0]['name'];
// your main problem can be this. may be it is not getting value from query this is why we have put validation on model function and error handler condition here
}else{
$data[$key]["subject_id"] = "";
// or anything you can use as error handler
}
}
$data['records'] = $selectStudys;
$this->load->view('home', $data);
}
}
?>
Crudmodel:
class Crudmodel extends CI_Model{
public function __construct(){
parent::__construct();
$this->load->database();
}
function selectStudys()
{
$query= $this->db->query("SELECT * FROM cursadas");
if($query->num_rows()>0){
$result = $query->result_array();
}else{
$result = "";
// or anything you can use as error handler
return $result;
}
}
function getName($name)
{
$query= $this->db->query("SELECT username FROM usuarios WHERE id = $name ");
if($query->num_rows()>0){
$result = $query->result_array();
}else{
$result = "";
// or anything you can use as error handler
return $result;
}
}
Dont know what to do now :(
Hope you can help me :S
The problem is in the model. You only return something inside the else. Easy fix, move the return.
You should probably return an empty array if there are no rows. Then the foreach will still have something to work with - even if it is empty. foreach will choke if given something that cannot be used in a loop - a string for instance.
function selectStudys()
{
$query= $this->db->query("SELECT * FROM cursadas");
if($query->num_rows()>0){
$result = $query->result_array();
}else{
$result = array();
}
return $result;
}
I got an error
"Cannot use object of type QueryResult as array"
I am simply trying to take state name from the records array but, I only manage to get this error "Cannot use object of type QueryResult as array"
Model.php
public function getstatename()
{
$statename = $this->salesforce->query ("SELECT State__c FROM RI_School_List__c group by State__c");
return $statename;
}
Controller.php
public function index() {
$data['getstatename'] = $this->model->getstatename();
$this->load->view('footer', $data);
echo "<pre>";
print_r($data['getstatename']['records']);
}
Actually $statename is result-set object not an array.
So you need to do like below:-
Model.php:-
public function getstatename(){
$statename = $this->salesforce->query ("SELECT State__c FROM RI_School_List__c group by State__c");
$state_array = array(); // create an array
foreach ($statename as $row){
$state_array[] = $row->State__c; // assign value to array
}
return $state_array; // return array
}
Controller.php:-
public function index() {
$data['getstatename'] = $this->Footer_model->getstatename();
echo "<pre/>";print_r($data['getstatename']); // check this and accordingly change you code further
$this->load->view('footer', $data);
}
Here is the solved code Thanks to #Anant
Model.php
public function getstatename()
{
$statename = $this->salesforce->query ("SELECT State__c FROM RI_School_List__c group by State__c");
$state_array = array();
foreach ($statename as $row){
$state_array[] = $row->State__c;
}
return $state_array;
}
Controller.php
public function index() {
$data['getstatename'] = $this->Footer_model->getstatename();
$this->load->view('footer', $data);
}
See if you can do
$data['getstatename']->fetchRow();
here is my code :
function getValue($id)
{
$this->db->select('value');
$this->db->where('id',$id);
$q = $this->db->get('ref_table');
$data = array_shift($q->result_array());
$result = $data['value'];
return $result;
}
when executed emerge error array conversion to string
Error gives you the answer Array conversion to string
Try this
In model
public function getValue($id)
{
$this->db->select('value');
$this->db->where('id',$id);
$q = $this->db->get('ref_table');
$result = $q->result_array();
return $result;
}
In Controller
$data['value'] = $this->Model_name->getValue($id);
$this->load->view('my_view',$data)
This is the controller search.php
public function login()
{
$email=$this->input->post('email');
$password=md5($this->input->post('password'));
$result=$this->search_model->login($email,$password);
if(!$result) {
$this->index();
}
else {
$this->session->set_userdata('user_id',$email);
$seid=$this->session->userdata('user_id');
$data['result'] =$this->search_model->autocomplete();
$this->load->view('pagination_view',array('result' => $result));
}
}
===========================Model=========================================
public function autocomplete($like) {
$this->db->select('name');
$this->db->from('tbl_reg');
$this->db->like('name', $like);
// $this->db->where('status', ACTIVE);
$q = $this->db->get();
if ($q->num_rows() > 0) {
return $q->result();
}
}
in controller search.php what argument is added to the autocomplete(). the code is
$data['result'] = $this->search_model->autocomplete();
I got an error like this Severity: Warning
Message: Missing argument 1 for search_model::autocomplete(),
and another error message like
Undefined variable: like
Whats the solution for this problem?
You need to pass argument in your model function
$this->search_model->autocomplete($YOUR_LIKE_VARIABLE);// pass your like variable here
Because in model file you use
public function autocomplete($like) {
you are supposed to pass the like variable as argument to your model function
In your controllers
$like = "matching word"; // replace this with your value;
$data['result'] =$this->search_model->autocomplete($like);
If you not passing $like variable then How your model will search.
$like='any text' // this would be your keyword for which you are doing auto complete process, Like any name or anything.
$data['result'] = $this->search_model->autocomplete($like);