0

我正在尝试将其集成到我的 PHP 项目中:https ://github.com/bastianallgeier/gantti

我已将 calendar.php 和 gantii.php 复制到应用程序/库中。

控制器:

<?php

class summary extends CI_Controller {

  function index() {
     $this->load->library('gantti');

     $gantti = new Gantti($data, array(
  'title'      => '',
  'cellwidth'  => 25,
  'cellheight' => 35,
  'today'      => true
));

    $this->load->view('summary_view', $gantti);

  }

}

看法:

<?php

require('lib/gantti.php'); 
require('controllers/summary.php'); 

date_default_timezone_set('UTC');
setlocale(LC_ALL, 'en_US');

?>

<!DOCTYPE html>
<html>
<head>  

  <title>Summary</title>
  <meta charset="utf-8" />  

  <link href="<?php echo base_url(); ?>assets/css/gantti.css" rel="stylesheet" media="screen">
  <link href="<?php echo base_url(); ?>assets/css/metro-bootstrap.css" rel="stylesheet" media="screen">

  <script type="text/javascript" src="<?php echo base_url(); ?>assets/js/jquery-1.10.2.js"></script>

</head>

<body>

<div class="page-header">
  <h1>Summary</h1>
</div>

<?php echo $gantti ?>

</body>

</html>

但我没有查看页面,而是收到如下错误(404):

A PHP Error was encountered
Severity: Warning
Message: Missing argument 1 for Gantti::__construct(), called in C:\xampp\htdocs\wwww\system\core\Loader.php on line 1099 and defined
Filename: libraries/gantti.php
Line Number: 18

A PHP Error was encountered
Severity: Notice
Message: Undefined variable: data
Filename: libraries/gantti.php
Line Number: 29


A PHP Error was encountered
Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: libraries/gantti.php
Line Number: 41

Fatal error: Call to a member function month() on a non-object in C:\xampp\htdocs\www\application\libraries\gantti.php on line 58
4

1 回答 1

2

load->library` 在 CI 中加载任何库它会自动调用它的构造函数 在这个库中发生同样的事情,但是你需要将至少一个参数传递给库,这就是它给出错误的原因,你只能传递一个参数给使用 ci 加载器的库

$this->load->library('gantti',$data);

但是你不能设置第二个参数来设置这个库但是这个库中的所有变量都是公共的你可以这样调用它们来设置它们

$this->gantti->options = array(
  'title'      => '',
  'cellwidth'  => 25,
  'cellheight' => 35,
  'today'      => true
)

完整的解决方案

$this->load->library('gantti',$data); //first load library and pass data
$this->gantti->options = array(
      'title'      => '',
      'cellwidth'  => 25,
      'cellheight' => 35,
      'today'      => true
    );
$data['gantti'] = $this->gantti->__toString();

另一种解决方案是创建一个自定义库并在库目录下使用 Gantti 库复制 gantti lib 目录对其进行扩展

自定义库

<?php
    require_once APPPATH.'libraries/lib/gantti.php';

    class cigantti extends Gantti {
        //put your code here

        public function __construct() {
        }

        public function generate($data = array(), $params = array()){
            parent::__construct($data, $params);
            return $this->render();
        }
    }

    ?>

在控制器中

$this->load->library('cigantti');
$data['gantti'] = $this->cigantti->generate($data, array(
                    'title' => 'Demo',
                    'cellwidth' => 25,
                    'cellheight' => 35,
                    'today' => true
                ));
于 2013-07-15T07:30:00.437 回答