Php 我能';t加载我创建的库-致命错误:调用未定义的函数CI
我创建了名为Kategori.php的库文件,但不知何故,我无法加载我创建的库 以下是库代码:Php 我能';t加载我创建的库-致命错误:调用未定义的函数CI,php,codeigniter,Php,Codeigniter,我创建了名为Kategori.php的库文件,但不知何故,我无法加载我创建的库 以下是库代码: class Kategori { public function panggil_kategori($id_jenis) { $CI =& get_instance(); $CI->load->model('ticketing_model'); $idjenis = $CI->ticketing_model->getone_kategor
class Kategori {
public function panggil_kategori($id_jenis)
{
$CI =& get_instance();
$CI->load->model('ticketing_model');
$idjenis = $CI->ticketing_model->getone_kategori($id_jenis);
return $idjenis;
}
}
以下是控制器代码:
public function browse_ticketing()
{
$this->load->library('kategori');
$this->data['data']=$this->ticketing_model->get_all();
$this->data['body']='data_ticketing';
$this->load->view('welcome_ticketing',$this->data);
}
以下是查看代码:
<table class="table table-condensed table-bordered table-hover table-striped" border="1">
<h4>Open</h4>
<tr>
<th>No</th>
<th>Judul</th>
<th>Kategori</th>
<th>Prioritas</th>
</tr>
<?php
$no = 1;
foreach($data as $row){
?>
<tr>
<td><?php echo $no++ ?></td>
<td><?php echo $row['judul'] ?></td>
<td><?php echo panggil_kategori($row['id_jenis']); ?></td>
<td><?php echo $row['prioritas'] ?></td>
</tr>
<?php
}
?>
当我尝试运行代码时,答案是致命错误:在第18行的C:\xampp\htdocs\helpdesk\application\views\data\u ticketing.php中调用未定义的函数panggil_kategori()是类的方法kategori
。您必须创建Kategori类的对象才能使用它:
$myKategori = new Kategori();
然后
我看不到您在哪里实例化了Kategori。不知道为什么要使用库进行此操作,因为您只是从数据库中获取数据,然后可以使用一些连接条件来获取记录并传递到视图
如果您想使用库,那么我建议在这里使用helper,因为helper最适合视图。您的库正在加载中。问题在于您试图访问库中方法的视图中。试图在视图中访问您的库是一种不良做法。我建议您将方法的返回值分配给控制器中的一个变量,然后您可以在视图中使用它 因此,在控制器中,您希望执行以下操作
$this->data['return'] = $this->kategori->panggil_kategori($value);
那么在你看来你可以做什么
<td><?php echo $return; ?></td>
$this->data['return'] = $this->kategori->panggil_kategori($value);
<td><?php echo $return; ?></td>