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)
Related
I have two controller functions both need one model function but when I pass parameters to single model function it shows me an error, I need help if there is another method to pass parameters
Controller
public function view_files()
{
$assignment_id='1255';
$data['files'] = $this->AdminModel->getRows111();
$this->load->view('admin/view_files', $data);
}
function download_test1(){
$id = $this->uri->segment(3);
if (empty($id)){
redirect(base_url());
}
$data = $this->AdminModel->getRows111($id);
$filename =$data['assignment_file_name'];
$fileContents = file_get_contents(base_url('upload/'.
$data['assignment_file_name']));
force_download($filename, $fileContents);
}
Model
public function getRows111($id=''){
$this->db->select('*');
$this->db->where('assignment_id','1255');
$this->db->from('tbl_assignments_files');
if($id){
$this->db->where('assignment_file_id',$id);
$query = $this->db->get();
$result = $query->row_array();
}else{
$query = $this->db->get();
$result = $query->result_array();
}
return !empty($result)?$result:false;
}
}
function 1
$data['files'] = $this->AdminModel->getRows111(assignment_id);
function 2
$data = $this->AdminModel->getRows111($id);
model by doing this, shows error
public function getRows111($id='',$assignment_id){
public function getRows111($id='',$assignment_id=""){
$this->db->select('*');
$this->db->from('tbl_assignments_files');
//if assignment_id is not empty it will add where clause to statement
if($assignment_id != ""){
$this->db->where('assignment_id',$assignment_id);
}
//if id is not empty it will add where clause to statement
if($id != ""){
$this->db->where('assignment_file_id',$id);
}
$query = $this->db->get();
$result = $query->result_array();
return !empty($result)?$result:false;
}
}
To call model function first parameter will be $id and second will be $assignment_id
To call model function by $id
$data = $this->AdminModel->getRows111($id);
OR
$data = $this->AdminModel->getRows111($id,'');
To call function by $assignment_id
$data = $this->AdminModel->getRows111("",$assignment_id);
To call function by both $id and $assignment_id
$data = $this->AdminModel->getRows111($id,$assignment_id);
Hope this helps!
you are using your function wrong,
base on the function getRows111 it does have 2 parameters need the $id and $assignment_id
proper used would be like this,
$this->AdminModel->getRows111($id, $assignment_id);
I suggest for your function to make it more flexible is this.
public function getRows111($array){
$id = $array[0];
$assignment_id = $array[1];
}
then call the function using with array parameter
$this->AdminModel->getRows111([$id, $assignment_id]);
to handle the array and you want it to be more flexible.
public function getRows111($array){
$id = isset($array['id']) ? $array['id'] : "";
$assignment_id = isset($array['assignment_id']) ? $array['assignment_id'] : "";
}
call the function like this
$this->AdminModel->getRows111(
['id' => $id,
'assignment_id' => $assignment_id
]);
// if you want just the id
$this->AdminModel->getRows111(['id' => $id]);
// or the the assignment id
$this->AdminModel->getRows111(['assignment_id' => $assignment_id]);
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();
Hello any one can tell that how to load two function from model class in controller one method. I want to run multiple select quires in one page using codeigniter:
Controller
public function property_detail( $id )
{
$this->load->model('insertmodel');
$select1 = $this->insertmodel->find($id);
$select2 = $this->insertmodel->detail_list();
$data = array();
$this->load->view('home/property_detail', ['select1'=>$select1], ['select2'=>$select2]);
//$this->load->view('home/property_detail', ['select2'=>$select2]);
}
Model
public function find( $id )
{
$query = $this->db->from('article')->where(['id'=> $id])->get();
if( $query->num_rows() )
return $query->row();
return false;
}
public function detail_list(){
$query1 = $this->db->query("select * from article");
return $query1->result();
}
In Controller
public function property_detail( $id )
{
$this->load->model('insertmodel');
$data['select1'] = $this->insertmodel->find($id);
$data['select2'] = $this->insertmodel->detail_list();
$this->load->view('home/property_detail', $data);
}
In Model
public function find($id)
{
$query = $this->db->get_where('article', array('id' => $id), 0, 0)->get();
if( $query->num_rows() > 0 )
{
$result = $query->result_array();
return $result;
}
else
{
return false;
}
}
public function detail_list()
{
$query1 = $this->db->query("select * from article");
$result = $query1->result_array();
return $result;
}
In View
foreach ($select2 as $item) {
# your foreach lop goes here
}
As well check empty() before passing it to the foreach loop
As an alternative to #Rijin's useful answer newMethod() can make calls to the existing model methods. This might be useful if you don't want to break the interface already created for the model because you are using find($id) and detail_list() in other code.
Model:
public function find($id)
{
$query = $this->db->from('article')->where(['id' => $id])->get();
if($query->num_rows())
return $query->row();
return false;
}
public function detail_list()
{
$query1 = $this->db->query("select * from article");
return $query1->result();
}
public function newMethod($id)
{
$result['select1'] = $this->find($id);
if($result['select1'] !== FALSE)
{
$result['select2'] = $this->detail_list();
return $result;
}
return FALSE;
}
Controller:
public function property_detail($id)
{
$this->load->model('insertmodel');
$data = $this->insertmodel->newMethod($id);
$this->load->view('home/property_detail', $data);
}
Model :
public function find( $id )
{
$query = $this->db->from('article')->where(['id'=> $id])->get();
if( $query->num_rows() )
return $query->row();
return false;
}
public function detail_list()
{
$query1 = $this->db->query("select * from article");
return $query1->result();
}
Controller :
public function property_detail( $id )
{
$this->load->model('insertmodel');
$data['select1'] = $this->insertmodel->newMethod($id);
$data['select2'] = $this->insertmodel->detail_list();
$this->load->view('home/property_detail', $data);
}
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 :)