如何获得单行CodeIgniter

时间:2017-06-08 04:53:58

标签: php mysql codeigniter

当我打电话给#34; conversationID",

时,如何获得单行?

现在我打电话给" conversationID = 0"它显示所有" conversationID"按0

消息表,

|ID|conversationID|from|to|
|1|0|3|4
|2|0|4|3
|3|0|3|4
|4|1|2|4
|5|1|4|2

控制器

public function index()
{
    $conversations_inbox = $this->conversation_model->get_conversation_inbox($this->user->info->ID);
    $this->template->loadContent("conversations/index.php", array("conversations_inbox" =>  $conversations_inbox,)
    );
}

模型

public function get_conversation_inbox($userid) {
        $query = $this->db->where("conversations.from_user = ". $userid)->or_where("conversations.to_user = ". $userid)->select("*")->from("conversations")->join("conversation_message", "conversation_message.conversationID = conversations.conversationID")->order_by("conversation_message.date", "DESC")->get();
        return $query->row();
    }


<?php
foreach ($conversations_inbox as $c) { ?>
<li class="mail-starred">
    <div class="mail-from"><a href="<?php echo base_url("conversations/message/") . $c['conversationID'] ?>"><img src="<?php echo base_url() . $c['store_picture'] ?>" style="width: 40px;height: 40px;margin-right: 10px;">
    <?php echo $c['store_name'] ?></a></div>
    <div class="mail-subject">
    <a href="<?php echo base_url("conversations/message/") . $c['conversationID'] ?>">Test</a>
    </div>
</li>
<?php } ?>

2 个答案:

答案 0 :(得分:1)

您正在使用$query->result()返回可能结果数组,如果您只需要一条记录,则应使用$query->row()

您可以在此处详细了解如何从文档中返回结果:https://www.codeigniter.com/user_guide/database/results.html#result-rows

答案 1 :(得分:0)

您可以通过以下方式获得包含数组属性的单行:

$row = $query->row_array()

然后您可以通过表格示例获取属性:

if ($row) {
    echo $row['ID'];
    echo $row['conversationID'];
    echo $row['from'];
    echo $row['to'];
}