How To Set character_limiter() in Codeigniter

2020-06-29 11:07发布

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

And my Model:

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

3条回答
The star\"
2楼-- · 2020-06-29 11:32

First Load the helper class in either autoload.php or in the controller class.

For Global use , write in Autoload.php:

$autoload['helper'] = array('text');

OR If you want to use only in one controller.php class then:

function __construct()
{
  parent::__construct();
  $this->load->helper('text');
}

Then in your view.php:

<?php 
  if(!empty($ques)) {
    foreach($ques as $list) { 
      $title = character_limiter($list->Title, 80); // Here we are setting the title character limit to 80
      <?php echo $title;?>
    }
  }
?>
查看更多
混吃等死
3楼-- · 2020-06-29 11:40

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

You should import Text Helper

in your controller, it is a good practice to load helper, models and libraries in a constructor

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

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;
    }
查看更多
淡お忘
4楼-- · 2020-06-29 11:49

you can use my code there

<?php $artikel=$a->isi;$artikel=character_limiter($artikel,200); ?>
            <p><?php echo $artikel ?></p>
            <a href="<?php echo base_url()."index_cont/list_artikel_kat/".$a->id_artikel; ?>" target="_blank">Selanjutnya</a>
        <?php endforeach; ?>
查看更多
登录 后发表回答