Codeigniter, Result() VS. Result_Array()

codeigniter, result() vs. result_array()

Result has an optional $type parameter which decides what type of result is returned. By default ($type = "object"), it returns an object (result_object()). It can be set to "array", then it will return an array of result, that being equivalent of caling result_array(). The third version accepts a custom class to use as a result object.

The code from CodeIgniter:

/**
* Query result. Acts as a wrapper function for the following functions.
*
* @param string $type 'object', 'array' or a custom class name
* @return array
*/
public function result($type = 'object')
{
if ($type === 'array')
{
return $this->result_array();
}
elseif ($type === 'object')
{
return $this->result_object();
}
else
{
return $this->custom_result_object($type);
}
}

Arrays are technically faster, but they are not objects. It depends where do you want to use the result. Most of the time, arrays are sufficient.

Difference between row_array and result_array

From the documentation, row_array returns a single result and result_array returns multiple results (usually for use in a loop).

Examples from the documentation:

Result_array:

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

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

Row_array:

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

if ($query->num_rows() > 0)
{
$row = $query->row_array();

echo $row['title'];
echo $row['name'];
echo $row['body'];
}

Codeigniter - How to access the result_array()

Try this, no need to use result_array() here

function surcharge_load_data(){
$data = $this->Surcharge_model->surcharge_load_data();
foreach($data as $r) {//changes
echo $r['surcharge_id'];
}
}

HERE, you are using result_array() so you need to use each object like $r['surcharge_id'] if you use result() then you need to use $r->surcharge_id

Codeigniter $query- row_array() SQL $query- result_array()

Hope this will help you :

You can modify your result array like this :

$this->db->join('itemsToCats', 'items.itemID = itemsToCats.itemId','left');
$this->db->join('categories', 'itemsToCats.catId = categories.ctgID', 'left');
$this->db->join('itemImg', ' items.itemID = itemImg.itemID', 'left');
$this->db->select('items.itemID, itemSKU, itemName, itemSDesc, addDate, chgDate, ctgID, ctgName, parentID, itemImg');
$query = $this->db->get_where('items', array('items.itemID' => $itemID));
if ($query->num_rows() > 0 )
{
foreach ($query->result_array() as $key => $item)
{
$data['itemID'] = $item['itemID'];
$data['itemSKU'] = $item['itemSKU'];
$data['itemName'] = $item['itemName'];
$data['itemSDesc'] = $item['itemSDesc'];
$data['addDate'] = $item['addDate'];
$data['chgDate'] = $item['chgDate'];
$data['ctgID'] = $item['ctgID'];
$data['ctgName'] = $item['ctgName'];
$data['parentID'] = $item['parentID'];

$data['itemImg'][$key] = $item['itemImg'];
}
}
//print_r($data);die;
return $data;

CodeIgniter - result_array in memory

I believe you are looking for the little-known unbuffered_row function:

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

while ($row = $query->unbuffered_row())
{
echo $row->title;
echo $row->name;
echo $row->body;
}

This method returns a single result row without prefetching the whole
result in memory as row() does. If your query has more than one row,
it returns the current row and moves the internal data pointer ahead.

https://www.codeigniter.com/userguide3/database/results.html

You can also get an array:

$query->unbuffered_row('array');        // associative array

Codeigniter : Pass result_set from row_array and result_array to view file

As @Dinesh mentioned, you can access the $footer directly and the $image_info in a loop.

Example of $footer in your view:

<footer>
<?= $footer['address1'] ?>
<?= $footer['address2'] ?>
</footer>

Example of $image_info in your view

<footer>
<?php foreach($image_info as $img) { ?>
<img src="YOUR_PATH/<?= $img['file_name']; ?>"/>
<?php } ?>
</footer>

CodeIgniter difference in result_array when using while or foreach

The while() loop runs for as long as whatever is inside the parentheses is truthy.

You're assigning the value of $this->synchronization_model->get_current_knowledge($account['id']) to the variable $record every time the loop runs. Of course, the variable has a value (since you're assigning it), and so its always truthy. That's where you get your infinite loop.

Codeigniter when does result_array() return a single or multi dimensional array?

$query->result_array() always returns you a 2D array (unless the database returns no results, then it returns an empty array).

It returns you an array of "result" arrays. Each result array contains that row's fields.

Docs: http://ellislab.com/codeigniter/user-guide/database/results.html



Related Topics



Leave a reply



Submit