i am using codeigniter library, and am stuck in a matter. first here’s my array
Array (
[0] => Array (
[product_name] => some data
[product_slug] => some data
[price] => some Data...
[sell_price] => 2000.00
)
[1] => Array (
[product_name] => some data
[product_slug] => some data
[price] => some Data...
[sell_price] => 2000.00
)
)
now here’s my code in controller:
$this->load->model('product_model');
$data['product']=$this->product_model->get_product();
$this->load->view('show_product',$data);
and heres my model codes
function get_product()
{
return $this->db->query("SELECT * FROM dt_product")->result_array();
}
and ultimately my view code
<div class="featured_right_gallery_bellow">
<ul class="product_list">
<?php foreach($product as $product)
{?>
<li>
<div class="pro_name"><?php echo $product['product_name'];?></div>
</li>
<?php
}?>
</ul>
</div>
now my question is that, i want to jst show the first 20 letters of the product_name….but here the full name is being shown…
i know i can write a mysql code like SELECT LEFT(product_name,20),sell_price,price FROM dt_product… but i dont want to do that, because the table contains nearly 20 attributes, and i have to change the query multiple times…. all i want is to someone point me out to the solution of how to display the first 20 letter of the product_name using the offset concept,
i tried echoing $product[‘product_name’][20], but withing foreach loop, its throwing an error of undefined index..
Try this:
Read up on function
substr()here: http://php.net/manual/en/function.substr.phpEdit:
If you want to do the edit directly in your array (which I don’t think is necessary):
Assuming
$arrayis the array:Then do this: