如何在Codeigniter中设置character_limiter()

Ebr*_*him 0 php codeigniter character

我是Codeigniter的新手。现在,我想在视图中设置字符数限制。首先,使用$ query_result-> result()从数据库中获取数据,然后使用foreach()在视图中显示数据。

这是我的控制器,模型和视图:

public function index() {
$data = array();
$data['category'] = $this->product_model->selectAllcategory();
$data['randProduct'] = $this->product_model->selectRandomProduct();
$data['products'] = $this->product_model->selectAllProduct();
$data['maincontent'] = $this->load->view('home', $data, true);
$data['title'] = 'Welcome Russel Store';
$this->load->view('index', $data);
}
Run Code Online (Sandbox Code Playgroud)

而我的模特:

public function selectAllProduct() {
$this->db->select('*');
$this->db->from('product');
$this->db->where('status', 1);
$this->db->order_by('product_id', 'desc');
$query_result = $this->db->get();
$result = $query_result->result();
return $result;
}
Run Code Online (Sandbox Code Playgroud)

我想在视图中设置字符数限制:

http://russelstore.mastersite.info

echo character_limiter($ result-> product_title,25); 

Way*_*Tun 5

http://ellislab.com/codeigniter/user-guide/helpers/text_helper.html

您应该导入文本助手

在您的控制器中,最好在构造函数中加载帮助程序,模型和库

function __construct()
{
  parent::__construct();
  $this->load->helper('text');
  $this->load->model('products_model'); //name of your model class

}

function index()
{
  $data['products']=$this->products_model->selectAllProduct();
  $this->load->view('index',$data);
}
Run Code Online (Sandbox Code Playgroud)

在您看来index.php

//This is an example from CI's home page        
//$string = "Here is a nice text string consisting of eleven words.";            
//$string = word_limiter($string, 4);

    foreach($products as $p)
    {
        $limited_word = word_limiter($p[product_title],25);
        echo $limited_word;
    }
Run Code Online (Sandbox Code Playgroud)