显示两个表中的数据 - codeigniter

时间:2015-10-09 04:50:42

标签: php mysql codeigniter

我有两张桌子:

Table 1 -- rps_users:

id
membership_number
family_name
first_name
email_address
staff (staff column having values Y and N)

Table 2 -- rps_volunteer_score:
volunteer_id
registration_assessor
interviewer
professional_registration_advisor

第一个表中的id列包含与第二个表中volunteer_id列相同的值。 每个用户可以有多个registration_assessorinterviewerprofessional_registration_advisor值。这些列的可能值为0-4

我希望在浏览器记录中显示N列中值staff的用户,其格式如下:

membership_number:000
family_name:Name1
first_name:Name2
email_address:a@b.com
Type:Interviwer/Assessor/Registrar/PRA/Moderator(If the user have multuple role display all ie, Interviewer,Assessor)

根据以下条件记录的类型列

    if( $interviewer!="" && $interviewer <= 4 )echo "Interviewer";
    if($registration_assessor !="" && $registration_assessor >= 2 &&     $registration_assessor <= 4 )echo "Assessor";
    if($registration_assessor !="" && $registration_assessor >= 3 && $registration_assessor <= 4)echo "Moderator";
    if($registration_assessor !="" && $registration_assessor == 4 ) echo "Registrar";
    if($professional_registration_advisor !="" && $professional_registration_advisor== 1)echo "PRA";

这是我的模特函数

    public function get_volunteers_list($condition)
{

    $this->db->select("u.id,u.membership_number,u.family_name,u.first_name,u.email_address,vs.volunteer_id,vs.registration_assessor, vs.interviewer,vs.professional_registration_advisor");
    $this->db->from(self::$tbl_name . " as u");
    $this->db->join(Volunteer_score::$tbl_name . " as vs", "u.id = vs.volunteer_id","left");
    $this->db->where($condition);
    $query = $this->db->get(); 
    return $query->result();

}

这是我的控制器代码

    $volunteer_list = $this->Users->get_volunteers_list(array("u.staff"=>'N'));
    $this->data["volunteer_list"] = $volunteer_list;

这是我的观看代码

    <?php
        if($volunteer_list)
        {
        foreach($volunteer_list as $volunteer)
        {

            echo "<td>{$volunteer->membership_number}</td>";
echo "<td>{$volunteer->family_name}, {$first_name}</td>";
echo "<td>{$volunteer->email_address}</td>";
echo "<td>";(Here i want to display the type of the user)
//Following is the condition for "TYPE"
/*if( $interviewer!="" && $interviewer <= 4 )
{
    echo "Interviewer"; 


}

if($registration_assessor !="" && $registration_assessor >= 2 && $registration_assessor <= 4 )
{
    echo "Assessor";


}
if($registration_assessor !="" && $registration_assessor >= 3 && $registration_assessor <= 4)
{
    echo "Moderator";


}
if($registration_assessor !="" && $registration_assessor == 4 )
{
    echo "Registrar";


}
if($professional_registration_advisor !="" && $professional_registration_advisor == 1)
{
    echo "PRA";


}*/
echo"</td>";
echo "</tr>";
        }
        }
        ?>

1 个答案:

答案 0 :(得分:1)

组装好的sql看起来像:

SELECT u.id, u.membership_number, u.family_name, u.first_name, u.email_address, 
GROUP_CONCAT(vs.registration_category) AS type
FROM volunteers_list as u
LEFT JOIN Volunteer_score as vs ON(u.id = vs.volunteer_id)
GROUP BY u.id;

你会得到一行,其中registration_categorys用逗号分隔