I created a JQuery Plugin, imValidateForm. As the name suggests, the plugin is used to validate forms. In this post, I will discuss how to use the imValidateForm plugin with CodeIgniter.
Archive for the ‘Codeigniter’ Category
One aspect of CodeIgniter that I don’t like is the amount of view files that I have to create. Although I create folders to group relevant views, I’m always searching through files to find the view that I need. To reduce the amount of files that I need for a project, I usually create a single view file to handle both inserts and updates to a table. But I had to create a CodeIgniter Helper function in order to make it work.
I’m updating a project that I created using CodeIgniter. I have to add a combobox (select) to a registration form that contains state names. I did not want to create a static combo box, so I created a states table in the MySQL database that I’m using for the project. The states table contains three fields (state_id, state_abbrev, state_name).
I’m updating a project for a client that I developed using CodeIgniter. One of the updates involves adding a text editor. I decided to use TinyMCE. Integrating TinyMCE with CodeIgniter was easier than I thought.
Create a folder under the js folder and name it tiny_mce (/js/tiny_mce). Download TinyMCE and place the files in the tiny_mce folder.
I just completed a website where the client uses PayPal to handle the payment transaction. I have used PayPal quite a bit in the past, but I have always had problems using IPN (Instant Payment Notification). Because of the problems, I usually opted not to use IPN, but I was determined to use it for this project. The primary problem with testing IPN is, because the transaction occurs in the background, it is difficult to figure out where the point of failure is. But that is all behind me now.
Using JQuery with CodeIgniter is simple. In fact, using most Ajax frameworks with CodeIgniter is easy. I created a simple JQuery plugin that displays randomly selected images (imBannerRotater).
First I use CodeIgniter’s ‘base_url’ function to link the necessary Javascript files in a main view of my application:
<script type="text/javascript" src="js/jquery/jquery-1.3.1.min.js"></script> <script type="text/javascript" src="js/jquery/jquery.imBannerRotater-0.5.js"></script> |
Next, I define a Javascript variable to be used with by the plugin:
<script type="text/javascript"> //<![CDATA[ base_url = '<?= base_url();?>'; //]]> </script> |
I then use this variable in my JQuery Plugin:
$(document).ready(function(){
$(".randPic").imBannerRotater({
data_url: base_url + 'assets/sidebar/sidebar.php',
base_path: base_url + 'assets/sidebar/'
});
}); |
That’s about it. While this is a simple example, using CodeIgniter’s base_url function is the key to using JQuery or any other Ajax Framework.
I just finished a project using CodeIgniter. I ftp’d the files to the host provider’s servers. When I attempted to view the website, I received a ‘500 Internal Server Error’. Viewing the error logs, I found: SoftException in Application.cpp:252: File “/home/arrowcol/public_html/index.php” is writeable by group. I looked at the permissions of the index.php file and found that it was set to 0666, so I changed it to 0755 and the site is now up and running.
I recently ran into a problem using CodeIgniter where I kept receiving the error: “Call to a member function on a non-object”. This problem occurred when I was trying to use a method from one model from within another model.
I have a model named mGalleryImages. Within this model, I have a method named addGalleryImages. Within this method, I load another model and tried to call a method from the loaded model:
function addGalleryImages(){
...
$this->load->model('mUtilImages');
...
$this->mUtilImages->setImageFile($_FILES['uploadFile']);
}
I receive the error when the setImageFile method is called. The problem is that when the model sees “$this”, it is looking for a method within the mGalleryImages model. The solution was to use a CodeIgniter function named get_instance().
function addGalleryImages(){
...
$CI =& get_instance();
$this->load->model('mUtilImages');
...
$CI->mUtilImages->setImageFile($_FILES['uploadFile']);
}
Easy enough, but I would never have thought that this was the problem. I played with this code for hours, trying to figure out why it didn’t work similarly to a regular PHP class. But now I know.
I often need to know the id of the last inserted record into a table. In the past, I’ve used a PHP function:
function getLastInserted($table, $id) { $query = "SELECT $id as maxID from $table where $id = LAST_INSERT_ID()"; $result = $this->runQuery($query); $row = mysql_fetch_row($result); return $row[0]; } |
To duplicate this using CodeIgniter’s Active Record, I could write:
function getLastInserted($table, $id) { $this->db->select_max($id); $Q = $this->db->get($table); $row = $Q->row_array(); return $row[$id]; } |
But, I learned that there is an even faster way that uses one of CodeIgniter’s Helper functions. Immediately after a record has been inserted into the database, I simply call:
$id = $this->db->insert_id(); |
Although, I am not sure how CodeIgniter knows which field is the primary key, it seems to work.
Before I begin to learn any new language, I generally purchase a book or two. A few months ago, I purchased the book, “Professional CodeIgniter”. This is a really good book in that I could copy/paste/tweak much of the code from the book for an application that I was working on. I learned the basics and then some (integrating Ajax, TinyMCE, etc). I highly recommend it.


