本文介绍了在Codeigniter中点击计数器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有下面的代码:(一步一步地)
- 在 APPPATH中放置 counter.txt 。 'logs / counter.txt'
- 在中设置 counter_helper.php APPPATH。 'helpers / counter_helper.php';
- 自动加载 APPPATH中新创建的帮助程序。 'config / autoload.php' file;
- 在 MY_Controller.php > APPPATH。 'core / MY_Controller.php'
- 任何控制器都应该扩展 MY_Controller 而不是 CI_Controller ;
- 在页面上使用< php echo $ this-> count_visitor;?> code>
助手:
<?php defined('BASEPATH')or exit('No direct script access allowed。');
if(!function_exists('count_visitor')){
function count_visitor()
{
$ filecounter =(APPPATH。'logs / counter.txt') ;
$ kunjungan = file($ filecounter);
$ kunjungan [0] ++;
$ file = fopen($ filecounter,'w');
fputs($ file,$ kunjungan [0]);
fclose($ file);
return $ kunjungan [0];
$ / code $ / pre
核心:
<?php if(!defined('BASEPATH'))exit('No direct script access allowed');
class MY_Controller扩展了CI_Controller
{
public $ count_visitor;
public function __construct()
{
parent :: __ construct();
$ this-> count_visitor = count_visitor();
}
}
/ *文件结尾MY_Controller.php * /
/ *位置:./application/core/MY_Controller.php * /
控制台:
< ;?php if(!defined('BASEPATH'))exit('No direct script access allowed');
class Home扩展MY_Controller {
public function index(){
$ data = array('isi'=>'home / index_home');
$ this-> load-> view('layout / wrapper',$ data);
}
}
查看:
<?php echo $ this-> count_visitor;?>
该代码返回如下错误:
解决方案当我加载帮助程序 $ this->时, load-> helper('counter');
application> core> MY_Controller.php
<?php if(!defined('BASEPATH'))exit('No direct script access allowed');
class MY_Controller扩展了CI_Controller
{
public $ count_visitor;
public function __construct()
{
parent :: __ construct();
$ this-> load-> helper('counter');
$ this-> count_visitor = count_visitor();
}
}
I have the code below:
(Step by step)
- Put counter.txt in APPPATH . 'logs/counter.txt'
- Make counter_helper.php set in APPPATH . 'helpers/counter_helper.php';
- Autoload newly created helper in APPPATH . 'config/autoload.php' file;
- Make MY_Controller.php in APPPATH . 'core/MY_Controller.php'
- Any controller should extend MY_Controller instead of CI_Controller;
- Echo it on page with: <?php echo $this->count_visitor;?>
The Helper :
<?php defined('BASEPATH') OR exit('No direct script access allowed.'); if ( ! function_exists('count_visitor')) { function count_visitor() { $filecounter=(APPPATH . 'logs/counter.txt'); $kunjungan=file($filecounter); $kunjungan[0]++; $file=fopen($filecounter, 'w'); fputs($file, $kunjungan[0]); fclose($file); return $kunjungan[0]; } }The Core :
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class MY_Controller extends CI_Controller { public $count_visitor; public function __construct() { parent::__construct(); $this->count_visitor = count_visitor(); } } /* End of file MY_Controller.php */ /* Location: ./application/core/MY_Controller.php */The Controller :
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class Home extends MY_Controller { public function index() { $data=array('isi' =>'home/index_home'); $this->load->view('layout/wrapper',$data); } }The View :
<?php echo $this->count_visitor;?>The code return an error like below :
解决方案I got it to work fine when I loaded the helper $this->load->helper('counter');
application > core > MY_Controller.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class MY_Controller extends CI_Controller { public $count_visitor; public function __construct() { parent::__construct(); $this->load->helper('counter'); $this->count_visitor = count_visitor(); } }
这篇关于在Codeigniter中点击计数器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!