在 codeigniter 的视图中调用我自己的库

发布于 2024-12-13 17:39:12 字数 1365 浏览 0 评论 0原文

我刚刚在此文件夹(应用程序/库)上创建了自己的库,并按照所有步骤创建单独的库,

一旦我在控制器中加载该库,它就会执行该函数,但是当尝试将其传递到视图时,

这里 没有任何返回我的代码

我自己的函数

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

    class Common {

            public function date_arabic()
            {
            $daysarabic=array('الأحد','الاثنين','الثلاثاء'
            ,'الأربعاء','الخميس','الجمعة','السبت');
            $monarabic=array('','يناير','فبراير','مارس',
            'أبريل','مايو','يونيو','يوليو'
            ,'أغسطس','سبتمبر','أكتوبر','نوفمبر','ديسمبر');
            $date=getdate(time());
            echo $daysarabic[$date['wday']].' '.$date['mday'].' '.$monarabic[$date['mon']].' '.$date['year']/*.' الوقت الأن '.$date['hours'].':'.$date['minutes'].':'.$date['seconds']*/;
            }       

    }

我的控制器

    //arabic date
    $this->load->library('Common');
    $this->common->date_arabic();

这里它打印出我自己的函数中的数据,我尝试将其存储在 $data 中以将其传递给视图,

    //arabic date
    $this->load->library('Common');
    $data['date_arabic'] = $this->common->date_arabic();
    ...

    $this->load->view('home_page.php', $data);

然后 像这样当要查看时我只是输入

<?php echo $date_arabic ; ?>

但没有返回

i just created my own library on this folder (application/library) and following all steps to create individual library,

once i load this library in my controller it execute the function, but when trying to pass it to the view, nothing return

here is my code

MY OWN FUNCTION

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

    class Common {

            public function date_arabic()
            {
            $daysarabic=array('الأحد','الاثنين','الثلاثاء'
            ,'الأربعاء','الخميس','الجمعة','السبت');
            $monarabic=array('','يناير','فبراير','مارس',
            'أبريل','مايو','يونيو','يوليو'
            ,'أغسطس','سبتمبر','أكتوبر','نوفمبر','ديسمبر');
            $date=getdate(time());
            echo $daysarabic[$date['wday']].' '.$date['mday'].' '.$monarabic[$date['mon']].' '.$date['year']/*.' الوقت الأن '.$date['hours'].':'.$date['minutes'].':'.$date['seconds']*/;
            }       

    }

MY Controller

    //arabic date
    $this->load->library('Common');
    $this->common->date_arabic();

here it prints out the data in my own function, i tried to store this in a $data to pass it to the view like that

    //arabic date
    $this->load->library('Common');
    $data['date_arabic'] = $this->common->date_arabic();
    ...

    $this->load->view('home_page.php', $data);

then when going to view i just type

<?php echo $date_arabic ; ?>

but nothing returned

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

猫九 2024-12-20 17:39:12

在您的函数中,将最后一行从 this: 更改

echo $daysarabic[$date['wday']].' '.$date['mday'].' '.$monarabic[$date['mon']].' '.$date['year']/*.' الوقت الأن '.$date['hours'].':'.$date['minutes'].':'.$date['seconds']*/;

为:

return $daysarabic[$date['wday']].' '.$date['mday'].' '.$monarabic[$date['mon']].' '.$date['year']/*.' الوقت الأن '.$date['hours'].':'.$date['minutes'].':'.$date['seconds']*/;

In your function, change the last line from this:

echo $daysarabic[$date['wday']].' '.$date['mday'].' '.$monarabic[$date['mon']].' '.$date['year']/*.' الوقت الأن '.$date['hours'].':'.$date['minutes'].':'.$date['seconds']*/;

to this:

return $daysarabic[$date['wday']].' '.$date['mday'].' '.$monarabic[$date['mon']].' '.$date['year']/*.' الوقت الأن '.$date['hours'].':'.$date['minutes'].':'.$date['seconds']*/;
还在原地等你 2024-12-20 17:39:12
when you are writing libraries, you have to manually grab the Codeigniter instance like this

$CI =& get_instance();

then you would use $CI where you would normally use $this to interact with loaded codeigniter resources

so...

instead of

$this->input->post();
you would write

$CI->input->post();


EXAMPLE LIBRARY STRUCTURE

class Examplelib {

    // declare your CI instance class-wide private
    private $CI;

    public function __construct()
    {
        // get the CI instance and store it class wide
        $this->CI =& get_instance();
    }

    public function lib_function()
    {
        // use it here
        $this->CI->db->etc()
    }

    public function another_func()
    {
        // and here
        $this->CI->input->post();
    }

}
when you are writing libraries, you have to manually grab the Codeigniter instance like this

$CI =& get_instance();

then you would use $CI where you would normally use $this to interact with loaded codeigniter resources

so...

instead of

$this->input->post();
you would write

$CI->input->post();


EXAMPLE LIBRARY STRUCTURE

class Examplelib {

    // declare your CI instance class-wide private
    private $CI;

    public function __construct()
    {
        // get the CI instance and store it class wide
        $this->CI =& get_instance();
    }

    public function lib_function()
    {
        // use it here
        $this->CI->db->etc()
    }

    public function another_func()
    {
        // and here
        $this->CI->input->post();
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文