PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, February 22, 2022

[FIXED] Array to string conversion in Php Codeigniter

 February 22, 2022     arrays, codeigniter, database, php, string     No comments   

Issue

I am trying to get data from database , based on the , manager and project_name, basically I want to get All the tasks under all the projects of particular manager,I successfully able to get all the projects, but when trying to get the tasks under all those projects getting error:enter image description here

Here is controller

  function index()
{
  // print_r($_REQUEST); 
  // die;
  $user_id =  $this->session->userdata('manager');
  $project_manager = $this->task_model->getmanager($user_id);
  // print_r($project_manager); 
  //   die;
  $project_name = $this->task_model->getProjectDetails($project_manager);
  //  print_r($project_name); 
  //  die;

 $data['tasks'] = $this->task_model->getTasksDetails($project_name); //error on this line
  // print_r($data); 
  // die;

Here is Model:

 function getmanager($user_id)
{
  $first_name =$this->db->select('first_name')->from('user_login')->where(array('id' => $user_id,'delete_flag'=>0))->get()->row();
 return  $first_name->first_name; 

} 
function getProjectDetails($project_manager)
{
//table  (projects)
 $delete_flag=1;
 $project_name =$this->db->get_where('projects',array('delete_flag!='=>$delete_flag,'project_manager'=>$project_manager))->result();
return  $project_name;

}


function getTasksDetails($project_name)
{
 $this->db->select('*,tasks.id as id, tasks.status as status');
   $this->db->join('user_login', 'tasks.assign_to = user_login.id');
   $this->db->where('tasks.delete_flag','0');
   $this->db->where('tasks.project_name' ,$project_name);
   return $this->db->get('tasks')->result(); //error on this line
  // return   $tasks->tasks;
} 

And running

 print_r($project_name); 
    die;

Results:

Array ( 
    [0] => stdClass Object ( 
      [id] => 4 
      [delete_flag] => 0 
      [project_name] => a test 
      [client_name] => a 
      [company] => AIM Solutions Sdb Bhd 
      [project_manager] => Ravi 
      [support_staff] => elango,test2 
      [flag] => 3 
    ) 
    [1] => stdClass Object ( 
      [id] => 5 
      [delete_flag] => 0 
      [project_name] => test project 
      [client_name] => test 
      [company] => AIM Solutions Sdb Bhd 
      [project_manager] => Ravi 
      [support_staff] => elango,test2,mani 
      [flag] => 0 
    ) 
 )

would appreciate if anyone can help.


Solution

You are sending an array/object into your getTasksDetails() function when that function should receive a string.

This line here:

$project_name = $this->project_model->getProjectDetails($project_manager);

Should be:

$project_name = $this->project_model->getProjectDetails($project_manager[0]->project_manager);

However I would go even further and change your function that returns the project manager to return just one row instead of result.

From what I see I suspect you're returning $foo->result() on that function, instead change that to $foo->row();

Then instead of using

$project_manager[0]->project_manager 

you would just use:

$project_manager->project_manager

This solution is obviously based on the fact that you might want projects details for just one project manager.

If what you want is all the project details for a group of project managers then you might want to tweak your project managers array into being just one array of ids and then change your query to use where in instead of where. So I just assume your function getmanager is probably wrong since you're sending a user_id and getting more than one result.

Based on your comments, saying that what you need is all the tasks associated with all the projects of a given project manager, what you need is the following:

// Controller Function
function index()
{
    $user_id =  $this->session->userdata('manager');
    // return the project manager name
    $project_manager = $this->task_model->getmanager($user_id);
    // Return all the projects for a given project manager
    $project_manager_projects = $this->task_model->getProjectDetails($project_manager); 
    // Get all tasks in a set of projects
    $data['tasks'] = $this->task_model->getTasksDetails($project_manager_projects);
}


// Model functions
function getmanager($user_id)
{
    $first_name =$this->db->select('first_name')
        ->from('user_login')
        ->where(array('id' => $user_id,'delete_flag'=>0))
        ->get()
        ->row();
    return  $first_name->first_name; 
} 

function getProjectDetails($project_manager)
{
    $delete_flag=1;
    $project_name = $this->db->get_where('projects',array(
        'delete_flag != ' => $delete_flag,
        'project_manager' => $project_manager)
    )->result();
    return  $project_name;
}


function getTasksDetails($project_manager_projects)
{
    $projects = array();
    foreach($project_manager_projects as $project) {
        $projects[] = $project->project_name;
    }
    if (empty($projects)) {
        return array();
    }
    return $this->db->select('*,tasks.id as id, tasks.status as status')
        ->join('user_login', 'tasks.assign_to = user_login.id')
        ->where('tasks.delete_flag','0')
        ->where_in('tasks.project_name' ,$projects)
        ->get('tasks')
        ->result(); 
} 

For your where_in to work, you need to use an array with just the data you are using, not an entire multidimensional array with a lot of fields that aren't used.

Now this code can be improved quite a lot, but to solve your question this should suffice.



Answered By - marcogmonteiro
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing