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:
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.
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.
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
CodeIgniter is a framework that is known for requiring a minimum amount of customization to get it up and running. This allows those who choose it to work at a good pace. It has been updated many times since its inception in 2006. Now the most recent version is 4.0.3.
https://www.codeigniter.com/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.