Saturday, July 31, 2010

How to loop through a php assoative array

Looping through a php assoative array is really easy with foreach loop.

<?php

$array = array ("Fruit" => "Apple", "Animal" => "Dog", "Number" => 1, "Name" => "Jhon" );

foreach($array as $key => $value)
{
    echo $key.': '.$value;
    echo '<br />';
}

/* result

Fruit: Apple
Animal: Dog
Number: 1
Name: Jhon

*/

?>

Thursday, July 29, 2010

jquery datepicker with current date and custom date format

I searched the net to find a more simpler solution to set the current date with jQuery UI datepicker but didn't find anything. So here is the solution I used. Mixture of both normal JavaScript and jQuery. You can also check out how to use a custom date format with jQuery UI datepicker.

var currentTime = new Date();
var month = currentTime.getMonth() + 1;
var day = currentTime.getDate();
var year = currentTime.getFullYear();
var d = year + "-" + month + "-" + day;

$('.date-pick').datepicker({ dateFormat: 'yy-mm-dd' }).val(d);

Wednesday, July 21, 2010

How to get number of characters of a String in php - strlen()

You can use strlen() to easily find the length of an php string.

<?php
$str = 'Hello World';
echo strlen($str); // 11
?>

Many new php developers make the mistake of using count() which is used to count elements in an array or properties in an object.

Saturday, July 17, 2010

Get Select Box Value - jQuery Tip

It is easy as 1,2,3 with jQuery :)

$('#selectbox').val()

Lets see how to write this with on change event

$('#selectbox').change(function(){
     alert($(this).val());
});

Lets see how to get the selected text (not the value)

$('#selectbox').change(function(){
     alert($('#selectbox :selected').text());
});