如何获取CodeIgniter中的单行数据

3

如何在调用“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 } ?>

return $query->row(); - Madhawa Priyashantha
2个回答

2
你可以通过以下方式获取包含数组属性的单行数据:
$row = $query->row_array()

然后,您可以通过表格示例获取属性:
if ($row) {
    echo $row['ID'];
    echo $row['conversationID'];
    echo $row['from'];
    echo $row['to'];
}

2

获取错误信息:尝试获取非对象属性。 - MSufian
您可以通过$varName['fieldName']访问数组符号的变量,如果是对象,则使用 $varName->fieldName。请检查print_r($conversations_inbox)的输出,并相应地使用语法。参考:https://dev59.com/1m435IYBdhLWcg3w2z98 - Jigar Shah
输出是数组还是对象?请标记为答案并编辑您的问题,以便我可以帮助您。 - Jigar Shah
然后你需要使用 $varName['fieldName'] 访问,而你当前的代码使用了对象符号。 - Jigar Shah
仍然无法工作,错误信息:非法字符串偏移量 'store_name'<?php foreach ($conversations_inbox as $c) { ?> <?php echo $c['store_name'] ?><?php } ?> - MSufian
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接