【问题标题】:How To Set character_limiter() in Codeigniter如何在 Codeigniter 中设置 character_limiter()
【发布时间】:2013-04-17 01:14:08
【问题描述】:

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

还有我的模特:

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

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

http://russelstore.mastersite.info echo character_limiter($result->product_title, 25);

【问题讨论】:

  • character_limiter() 应该可以做到这一点。我对你的问题感到困惑。
  • @Ebrahim,在为 StackOverflow 格式化代码时,您不需要
     标签或反引号。只需将所有行缩进四个空格。谢谢。

标签: php codeigniter character


【解决方案1】:

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

在你的视图 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;
    }

【讨论】:

  • 如果解决了您的问题,请标记为答案。谢谢
【解决方案2】:

你可以在那里使用我的代码

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

【讨论】:

    【解决方案3】:

    首先在autoload.phpcontroller 类中加载帮助程序类。

    全球使用,请写信Autoload.php

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

    或者如果您只想在一个 controller.php 类中使用,那么:

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

    然后在你的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;?>
        }
      }
    ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-18
      相关资源
      最近更新 更多