简体   繁体   English

Codeigniter 返回结果和行

[英]Codeigniter return result and row

In the Model class I use return $query->row();在 Model class 我使用return $query->row(); to return single rows and return $query->result();返回单行并return $query->result(); when returning multiple rows.返回多行时。

On a single page I have to return single rows and multiple rows from 2 separate tables.在单个页面上,我必须从 2 个单独的表中返回单行和多行。

Table users contains general information like the user name, full name, and email address.users包含一般信息,如用户名、全名和 email 地址。 Table user_links contains links submitted by the respective user and has multiple rows for each user.user_links包含各个用户提交的链接,并且每个用户都有多行。

My query我的查询

        $this->db->select('*');
        $this->db->from('users');
        $this->db->join('user_links', "user_links.user_id = users.user_id");
        $this->db->where('users.user_id', $user_id);
        $this->db->where('user_links.user_id', $user_id);
        $query = $this->db->get();
        return $query->row(); 

In my controller I load the query in my view by在我的 controller 中,我在视图中加载查询

$data['row'] = $this->User_model->user_read($user_id); , ,

$user_id being the 3rd URL segment containing the unique user id. $user_id是包含唯一用户 ID 的第三个 URL 段。

Finally, in my view I retrieve rows by echo $row->first_name;最后,在我看来,我通过echo $row->first_name;检索行。

This works for single rows but how can I create a foreach loop for user links?这适用于单行,但如何为用户链接创建一个foreach循环? The goal is to avoid loops for single rows and use them just for retrieving multiple rows.目标是避免单行循环并将它们仅用于检索多行。

This is psuedo code but you can probobly do something like this这是伪代码,但你可能可以做这样的事情

    $this->db->select('*');
    $this->db->from('users');
    $this->db->join('user_links', "user_links.user_id = users.user_id");
    $this->db->where('users.user_id', $user_id);
    $this->db->where('user_links.user_id', $user_id);
    $query = $this->db->get();
    if ($query->num_rows() == 1)
    {
        return $query->row(); 
    }
    elseif ($query->num_rows() > 1)
    {
        return $query->result_array(); //This returns an array of results which you can whatever you need with it
    }
    else 
    {
         //Add some logic to handle if there are zero results
    }

Hard to tell what you want.很难说出你想要什么。

You want a function that checks if you're passing a row() or a result() and treat them accordingly.您需要一个 function 来检查您是否传递了row()result()并相应地处理它们。

In my opinion your code would be easier to read (and maintain) if you just passed everything as a result() .在我看来,如果您只是将所有内容作为result() And do check if the set is empty to show a nice message to the user.并检查该集合是否为空以向用户显示一条好消息。

If I understand your question correctly, you want to get both the user data as well as user_links data with a single query while avoiding iterating through it to get the user's data.如果我正确理解您的问题,您希望通过单个查询同时获取用户数据和 user_links 数据,同时避免遍历它来获取用户数据。 While this may be possible using result_array, I would advise against it since you will get 0 results when there are no entries in user_links for that particular user.虽然这可能使用 result_array,但我建议不要这样做,因为当 user_links 中没有该特定用户的条目时,您将获得 0 个结果。

My suggestion is that you use two queries, one to get the user from the user table, another to get user's links from user_links table.我的建议是您使用两个查询,一个从 user 表中获取用户,另一个从 user_links 表中获取用户的链接。 This will also help you avoid joins.这也将帮助您避免连接。

Sounds like you're looking for some of the other features already provided by CI's Active Record: http://codeigniter.com/user_guide/database/results.html听起来您正在寻找 CI 的 Active Record 已经提供的其他一些功能: http://codeigniter.com/user_guide/database/results.html

For what you are doing it sounds like using the built in function result_array would work.对于您正在做的事情,听起来使用内置的 function result_array 会起作用。 You use it like so:你像这样使用它:

TAKEN FROM LINK ABOVE取自以上链接

$query = $this->db->query("YOUR QUERY");

foreach ($query->result_array() as $row)
{
   echo $row['title'];
   echo $row['name'];
   echo $row['body'];
}

You can just replace this line:您可以替换这一行:

$this->db->join('user_links', "user_links.user_id = users.user_id");

With this one:有了这个:

$this->db->join('user_links', "user_links.user_id = users.user_id", 'left outer');

Join in codeigniter uses INNER JOIN by default but in some cases if there is no data in user_links it returns nothing so u can use LEFT JOIN in your frame work CI it used like i mentioned above.加入 codeigniter 默认使用INNER JOIN ,但在某些情况下,如果user_links中没有数据,它不会返回任何内容,因此您可以在框架 CI 中使用LEFT JOIN ,就像我上面提到的那样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM