我是php以及codeigniter的新手。基本上我希望项目表中的所有项目与该项目的用户一起显示。**单个项目上可能有多个用户,或者用户尚未分配给该项目。**所以我尝试实现它但是我没有得到所需的输出。请帮我 !!以下是代码:
型号:
public function get_projects()
{
$sql = "SELECT * FROM `project`";
$query = $this->db->query($sql);
return $query->result();
}
public function list_projects()
{
$project_result = $this->get_projects();
$sql = "select user_fname , project_id , user_id from user";
$query = $this->db->query($sql);
$user_result = $query->result();
$result_arr = array();
$proj_id_arr = array();
foreach($project_result as $curr_proj):
if(in_array($curr_proj->project_id, $proj_id_arr)){
$result_arr[$curr_proj->project_name][] = array(
'project_id ' => $curr_proj->project_id,
'project_name' => $curr_proj->project_name
);
}else{
$result_arr[$curr_proj->project_name][] = array(
'project_id ' => $curr_proj->project_id,
'project_name' => $curr_proj->project_name
);
}
foreach($user_result as $curr_user):
if($curr_user->project_id == $curr_proj->project_id):
$result_arr[$curr_proj->project_name][] = array(
'project_id ' => $curr_proj->project_id,
'project_name' => $curr_proj->project_name,
'user' => $curr_user->user_fname,
'user_id' => $curr_user->user_id
);
$proj_id_arr[] = $curr_proj->project_id;
endif;
endforeach;
endforeach;
echo '<pre>';
print_r($result_arr);exit;
return $data;
}
控制器:
function index() {
$this->check_session();
$data['main_content'] = 'admin/project/list';
$data['lists']=$this->project_model->list_projects();
$data['projects']=$this->project_model->get_projects();
$data['result'] = false;
$this->load->view('common/admin_template', $data);
}
这是我的代码的结果:
Array
(
[project 1 ] => Array
(
[0] => Array
(
[project_id ] => 1
[project_name] => project 1
)
[1] => Array
(
[project_id ] => 1
[project_name] => project 1
[user] => user1
[user_id] => 1
)
[2] => Array
(
[project_id ] => 1
[project_name] => project 1
[user] => user2
[user_id] => 2
)
)
[project 2] => Array
(
[0] => Array
(
[project_id ] => 19
[project_name] => project 2
)
[1] => Array
(
[project_id ] => 19
[project_name] => project 2
[user] => user3
[user_id] => 3
)
)
[project 3] => Array
(
[0] => Array
(
[project_id ] => 20
[project_name] => project 3
)
)
[project 4] => Array
(
[0] => Array
(
[project_id ] => 21
[project_name] => project 4
)
)
)
有两个表:一个是由project_id,project_description组成的项目,另一个是具有user_id,user_fname,project_id(外键)的用户
答案 0 :(得分:0)
首先,你使用循环内循环。这不是一个好习惯。用这个 如果您只想获得至少拥有一个用户的项目。比,设置$ showOnlyHaveUser = true。 `
public function list_projects()
{
$showOnlyHaveUser = FALSE;
$project_result = $this->get_projects();
$sql = "select user_fname , project_id , user_id from user";
$query = $this->db->query($sql);
$user_result = $query->result();
$result_arr = array();
$proj_id_arr = array();
$projects = array();
foreach ($project_result as $project) :
$projects[$project->project_id] = $project;
if($showOnlyHaveUser==FALSE)
$result_arr[$project->project_name] = array();
endforeach;
foreach ($user_result as $user) :
$project_name = isset($projects[$user->project_id]) ? $projects[$user->project_id]->project_name : "";
$project_id = isset($projects[$user->project_id]) ? $projects[$user->project_id]->project_id : "";
if(!isset($result_arr[$project_name])):
$result_arr[$project_name] = array();
endif;
$temp = array(
'project_id ' => $project_id ,
'project_name' => $project_name,
'user' => $user->user_fname,
'user_id' => $user->user_id
);
$result_arr[$project_name][] = $temp;
endforeach;
echo '<pre>';
print_r($result_arr);exit;
return $data;
}`