About a month ago I started working with the CodeIgniter framework. It’s the first framework I have worked with and frankly, I love it!
So I was looking to add a second parameter to a form_validation callback. Here’s what I found:
$this->form_validation('user_name', 'name', "callback_test_name[$param2]");
and the callback:
test_name($name, $param2){
..// some logic to return
}
The form_validation class uses the form value as the first parameter, and you can pass the second parameter in the arguments as you would with any other rule references. (First snippet)
At first I was calling 2 validations. First the standard type of validation, then another validation on the same input. * BAD IDEA *
There were some issues with order of operations. I found it best to chain the whole lot together. Like so:
$this->form_validation('user_name', 'name', "required|min_length[10]|callback_test_name[$param2]");
I hope that helps anybody with the same question.
I was on the hunt not only to find the different variations each courier had for their tracking numbers but also a regex to match. Most of the google fu I found was outdated. Fedex “recently” (months? years? days?) changed from a 12 digit to 15 digit system. And NO, tracking numbers are not totally random. There is usually a space separation on the printed labels you see. Each of the spaced out subsets have a meaning to the courier as well as a checksum. Checksum being, a pre selected sequence of particular number positions added together then divided by a pre selected number. That you can search up yourself if you like. I didn’t find all too much on that matter either.
I am not very good with regex so if there are any suggestions by l33t coderz, they are more than welcome. On with the codes:
/****
[ UPS ]
9 digits, or 1Z+whatever digits
The quick brown fox 1Z9999W99999999999 jumps over the lazy dog.
*/
$ups = '/(\b\d{9}\b)|(\b1Z\d+\b)/';
/****
[ Fedex ]
12 digits, 15 digits, or '96'+ 20 digits
The quick brown fox 999999999999 jumps over the lazy dog.
*/
$fedex = '/(\b96\d{20}\b)|(\b\d{15}\b)|(\b\d{12}\b)/';
/***
[ USPS ]
30 digits, '91'+20 digits, 20 digits (untested)
< TOTALLY UNTESTED BY ME AT THIS TIME >
*/
$usps = '/(\b\d{30}\b)|(\b91\d+\b)|(\b\d{20}\b)/';
I did get the common characters per courier from Packagetrackr. I don’t know how much of it is accurate, but from what I could tell from my experience with FedEx and UPS it seemed to be in line. I was unable to find an all inclusive source.
I found myself working with text files a little more recently and was looking for a way to tell if a directory was empty. Why? In my case I wanted to check if a directory was empty. If it wasn’t, to grab the data in those files and put them in a database.
What I didn’t want to do was have the expense of connecting to a database if the directory didn’t have any files in it to begin with. After chatting with my good friend TDavid at php-scripts.com, I decide to run with his suggestion…
Fill (or don’t fill) a variable with information if there were files in the directory. Then test the var to confirm or deny if there are files in the directory. It’s really a small bit of code…
//----- Check if ticket dir has files. If it has files,
// set a variable to hold the list of file names.
$dir = "../path/to/my/textfiles"; // set directory
if($handle = opendir($dir)){ // open directory
while(($file = readdir($handle)) !== false){
if($file != "." && $file != ".."){
$file_list[] = $file; // Set file list variable
}
}
closedir($handle); // Close directory
}
If there are files in this directory, the variable $file_list would not only exist, but also contain files. For example…
Array
(
[0] => 195972.txt
[1] => 196027.txt
[2] => 196053.txt
[3] => 196067.txt...
)
If there aren’t any files in this directory, the variable $file_list would not even exist because no $file would be inserted into it. At this point we can check/ test the variable to output or do further processing…
if(isset($file_list)){
echo "This dir has files!";
// perform further processing
// eg: collect each files contents
} else {
echo "This dir has NO files!";
// stop processing
// Not much do do with this since there are no files
}
Of course there are a few ways of checking to see if a directory is empty, but this way seemed to be the most simplistic to me. If you wanted to you could turn the entire lot into a function for reuse, and less script code clutter.
Comments? Questions?
php, empty directory
I came across a situation where I needed to pull records from 1 db to another on a recurring basis. I read many different ways to do it via searching around the net. Some of the suggestions included creating a temporary table and copying it over. What was lacking was that sometimes the new table also needed to be UPDATED, as the information changed from the original table.
I’ve come up with a dirty little example to show this can be done. There are more keywords that can enhance the ON DUPLICATE KEY UPDATE function even more. The Example is assuming an “employees” table that looks like so:
| employees |
| id (PK) |
| first_name |
For brevity, I’ve left out connection data etc;
$myArray = array(0 => array('id' => '1', 'fname' => 'Steve'),
array('id' => '2', 'fname' => 'sara'),
array('id' => '3', 'fname' => 'Matt')
);
// Don't forget to validate & clean your data
// Connect to db here
foreach($myArray as $key => $value){
$query = "INSERT INTO employees
(id, first_name) VALUES ('$value[id]', '$value[fname]')
ON DUPLICATE KEY UPDATE first_name = '$value[fname]'";
$result = mysql_query($query);
if(!$result){
print("Problem: " . mysql_error() . "");
} else {
print("Success !");
}
}
Read the manual on this for more information. This snippet will INSERT if a UNIQUE (in this case ‘id’) does not exist. It will UPDATE any existing unique. In this case any existing ‘id’s, the ‘first_name’ column will get updated.
One could also use the REPLACE function. As I understand the difference, REPLACE will DELETE any matching uniques, and INSERT a new record in it’s place.
Comments? Better ways? I’m always up for learning something new so please chime in ;-)
This may seem simple enough to many programmers out there but this threw me for a few days. This is how you install imagick on a windows server. If your site is on a shared host, you need to ask your host to install the extension for you.
For this example I am running php 5.2.4 on Abyss/Apache web server
Download the matching pecl5 Binary from php.net. In this case its pecl5.2-win32-200710121230.zip (matching my current php version).
Unpack the file.
Copy the php_imagick.dll into php/ext/ directory

Open your php.ini file and look for the “Dynamic Extensions” area. In there will be a list of items (some commented out). Add the following line to the bottom of the list: extension=php_imagick.dll

Save the changes you made.
Restart your web server.
If these steps were successful imagick will show up on any php page using the phpinfo() function.

imagick, pecl-extension, php-extension