Posts

Showing posts with the label Codeigniter

CodeIgniter Htaccess And URL Rewrite Issues

Answer : There are 3 steps to remove index.php . Make below changes in application/config.php file $config['base_url'] = 'http://'.$_SERVER['SERVER_NAME'].'/Your Ci folder_name'; $config['index_page'] = ''; $config['uri_protocol'] = 'AUTO'; Make .htaccess file in your root directory using below code RewriteEngine on RewriteCond $1 !^(index\.php|resources|robots\.txt) RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php/$1 [L,QSA] Enable the rewrite engine (if not already enabled) i. First, initiate it with the following command: a2enmod rewrite ii. Edit the file /etc/apache2/sites-enabled/000-default Change all AllowOverride None to AllowOverride All . Note: In latest version you need to change in /etc/apache2/apache2.conf file iii. Restart your server with the following command: sudo /etc/init.d/apache2 restart Your .htaccess is slightly o...

Codeigniter - Date Format - Form Validation

Answer : You can take use of CodeIgniters callback functions by creating a callback_date_valid() function that check if the date is valid. And to check if it is valid, you could use PHP's checkdate function array( 'field' => 'reg[dob]', 'label' => 'DOB', 'rules' => 'required|date_valid' ) function callback_date_valid($date){ $day = (int) substr($date, 0, 2); $month = (int) substr($date, 3, 2); $year = (int) substr($date, 6, 4); return checkdate($month, $day, $year); } I have a pretty clean solution for this. You can extend codeigniters form validation library. Do this by putting a MY_Form_validation.php file in your application/libraries folder. This file should look like this: class MY_Form_validation extends CI_Form_validation { public function __construct($rules = array()) { parent::__construct($rules); } public function valid_date($date) { $d = DateTime:...

Codeigniter Redirect -- The URI You Submitted Has Disallowed Characters

Answer : CodeIgniter checks all URI segments for disallowed characters. This happens by white listing allowed characters. Which ones are allowed can be checked in /system/application/config/config.php in the $config['permitted_uri_chars'] variable. permitted_uri_chars are the characters that CodeIgniter accepts in your URI.The default value is set to something like. $config['permitted_uri_chars'] = 'a-z 0-9~%.:&_\-'; By default only these are allowed: a-z 0-9~%.:_- Leave blank to allow all characters -- but only if you are insane. %22 comes for " .You can add this in permitted_uri_chars list. Try this may help but is not recommended , in your application/config/config.php change: $config['permitted_uri_chars'] = ''; #keep it blank to allow all characters $config['allow_get_array'] = TRUE; $config['enable_query_strings'] = TRUE;

CodeIgniter : Unable To Load The Requested File:

Answer : try $this->load->view('home/home_view',$data); (and note the " ' " not the " ‘ " that you used) File names are case sensitive - please check your file name. it should be in same case in view folder I error occor. When you are trying to access a file which is not in the director. Carefully check path in the view $this->load->view('path'); default root path of view function is application/view . I had the same error. I was trying to access files like this $this->load->view('pages/view/file.php'); Actually I have the class Pages and function. I built the function with one argument to call the any files from the director application/view/pages . I was put the wrong path. The above path pages/view/files can be used when you are trying to access the controller. Not for the view. MVC gave a lot confusion. I had this problem. I just solve it. Thanks.

Codeigniter CSRF Valid For Only One Time Ajax Request

Answer : In my opinion you should try to recreate your csrf token each request Try this code example... For the js funcion var csrfName = '<?php echo $this->security->get_csrf_token_name(); ?>', csrfHash = '<?php echo $this->security->get_csrf_hash(); ?>'; ("#avatar").change(function(){ var link = $("#avatar").val(); var dataJson = { [csrfName]: csrfHash, id: "hello", link: link }; $.ajax({ url : "<?php echo base_url('main/test'); ?>", type: 'post', data: dataJson, success : function(data) { csrfName = data.csrfName; csrfHash = data.csrfHash; alert(data.message); } }); }); and for the controller public function test() { $config['upload_path'] = './uploads/'; $config['allowed_types'] = 'gif|jpg|png'; $confi...

CodeIgniter: How To Do A Select (Distinct Fieldname) MySQL Query

Answer : $record = '123'; $this->db->distinct(); $this->db->select('accessid'); $this->db->where('record', $record); $query = $this->db->get('accesslog'); then $query->num_rows(); should go a long way towards it. You can also run ->select('DISTINCT `field`', FALSE) and the second parameter tells CI not to escape the first argument. With the second parameter as false , the output would be SELECT DISTINCT `field` instead of without the second parameter, SELECT `DISTINCT` `field` try it out with the following code function fun1() { $this->db->select('count(DISTINCT(accessid))'); $this->db->from('accesslog'); $this->db->where('record =','123'); $query=$this->db->get(); return $query->num_rows(); }

CodeIgniter - Best Place To Declare Global Variable

Answer : There is a file in /application/config called constants.php I normally put all mine in there with a comment to easily see where they are: /** * Custom defines */ define('blah', 'hello mum!'); $myglobalvar = 'hey there'; After your index.php is loaded, it loads the /core/CodeIgniter.php file, which then in turn loads the common functions file /core/Common.php and then /application/constants.php so in the chain of things it's the forth file to be loaded. I use a "Globals" class in a helper file with static methods to manage all the global variables for my app. Here is what I have: globals_helper.php (in the helpers directory) <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); // Application specific global variables class Globals { private static $authenticatedMemberId = null; private static $initialized = false; private static function initialize() { ...

Codeigniter: How To Include Javascript Files

Answer : You need to use the base_url() to include the javascript file in your VIEW. So, in the view_demo.php file: <script type="text/javascript" src="<?=base_url()?>js/jquery.js" ></script> <script type="text/javascript" src="<?=base_url()?>js/ajax.js" ></script> You will need the URL helper loaded. To load the helper you need to put on your demo.php controller: $this->load->helper('url'); You can also autoload on \config\autoload.php on the helpers array. More info about base_url(): http://www.codeigniter.com/user_guide/helpers/url_helper.html#base_url https://codeigniter.com/user_guide/general/styleguide.html#short-open-tags You wouldn't include JS files within the PHP, they would be output as script tags within the HTML you produce which you may be producing as output from the PHP script. As far as I know, there is no built in CodeIginiter function to include this outpu...

CodeIgniter 404 Page Not Found, But Why?

Answer : The cause of the problem was that the server was running PHP using FastCGI. After changing the config.php to $config['uri_protocol'] = "REQUEST_URI"; everything worked. You could try one of two things or a combination of both. Be sure that your controller's name starts with a capital letter. eg "Mycontroller.php" If you have not made any changes to your route, for some strange reason, you might have to include capital letters in your url. e.g if your controller is 'Mycontroller.php' with a function named 'testfunc' inside it, then your url will look like this: "http://www.yourdomain/index.php/Mycontroller/testfunc". Note the capital letter. (I'm assuming you haven't added the htaccess file to remove the 'index.php' part. If you have, just remove it from the url.) I hope this helps someone Leaving this answer here for others who ran into my situation. My codeigniter app was working fin...

CodeIgniter Select Query

Answer : Thats quite simple. For example, here is a random code of mine: function news_get_by_id ( $news_id ) { $this->db->select('*'); $this->db->select("DATE_FORMAT( date, '%d.%m.%Y' ) as date_human", FALSE ); $this->db->select("DATE_FORMAT( date, '%H:%i') as time_human", FALSE ); $this->db->from('news'); $this->db->where('news_id', $news_id ); $query = $this->db->get(); if ( $query->num_rows() > 0 ) { $row = $query->row_array(); return $row; } } This will return the "row" you selected as an array so you can access it like: $array = news_get_by_id ( 1 ); echo $array['date_human']; I also would strongly advise, not to chain the query like you do. Always have them separately like in my code, which is clearly a lot easier to read. Please also note that if you specify the table name...

Codeigniter Active Record Left Join

Answer : You have wrong where clause you need to compare user_id from your table ,you are comparing the id of email to the provided $user_id $CI->db->select('email'); $CI->db->from('emails'); $CI->db->where('user_id', $userid); $CI->db->join('user_email', 'user_email.user_id = emails.id', 'left'); $query = $CI->db->get(); A more useful way is to give aliases to your tables so the tables with same columns will not have any confusion $CI->db->select('e.email'); $CI->db->from('emails e'); $CI->db->join('user_email ue', 'ue.user_id = e.id', 'left'); $CI->db->where('ue.user_id', $userid); $query = $CI->db->get();