如何在Codeigniter中设置character_limiter() [英] How To Set character_limiter() in Codeigniter

查看:28
本文介绍了如何在Codeigniter中设置character_limiter()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm new to Codeigniter. Now I want to set the character limit in the view. First, get the data from the database with $query_result->result(), and then show it in the view using foreach().

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

Here is my Controller, Model and View:

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

我的模型:

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

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

And I want to set the character limit in the View:


http://russelstore.mastersite.info

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


推荐答案

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

您应在控制器中导入 Text Helper

,在构造函数中加载帮助器,模型和库是一个好习惯

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

在您看来index.php

at your view 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;
    }

这篇关于如何在Codeigniter中设置character_limiter()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆