我的控制器中有以下函数:
public function cached_icd10() {
/*
Get information from database and cache the information */
$this->load->driver('cache', array('adapter' => 'file'));
$cacheID = "icd10";
if (!$cache_data = $this->cache->get($cacheID)) {
//ge information from the database
$data['icd10_codes'] = $this->icd_10_codes();
// Save into the cache for 5 minutes
$this->cache->save($cacheID, $data['icd10_codes'], 300);
$cache_data = $data['icd10_codes'];
}
return $cache_data;
}
public function form() {
//Fetch all ICD10 Codes from the cache file which is icd10
$cache_data = $this->cached_icd10();
$this->load->view('form', $cache_data);
}
第一个函数缓存数据库中的信息,第二个函数将信息传递给名为form的视图。当我尝试从我的视图输出信息时,它抛出一个错误: Message: Undefined variable: cache_data,而当我尝试从控制器输出信息时,它会很好地响应它。使用以下代码:
<?php
foreach ($cache_data as $value) {
echo 'Out put per line is ....: ' . $value['icd_description'] . ' and the Id is .... ' . $value['id'] . '<br>';
}
?>
如何将此信息从控制器显示到视图?
发布于 2015-05-11 19:39:23
您可以使用array
将缓存数据传递给视图
public function form() {
//Fetch all ICD10 Codes from the cache file which is icd10
$cache_data = $this->cached_icd10();
$data['cache_data']=$cache_data;// create and pass data to array
$this->load->view('form', $data);
}
您可以使用以下命令在视图中获取缓存数据
foreach ($cache_data as $value) {
// your code here
}
发布于 2019-05-09 03:34:35
$this->load->driver('cache',
array('adapter' => 'apc', 'backup' => 'file', 'key_prefix' => 'my_')
);
$this->cache->get('foo'); // Will get the cache entry named 'my_foo'
参考网址:https://www.codeigniter.com/userguide3/libraries/caching.html#example-usage
https://stackoverflow.com/questions/30166480
复制相似问题