I'm trying to do a web counter, that count number who visit my website.
I use a txtfile as a counter, however when I start loading my web there was an error encounter.
Message: file_put_contents() [function.file-put-contents]: Filename cannot be empty
<?php if(! defined('BASEPATH')) exit('No direct script access allowed');
class Login extends CI_Controller{
function _construct(){
parent::__construct();
}
function index(){
//Function Web Counter (count visitor)
$filename = 'counterePOD.txt';
function inc_count() {
global $filename;
if (file_exists($filename)){
$current_value = file_get_contents($filename);
}else{
$current_value = 0;
}
file_put_contents($filename, ++$current_value);
}
inc_count();
}
$this->load->view('login_view');
}
}
?>
Somebody can tell what is the exact meaning of the error msg: "Filename cannot be empty"??
You should use a more OOP approach and pass the filename to the inc_count function:
<?php if(! defined('BASEPATH')) exit('No direct script access allowed');
class Login extends CI_Controller{
function _construct(){
parent::__construct();
}
function inc_count($filename) {
if (file_exists($filename)){
$current_value = file_get_contents($filename);
}else{
$current_value = 0;
}
file_put_contents($filename, ++$current_value);
}
function index(){
//Function Web Counter (count visitor)
$this->inc_count('counterePOD.txt');
$this->load->view('login_view');
}
}
?>
Pass the filename as an argument of the function like this. using global variable is not a good practice..
//Function definition
function inc_count($filename) {
if (file_exists($filename)){
$current_value = file_get_contents($filename);
}else{
$current_value = 0;
}
file_put_contents($filename, ++$current_value);
}
//Function call
$filename = 'counterePOD.txt';
inc_count($filename);
Related
I am trying to load model in my view file but problem is model is not loading and given error Message: Undefined property: CI_Loader::$CoupanCatModel
Following is my code
Controller
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class CoupanCategory extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('coupans/CoupanCatModel');
//$this->load->model('ForeignData_model');
}
public function init(){
$logoUrls = 'no-image.jpg';
$webUrls = 'no-image.jpg';
$mobileUrls = 'no-image.jpg';
$this->load->library('upload');
$config['upload_path'] = './assets/images/coupans/category/';
$config['allowed_types'] = 'gif|jpg|png';
$config['encrypt_name'] = TRUE;
$config['max_size'] = 20000;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$this->upload->initialize($config);
if ( ! $this->upload->do_upload('ccImage')){
$error = array('error' => $this->upload->display_errors());
}
else{
$data = $this->upload->data();
$logoUrls = $data['file_name'];
}
$is_init = $this->CoupanCatModel->init($logoUrls);
if ($is_init) {
redirect(base_url().'rech/'.ACCESS_KEY.'/coupans/coupan_category?create=success&success=New coupan category create','refresh');
}
else
{
redirect(base_url().'rech/'.ACCESS_KEY.'/coupans/coupan_category?create=failed&error=something want wrong','refresh');
}
}
}
Model
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class CoupanCatModel extends CI_Model {
public function init($logoUrls)
{
new_id:
$sc_id = 'SC'.mt_rand(1000,9999);
if ($this->check_id($sc_id))
goto new_id;
$sc_title = (isset($_POST['coupan_cat_name']) && $_POST['coupan_cat_name'] != '') ? trim($_POST['coupan_cat_name']):'';
$sc_logo = $logoUrls;
// $sc_web_image = $webUrls;
//$sc_mobile_image = $mobileUrls;
$active_flag =1;
$this->db->set('oc_cat_id',$sc_id);
$this->db->set('oc_cate_name',$sc_title);
//$this->db->set('sc_description',$sc_description);
// $this->db->set('sc_logo',$sc_logo);
$this->db->set('oc_web_image',$sc_logo);
//$this->db->set('sc_mobile_image',$sc_mobile_image);
$this->db->set('active_flag',$active_flag);
$this->db->insert('tbl_oc_category');
return true;
}
public function get_data($oc_cat_id = false)
{
if ($oc_cat_id != false) {
$this->db->where('oc_cat_id', $sc_id);
$query = $this->db->get('tbl_oc_category');
return $query->row_array();
}
$query = $this->db->get('tbl_oc_category');
return $query->result_array();
}
}
View i just load model like
<?php
$this->load->model('coupans/CoupanCatModel');
$scData = $this->CoupanCatModel->get_data();
echo '<pre>ddsd';
print_r($records);
die();
?>
But problem is model is not loading and given error
A PHP Error was encountered
Severity: Notice
Message: Undefined property: CI_Loader::$CoupanCatModel
Filename: coupans/coupan_category.php
Line Number: 139
For your own reference, you might want to read the Codeigniter Style Guide for filenames etc.
The code you have provided appears to be "dubious" in the fact there is no where in your code where you are loading the alleged "view".
So based upon what you have provided, I've come up with some Demo/Debug code.
Just note that your use of calling models inside views isn't quite correct in the scheme of MVC, nor is it entirely illegal.
So I've set this up on a Linux machine where filenames etc are case sensitive.
controllers/coupans/CoupanCategory.php
class CoupanCategory extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('coupans/CoupanCatModel');
}
public function index() {
echo "I am the controller ".__CLASS__;
$this->CoupanCatModel->init();
$this->load->view('coupans/coupan_category');
}
}
models/coupans/CoupanCatModel.php
class CoupanCatModel extends CI_Model{
public function init($logoUrls = '')
{
echo "<br>I am the ".__METHOD__;
}
public function get_data($oc_cat_id = false)
{
return "<br>I am the ".__METHOD__;
}
}
views/coupans/coupan_category.php
<?php
$this->load->model('coupans/CoupanCatModel');
$scData = $this->CoupanCatModel->get_data();
var_dump($scData);
And from out of all that you should get...
I am the controller CoupanCategory
I am the CoupanCatModel::init
string '<br>I am the CoupanCatModel::get_data' (length=37)
That should give you something to help solve your issue.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Controller extends CI_Controller
{
function __costruct(){
parent::__costruct();
$login = $this->session->userdata('login');
if(!empty($login)){
if($login!='valid'){
} else {
redirect('login/index');
}
} else{
redirect('login/index');
}
}
protected $template = array();
public function layout($arg = array()) {
$this->template['header'] = $this->load->view('theme/header_theme', $arg, true);
$this->template['header_menu'] = $this->load->view('theme/header_menu_theme', $arg, true);
$this->template['sidebar'] = $this->load->view('theme/sidebar_theme',$arg, true);
$this->template['content'] = $this->load->view($this->content, $arg, true);
$this->template['footer'] = $this->load->view('theme/footer_theme',$arg, true);
$this->load->view('theme/index_theme', $this->template);
}
If I am reading it right, and if that is the code used, the mistake is a simple spelling mistake.
function __costruct(){
parent::__costruct();
should have been
function __construct(){
parent::__construct();
Anyways, it is always better to use the server in development mode to check the same!!!
I am trying to learn code igniter and creating helper files. I have this as my functions_helper.php file located in my applications/helpers folder:
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
function __construct()
{
parent::__construct();
$this->load->model('user','',TRUE);
}
if (!function_exists('check_status')){
function check_status()
{
$CI = & get_instance();
$result=$CI->user->get_status(); //this is line 14!!
if($result)
{
$status_array=array();
foreach($result as $row)
{
$status_array=array(
'source' => $row->status,
'current' => $row->id
);
if ($status_array['current'] == 1) {
$status_array['current'] = "Live";
} else {
$status_array['current'] = "Amazon is Down";
}
$CI->session->set_userdata('status',$status_array);
}
return TRUE;
}
else
{
return false;
}
} // END OF CHECK_STATUS FUNCTION
}
From everything i can find, I am doing it correctly, yet I am getting this error:
PHP Fatal error: Call to a member function get_status() on a non-object in function_helper.php on line 14. exactly what am i doing wrong? Is this the best way to call a function? Ultimately I am trying to get information returned from a db query.
My get_status function is:
//FUNCTION TO SEE THE STATUS OF AMAZON
function get_status() {
$query = $this->db->query("select * from amz where active = 1");
if($query->num_rows()==1) {
return $query->row_array();
}else {
return false;
}
} //END OF GET_STATUS FUNCTION
Remove this code
function __construct()
{
parent::__construct();
$this->load->model('user','',TRUE);
}
There is no OO in helper files. You do not have a class -__construct() are used to initialize classes.
Then move
$this->load->model('user','',TRUE); to the check_status function
$CI =& get_instance();
$CI->load->model('user','','TRUE');
No idea what is the right way. But what I am trying to do is to make one helper function for positions list or dropdown (will set parameter for this) so that's how position list/dropdown can available to the entire system by writing single code `get_positions($type)'
The problem is not loading Model/Controller variables.
Error message:
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: positions
Filename: helpers/content_helper.php
Line Number: 14
Helper
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
// Position List
if ( ! function_exists('get_positions'))
{
function get_positions()
{
$CI =& get_instance();
$CI->load->model('admin/hr/hr_model');
if(count($positions)):
echo form_open();
$array = array();
foreach($positions as $position):
$label[] = $position->label;
$pos[] = $position->position;
endforeach;
echo form_dropdown('positions', array_combine($pos, $label));
echo form_close();
endif;
}
}
Model
<?php (defined('BASEPATH')) OR exit('No direct script access allowed');
class HR_Model extends MY_Model
{
protected $_table_name = 'positions';
protected $_order_by = 'label ASC';
public $rules = array();
public function get_new()
{
$position = new stdClass();
$position->position = '';
$position->label = '';
return $position;
}
public function get_positions($id = NULL, $single = FALSE)
{
$this->db->get($this->_table_name);
return parent::get($id, $single);
}
}
Controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class HR extends MX_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('admin/hr/hr_model');
}
public function index()
{
// Fetch all pages
$this->data['positions'] = $this->hr_model->get_positions();
// Load view
$this->load->view('hr/index', $this->data);
}
}
Eventually what I am looking is to create helper function which available to entire system by calling get_positions($type)
You have to define the $positions variable before if (count($positions)):. You should call the get_positions method from the Model.
I am getting error when calling the model from controller.
Error:- SCREAM: Error suppression ignored for
( ! ) Fatal error: Class 'Redirection_model' not found in C:\wamp\www\redirect\system\core\Loader.php on line 30
My controller redirection.php
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class redirection extends CI_Controller {
function __construct()
{
parent::__construct();
//$this->layout->setLayout('layouts/main');
}
public function redirection_url()
{
if($_GET){
$get_array = $_GET;
$targetId = $get_array['tagid'];
$this->load->model('redirection_model', 'redirection');
$dynamicurl = $this->redirection->getDynamicUrl($targetId);
if($dynamicurl){
$url=$dynamicurl['url'];
redirect($url,'refresh');
}
else{
echo "Invalid Request";
}
}
else{
echo "please pass the tagid";
// $this->layout->view('redirection/redirection_url');
}
}
}
redirection_model.php
<?
class redirection_model extends CI_Model {
function __construct()
{
parent::__construct();
}
function getDynamicUrl($targetId){
$dynamicUrl= array();
$query = $this->db->get_where('dynamic_url', array('tagid' => $targetId))->row();
if($query){
$dynamicUrl['url'] = $query->url;
$dynamicUrl['userid'] = $query->userid;
return $dynamicUrl;
}
else{
return false;
}
}
}
?>
you have not linked your view in controller, insert it.
And Also check you route file which is in your config folder, there you have to make entry for for your functions and route for view.