Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何从视图调用控制器函数?_Php_Codeigniter - Fatal编程技术网

Php 如何从视图调用控制器函数?

Php 如何从视图调用控制器函数?,php,codeigniter,Php,Codeigniter,我正在从数据库中获取产品的所有详细信息,并成功进入视图,但中间有产品详细信息太长,所以我想拆分该文本。我还在主控制器中创建了一个限制文本的函数,即: function limit_text($text, $length) // Limit Text { if(strlen($text) > $length) { $stringCut = substr($text, 0, $length); $text = substr($stringCut, 0,

我正在从数据库中获取产品的所有详细信息,并成功进入视图,但中间有产品详细信息太长,所以我想拆分该文本。我还在主控制器中创建了一个限制文本的函数,即:

function limit_text($text, $length) // Limit Text
{
    if(strlen($text) > $length) {
        $stringCut = substr($text, 0, $length);
        $text = substr($stringCut, 0, strrpos($stringCut, ' '));
    }
    return $text;
}

但问题是,我无法从视图中调用此函数…请帮助我..

您不可以-使用辅助函数来代替在控制器中创建函数,我建议您在中创建它,以便您可以从视图文件轻松访问它。 创建helper,比如说mysome\u helper.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

function limit_text($text, $length) // Limit Text
{
    if(strlen($text) > $length) {
        $stringCut = substr($text, 0, $length);
        $text = substr($stringCut, 0, strrpos($stringCut, ' '));
    }
    return $text;
}
然后您可以在视图中访问,如:

<?php echo limit_text("some long tezxt", 120); ?>

@user3415023不客气……)顺便说一句,在开始编码之前,试着阅读文档以获得更好的理解。。
<?php echo limit_text("some long tezxt", 120); ?>