28 January, 2011

Check value exists in multidimensional array

Most of the times we are playing with arrays in php. And it can be troublesome when we have to check for value in multidimensional array. I get into such a situation every often. I used to loop through the array to check for the existence of value.

So, i thought of making a function that checked for the existence of a value in a multi array. I am much happier using this function rather than looping through arrays making my time worse.

Following code simplifies checking of a value in multi-array in php.

//function to check if a values exists in multidimensional array
function in_multi($searchFor, $array) {
foreach($array as $key => $value) {
if($value == $searchFor) {
return true;
}
else {
if(is_array($value)) if(in_multi($searchFor, $value)) return true;
}
}
return false;
}
//test array
$testArr = array(
0 => array(
0 => array(
"10" => 20,
"20" => 40
),
1 => array(
"a" => 555,
"b" => 152
)
),
1 => array(
0 => 999,
1 => 2024
)
);


$isThere = in_multi(152, $testArr);
if($isThere) {
echo "Found";
}
else {
echo "Not Found";
}
?>
And we are done.

27 January, 2011

How to Sync php calendar events to Outlook 2007

I found lots of sites that showed how to sync events from my custom calendar to
Microsoft Outlook 2007 using PHP. But could not make any of it work my way. I
somehow managed to make it work. Microsoft Outlook 2007 uses iCal files, i.e.
Internet Calendar files, so with the help of some sites searched using Google
I created an iCal file adding my customs calendar events in it.
Here is the code snippet for the same.

$userTimeZoneName = "America/New_York";
$userTimeZoneAbbr = "GMT";
$eventSummary = "This is summary of event";

$eventStartDate = date("Ymd", time()); //like 20110112
$eventStartTime = date("His", time()); // like 163000
$eventEndDate = "20110128"; //you can set your own end date in Ymd format
$eventEndTime = "1223000"; //you can set your own end time in His format

$eventDescription = "Some event description here";
$eventsLocation = "Nepal, Kathmandu";
$eventId = "20"; //this can be your event id


$ical = "BEGIN:VCALENDAR\n";
$ical .= "PRODID:-//Sudhir/SudhirWebCal//NONSGML v1.0//EN\n";
$ical .= "VERSION:2.0\n";
$ical .="CALSCALE:GREGORIAN\n";
$ical .="METHOD:PUBLISH\n";
$ical .="X-WR-CALNAME:SudhirCal\n";
$ical .="X-WR-TIMEZONE:".$userTimeZoneName."\n";
$ical .="X-WR-CALDESC:Commonfig Events\n";
$ical .="X-PUBLISHED-TTL:PT5M\n";
$ical .="BEGIN:VTIMEZONE\n";
$ical .="TZID:".$userTimeZoneName."\n";
$ical .="X-LIC-LOCATION:".$userTimeZoneName."\n";
$ical .="BEGIN:DAYLIGHT\n";
$ical .="TZOFFSETFROM:+0000\n";
$ical .="TZOFFSETTO:+0100\n";
$ical .="TZNAME:".$userTimeZoneAbbr."\n";
$ical .="DTSTART:19700329T010000\n";
$ical .="RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU\n";
$ical .="END:DAYLIGHT\n";
$ical .="BEGIN:STANDARD\n";
$ical .="TZOFFSETFROM:+0100\n";
$ical .="TZOFFSETTO:+0000\n";
$ical .="TZNAME:GMT\n";
$ical .="DTSTART:19701025T020000\n";
$ical .="RRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU\n";
$ical .="END:STANDARD\n";
$ical .="END:VTIMEZONE\n";
$ical .="BEGIN:VEVENT\n";
$ical .="SUMMARY:".$eventSummary."\n";
$ical .="DTSTART:".$eventStartDate."T".$eventStartTime."\n";
$ical .="DTEND:".$eventEndDate."T".$eventEndTime."\n";
$ical .="DESCRIPTION;ENCODING=QUOTED-PRINTABLE:".str_replace("\r", "=0D=0A",$eventDescription)."\n";
$ical .= "LOCATION:".$eventsLocation."\n";
$ical .="UID:".sha1($eventStartDate.$eventId)."example.com\n";
$ical .="LAST-MODIFIED:20110119T123850Z\n";
$ical .="STATUS:PRIVATE\n";
$ical .="END:VEVENT\n";
$ical .="END:VCALENDAR";

//the line X-PUBLISHED-TTL:PT5M; sets the outlook calendar refresh time to 5 minutes
//so all your events will be automatically synchronized
//to your outlook calendar every 5 minutes t
//this can be increased to 2 hours like X-PUBLISHED-TTL:PT120M;

echo $ical;

//this will output the events in outlook calendar format
?>
The above code can be added inside a function and can be accessed using a link like
webcal://yourdomain.com/your_function_for_ical
In this way we can create calendar files for outlook 2007 and synchronize events of
custom PHP calendar with Microsoft Outlook 2007 calendar.
Hope it helps someone.

Coverflow effect with Jquery

I was searching for a Apple's like coverflow effect using jQuery, and got into the following post in stackoverflow.
Here's the link,
http://stackoverflow.com/questions/67207/apple-cover-flow-effect-using-jquery-or-other-library

Put it out with a perdurable