Reputation: 6799
I have the following code in my codeigniter's view file.
<?php foreach ($records as $rows){?>
<? echo $rows['product_name']; ?> <br>
<? } ?>
my model
$this->db->select('*');
$this->db->from('products');
$this->db->order_by('product_name','ASC');
$getData = $this->db->get('');
if($getData->num_rows() > 0)
return $getData->result_array();
else
return null;
If I run the above code I get the following result
Pepsi
Coke
Mountain Dew
I am trying to show only the first result (Pepsi). Could you please show me how to do that?
Upvotes: 0
Views: 1492
Reputation: 4014
reset is another solution to this problem. http://www.php.net/manual/en/function.reset.php
so in the example, $first_element = reset($records);
Upvotes: 0
Reputation: 3581
$records is an Array. You can specify the index like
<?=$records[0]['product_name']?>
Upvotes: 1
Reputation: 23061
This should work:
$query = $this->db->limit(0, 1);
See doc: http://codeigniter.com/user_guide/database/active_record.html
See also: CodeIgniter Database query limit
Upvotes: 0
Reputation: 9833
Use LIMIT
to tell the DB to pull only a limited number of records from the database
$this->db->from('products LIMIT 0,1');
Upvotes: 0