1

我有两个表:登录and跟随`。

  • 表名:login
    字段:id,email,username,imageurl

  • 表名:follow
    字段:id:user_id:follow_id

这就像 Twitter 追随者的概念。我想获取姓名的详细信息myfollower以及myfollower以下人员的姓名。

为此,我编写了如下代码。

public function follw ()
{
    if( $this->input->get("userid")  )

     {          

    extract($this->input->get()); 

    $followers_list = array();

    $follower = array();

    $query = $this->db->query('select follow_id from follow where user_id = '.$userid.'')->result();

    foreach($query as $row)

    { 
        $follower['follower_id'] = $row->follow_id;

        if($follower['follower_id'] == "")
        {
            echo "hi";
        }

        else
            {
        $query3 = $this->db->query('select username from login where id = '.$follower['follower_id'].'')->result();

        foreach($query3 as $row3)

            {
                $follower['followuser'] = $row3->username;
            }

        $query1 = $this->db->query('select follow_id from follow where user_id = '.$follower['follower_id'].'')->result();

        foreach($query1 as $row1)

            {
                $follower['follow_id'] = $row1->follow_id;

            if($follower['follow_id'] == "")

            {
                echo "jeeva";
                }


            else
            {
        $query2 = $this->db->query('select username from login where id = '.$follower['follow_id'].'')->result();

        foreach($query2 as $row2)

            {
                $follower['username'] = $row2->username;


            }

            }//second for each in else loop
            }//first foreach in else loop

            }//main else
        $followers_list[] = $follower;

        }

        $str = json_encode($followers_list);
    echo stripslashes($str);
}

else
{
    echo '[{"status":"Failure  - Error Occured - Not Enough Details provided"}]';

    }
}

我得到这样的输出:

[{"follower_id":"12","followuser":"janmejoy","follow_id":"24","username":"sarvana"},{"follower_id":"10","followuser":"jeeva","follow_id":"23","username":"selva"},{"follower_id":"6","followuser":"raj","follow_id":"17","username":"jeeva"},{"follower_id":"23","followuser":"selva","follow_id":"22","username":"guru"}]

此输出显示myfollower' 姓名 和myfollower' 关注人员姓名,但问题是它仅显示myfollower' 关注人员姓名的一个成员。

但是,我想要这样的输出:

[{"follower_id":"12","followuser":"janmejoy",{"follow_id":"24","username":"sarvana",follow_id":"13","username":"jai",follow_id":"9","username":"raj"}},{"follower_id":"10","followuser":"jeeva","follow_id":"23","username":"selva"},{"follower_id":"6","followuser":"raj","follow_id":"17","username":"jeeva"},{"follower_id":"23","followuser":"selva","follow_id":"22","username":"guru"}]
4

1 回答 1

1

[{"follower_id":"12","followuser":"janmejoy",{"follow_id":"24","username":"sarvana",follow_id":"13","username":"jai", follow_id":"9","username":"raj"}},{"follower_id":"10","followuser":"jeeva","follow_id":"23","username":"selva"} ,{"follower_id":"6","followuser":"raj","follow_id":"17","username":"jeeva"},{"follower_id":"23","followuser":"selva ","follow_id":"22","用户名":"guru"}]

此代码是无效的 JSON 变量。我无法为你解决这个问题。请更新您的问题。

于 2012-09-02T06:05:19.990 回答