Posted June 17th, 2010 by M.Zeb Khan
Suppose you have an unordered list with so many list items and you want to call a JavaScript function for each list item, the days are gone when you would put an onlick() attribute for each list item, we can now perform a loop on list items without even touching them!
Here is our unordered list with list items:
Now let’s loop through the list and show an alert with the id of each item. Below is the Javascript code to achieve our goal, please note you must include jquery library in your page’s <head> tag for the script to work:
$('#menu li').each(function() {
alert(this.id);
});
Posted June 16th, 2010 by M.Zeb Khan
Have you ever wanted to make an array with same keys as the values? for example, we want an array of fruits.
$my_array = array('apple','orange','mango');
By default, when you don’t specify a key for each array element a default numeric key is assigned, the above be assigned key indexes as:
Array
(
[0] => apple
[1] => orange
[2] => mango
)
But that’s not actually what you want right? thanks to the built in PHP array function array_combine() we can combine the array values with the keys. Let’s try the following
$my_array = array('apple','orange','mango');
$my_array = array_combine($my_array,$my_array);
Now, let’s run a print_r($my_array) to see how it looks, the results will be:
Array
(
[apple] => apple
[orange] => orange
[mango] => mango
)
There we go! you can use this idea if you have a country list array with short country name for the array key, this happened to me when I had a country list array where the key for country Canada was CA. I just used array_combine() and everything worked like a charm!
Posted June 12th, 2010 by M.Zeb Khan
By default Zend Framework will escape data passed to the insert() method, For example if you have a textarea in your form that is accepting input from the user, and someone writes something like I’m testing this form, this will be inserted to the database as I\’m testing this form. We have a built-in function in PHP called stripslashes() that will strip the slashes from the record, but how about if you’re populating the form fields using the $form->populate() ? the best solution is use the setEscape() inside the controller’s init() method. Let’s try it,
public function init() {
$this->view->setEscape('stripslashes');
}
This will strip all the slashes from the view.
Posted June 12th, 2010 by M.Zeb Khan
I’m planning to take the Zend PHP5 Certification next month, I have been PHP Programming since 2004, but I still need to improve my weak areas such as PHP Patterns, Regular Expressions and XML/Web Services etc. Thanks to my best friend Wajid Khan for giving me the PHP Anthology book, I consider this the best book ever written about PHP, the 101 Essential Tips, tricks and hacks mentioned in this book are really useful.
I will keep posting my experience regarding my preparation for the exam.
Posted June 12th, 2010 by M.Zeb Khan
A quick way to remove numbers from a string in PHP,
$string = "This is my string, I also have 3823 9283";
$clean_string = str_replace(range(0,9),'',$string);
// This will return "This is my string, I also have"
echo $clean_string;