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

时间:2011-11-03 11:31:45

标签: codeigniter

我刚刚在这个文件夹(应用程序/库)上创建了自己的库,并按照创建单个库的所有步骤进行操作,

一旦我在我的控制器中加载这个库它就会执行该函数,但是当试图将它传递给视图时,没有任何返回

这是我的代码

我自己的功能

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

这里它打印出我自己的函数中的数据,我试图将它存储在$ 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 ; ?>

但没有返回

2 个答案:

答案 0 :(得分:0)

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

}

答案 1 :(得分:0)

在您的函数中,更改此行的最后一行:

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']*/;