Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get single row CodeIgniter

How can I get single row when I call "conversationID",

Right now when I call "conversationID = 0" it show all "conversationID" by 0

message table,

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

Controller

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,)
    );
}

Model

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 } ?>
like image 372
MSufian Avatar asked Feb 17 '26 21:02

MSufian


2 Answers

You are using $query->result() which returns array of possible results, if you need only one record you should use $query->row().

You can read more about returning result from documentation here: https://www.codeigniter.com/user_guide/database/results.html#result-rows

like image 166
Jigar Shah Avatar answered Feb 19 '26 10:02

Jigar Shah


You can get single row with array properties by:

$row = $query->row_array()

Then you can get properties by your table example:

if ($row) {
    echo $row['ID'];
    echo $row['conversationID'];
    echo $row['from'];
    echo $row['to'];
}
like image 44
Nick Tsai Avatar answered Feb 19 '26 10:02

Nick Tsai



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!