getting error while passing a array through loop - codeigniter

This my controller function
public function update_monthly() {
$data = $this->student_model->get_due_info();
foreach ($data as $value) {
$due = $this->student_model->get_due_by_id($value);
$grade = $this->student_model->get_grade_by_id($value);
$grade_due = $this->student_model->get_due_by_grade($grade);
$new_due = $due + $grade_due;
$this->db->set('md_due',$new_due, FALSE);
}
}
I am getting following error
A PHP Error was encountered
Severity: Notice
Message: Array to string conversion
Filename: database/DB_active_rec.php
Line Number: 427
and
A Database Error Occurred
Error Number: 1054
Unknown column 'Array' in 'where clause'
SELECT `md_due` FROM (`monthly_due`) WHERE `md_student_id` = Array
Filename: F:\xampp\htdocs\student\system\database\DB_driver.php
Line Number: 330
Model Code
public function get_due_by_id($id) {
$this->db->select('md_due');
$this->db->from('monthly_due');
$this->db->where('md_student_id', $id);
$query = $this->db->get();
return $query->result_array();
}
public function get_due_by_grade($id) {
$this->db->select('dt_fee');
$this->db->from('due_table');
$this->db->where('dt_grade', $id);
$query = $this->db->get();
return $query->result_array();
}
public function get_grade_by_id($id) {
$this->db->select('grade');
$this->db->from('student_info');
$this->db->where('student_gen_id', $id);
$query = $this->db->get();
return $query->result_array();
}
public function get_due_info() {
$this->db->select('md_student_id');
$this->db->from('monthly_due');
$query = $this->db->get();
return $query->result_array();
}

Pass $value['md_student_id'] to the model instead of $value in controller.
$data = $this->student_model->get_due_info();
foreach ($data as $value) {
$due = $this->student_model->get_due_by_id($value['md_student_id'] );
$grade = $this->student_model->get_grade_by_id($value['md_student_id']);
$grade_due = $this->student_model->get_due_by_grade($grade[0]['grade']);
//Add remaining logic
}

you are passing whole the array to your model not just the value you want
$due = $this->student_model->get_due_by_id($value);
the right thing is to pass
$value['md_student_id']
this is the value you actually want to pass
replace your code with this
foreach ($data as $value) {
$due = $this->student_model->get_due_by_id($value['md_student_id'] );
$grade = $this->student_model->get_grade_by_id($value['md_student_id']);
$grade_due = $this->student_model->get_due_by_grade($grade[0]['grade']);
$new_due = $due + $grade_due;
$this->db->set('md_due',$new_due, FALSE);
}

Related

Call to a member function setPath() on a non-object in laravel

In my Laravel web application, getting error like "Call to a member function setPath() on a non-object" while using pagination.
My code is
In model,
public function scopegetApartments()
{
$list1 = DB::table('properties')->orderBy('id','DESC')->paginate(1);
$list = array();
foreach($list1 as $listeach){
$imgs = DB::table('property_images')->where('property_id',$listeach->id)->get();;
$images = array();
foreach($imgs as $img){
array_push($images, $img->image);
}
$lists = array(
'id'=>$listeach->id,
'name'=>$listeach->name,
'rent'=>$listeach->rent,
'shortdescription'=>$listeach->shortdescription,
'images'=>$images,
);
array_push($list, $lists);
}
if(count($list)>0){
return $list;
}else{
return '[]';
}
}
In view,
$apartmentlist->setPath('serviceapartments');
echo $apartmentlist->render();
How to resolve this problem?

Codeigniter confusing with $this->db queries

I'm trying to access data by using another model's method in my model but it gives me error because it confuses by previous $this->db parameters:
$this->db->select('*');
$this->db->group_start();
$this->db->like('title',$keyword);
$this->db->or_like('keyword',$keyword);
$this->db->group_end();
$locations = $this->place_model->search_ids_by_name($location);
and the search_ids_by_name() of Place_model is like this:
public function search_ids_by_name($q) {
$this->db->select('id');
$this->db->like('name',$q);
$qry = $this->db->get('places');
$results = $qry->result_array();
$place_ids = array();
foreach ($results as $result) {
array_push($place_ids, $result['id']);
}
return $place_ids;
}
But it gives me below error
Error Number: 1054, Unknown column 'category' in 'where clause'
Filename: models/Place_model.php
It seems in my place_model function also using like and or_like methods. How can I seperate them.
I found a solution. I made another connection:
public function search_ids_by_name($q) {
$places_db = $this->load->database('default', TRUE);
$places_db->select('id');
$places_db->like('name',$q);
$qry = $places_db->get('places');
$results = $qry->result_array();
$place_ids = array();
foreach ($results as $result) {
array_push($place_ids, $result['id']);
}
return $place_ids;
}

Cannot use object of type stdClass as an array in codeigniter

My controller code
function edtpost($id)
{
$this->load->model('post');
$data = $this->post->edt_post($id);
$this->load->model('category');
$data['catname'] = $this->category->retrivecat();
$this->load->view('dashboard/edit_post',$data);
}
my model code
model post
public function edt_post($id)
{
$query = $this->db->get_where('post', array('id' => $id));
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
return $row;
}
}
return false;
}
category model
public function retrivecat()
{
$query = $this->db->get('category');
foreach ($query->result() as $row)
{
$data[] = $row->catname;
}
return $data;
}
in controller & this code $data['catname'] = $this->category->retrivecat();
I have one error:
Cannot use object of type stdClass as array
Just change your that line with following :-
$data->catname = $this->category->retrivecat();

CodeIgniter pass variables form controller to model

Ok I want to pass two variables from a controller to a model but I get some kind of error. Am I passing variables on right way? My syntax is:
Controller:
public function add_tag(){
if(isset($_POST['id_slike']) && isset($_POST['id_taga'])){
$slika = $_POST['id_slike'];
$tag = $_POST['id_taga'];
$this->load->model("Member_model");
$res = $this->Member_model->add_tags($slike, $tag);
foreach ($res->result() as $r){
echo $r->name;
}
}
else{
echo "";
}
}
Model:
public function add_tags(){
$data = array(
'tags_id' => $tag ,
'photos_id' => $slika
);
$check = $this->db->query("SELECT tags_id,photos_id FROM bridge WHERE bridge.tags_id='{$tag}' AND bridge.photos_id={$slika} ");
if($check->num_rows()==0){
$this->db->insert('bridge',$data);
$res = $this->db->query("SELECT name FROM tags where `tags`.`id`='{$tag}' ");
return $res;
}
}
you are passing variables correctly, but do not get them correctly in the model, which should look like this:
public function add_tags($slike, $tag){
//your other code
}
The following code write on the controller file:-
$data = array();
$this->load->model('dbmodel');
$data['item'] = $this->dbmodel->getData('*','catagory',array('cat_id'=>21));
$this->load->view('listing_view', $data);
The following code write on the dbmodel file:-
public function getData($cols, $table, $where=array()){
$this->db->select($cols);
$this->db->from($table);
$this->db->where($where);
$query = $this->db->get();
$result = $query->result();
return $result;}

How can I use CodeIgniter form dropdown function?

codeIgniter form_dropdown() function can receive only associative array but I have multi dimension array by using result_array() function. How can I fetch my data on form_dropdown() function?
Let's say you want a dropdown of items, using result_array():
$query = $this->db->query("SELECT id, item_name FROM items");
$options = array();
foreach ($query->result_array() as $row){
$options[$row['id']] = $row['item_name'];
}
echo form_dropdown('my_items_dropdown', $options, '1');
I have extended the class DB_result.php in system/database with this new function
public function dropdown_array($id_field = FALSE, $list_field = FALSE)
{
$result = array();
if ($id_field === FALSE || $list_field === FALSE) return $result;
$array = $this->result_array();
foreach ($array as $value) {
$result[$value[$id_field]] = $value[$list_field];
}
return $result;
}
Now you can simply call from every Model class your new function to generate a dropdown-compliant array like this:
$customers = $this->db->get('customers');
$result = $customers->dropdown_array('id', 'name');
return $result;

Resources