<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>bari&#039;s blog &#187; jQuery</title>
	<atom:link href="http://iambari.com/tag/jquery/feed/" rel="self" type="application/rss+xml" />
	<link>http://iambari.com</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Tue, 19 May 2009 12:34:30 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Parse Twitter search atom feed using PHP and jQuery</title>
		<link>http://iambari.com/2009/05/19/parse-twitter-search-atom-feed-using-php-and-jquery/</link>
		<comments>http://iambari.com/2009/05/19/parse-twitter-search-atom-feed-using-php-and-jquery/#comments</comments>
		<pubDate>Tue, 19 May 2009 12:34:30 +0000</pubDate>
		<dc:creator>Bari</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Twitter]]></category>

		<guid isPermaLink="false">http://iambari.com/?p=73</guid>
		<description><![CDATA[Many of us have twitter accounts and we all know twitter has an excellent search feature. Hundreds and thousands of tweeple are tweeting various topics every moment. Twitter gives us the facility to search in those tweets from outside using Search API&#8217;s search method. This method returns the results in 2 formats &#8211; json and [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;"><a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fiambari.com%2F2009%2F05%2F19%2Fparse-twitter-search-atom-feed-using-php-and-jquery%2F"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fiambari.com%2F2009%2F05%2F19%2Fparse-twitter-search-atom-feed-using-php-and-jquery%2F" height="61" width="51" /></a></div><p>Many of us have twitter accounts and we all know twitter has an excellent search feature. Hundreds and thousands of tweeple are tweeting various topics every moment. Twitter gives us the facility to search in those tweets from outside using Search API&#8217;s <a href="http://apiwiki.twitter.com/Twitter-Search-API-Method%3A-search">search</a> method. This method returns the results in 2 formats &#8211; json and atom. I will show how to parse the atom format of search results using PHP and how to display them using jQuery in this post.</p>
<h3>Parsing the Atom feed</h3>
<p>Lets take a look at a search feed&#8217;s source file :</p>
<pre class="brush: xml">
&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;feed xmlns:google=&quot;http://base.google.com/ns/1.0&quot; xml:lang=&quot;en-US&quot; xmlns:openSearch=&quot;http://a9.com/-/spec/opensearch/1.1/&quot; xmlns=&quot;http://www.w3.org/2005/Atom&quot; xmlns:twitter=&quot;http://api.twitter.com/&quot;&gt;
  &lt;id&gt;tag:search.twitter.com,2005:search/twitter&lt;/id&gt;
  &lt;link type=&quot;text/html&quot; rel=&quot;alternate&quot; href=&quot;http://search.twitter.com/search?q=twitter&quot;/&gt;
  &lt;link type=&quot;application/atom+xml&quot; rel=&quot;self&quot; href=&quot;http://search.twitter.com/search.atom?q=twitter&amp;lang=en&quot;/&gt;
  &lt;title&gt;twitter - Twitter Search&lt;/title&gt;
  &lt;link type=&quot;application/opensearchdescription+xml&quot; rel=&quot;search&quot; href=&quot;http://search.twitter.com/opensearch.xml&quot;/&gt;
  &lt;link type=&quot;application/atom+xml&quot; rel=&quot;refresh&quot; href=&quot;http://search.twitter.com/search.atom?lang=en&amp;q=twitter&amp;since_id=1855677271&quot;/&gt;
  &lt;twitter:warning&gt;adjusted since_id, it was older than allowedsince_id removed for pagination.&lt;/twitter:warning&gt;
  &lt;updated&gt;2009-05-20T03:54:29Z&lt;/updated&gt;
  &lt;openSearch:itemsPerPage&gt;15&lt;/openSearch:itemsPerPage&gt;
  &lt;openSearch:language&gt;en&lt;/openSearch:language&gt;
  &lt;link type=&quot;application/atom+xml&quot; rel=&quot;next&quot; href=&quot;http://search.twitter.com/search.atom?lang=en&amp;max_id=1855677271&amp;page=2&amp;q=twitter&quot;/&gt;
  &lt;entry&gt;
    &lt;id&gt;tag:search.twitter.com,2005:1855677271&lt;/id&gt;
    &lt;published&gt;2009-05-20T03:54:29Z&lt;/published&gt;
    &lt;link type=&quot;text/html&quot; rel=&quot;alternate&quot; href=&quot;http://twitter.com/nihongotako/statuses/1855677271&quot;/&gt;
    &lt;title&gt;Respond to tgis twitter with a challenge u want to see accomplished in the next video!
www.youtube.com/fitzner123&lt;/title&gt;
    &lt;content type=&quot;html&quot;&gt;Respond to tgis &lt;b&gt;twitter&lt;/b&gt; with a challenge u want to see accomplished in the next video!

&lt;a href=&quot;http://www.youtube.com/fitzner123&quot;&gt;www.youtube.com/fitzner123&lt;/a&gt;&lt;/content&gt;
    &lt;updated&gt;2009-05-20T03:54:29Z&lt;/updated&gt;
    &lt;link type=&quot;image/png&quot; rel=&quot;image&quot; href=&quot;http://s3.amazonaws.com/twitter_production/profile_images/217800128/Photo_13_normal.jpg&quot;/&gt;
    &lt;twitter:source&gt;&lt;a href=&quot;http://twitterfon.net/&quot;&gt;TwitterFon&lt;/a&gt;&lt;/twitter:source&gt;
    &lt;twitter:lang&gt;en&lt;/twitter:lang&gt;
    &lt;author&gt;
      &lt;name&gt;nihongotako (Fitzner123)&lt;/name&gt;
      &lt;uri&gt;http://twitter.com/nihongotako&lt;/uri&gt;
    &lt;/author&gt;
  &lt;/entry&gt;

...................................
other results are truncated
..................................

&lt;/feed&gt;
</pre>
<p>As we can see,  its an XML file. Every search result is inside an <strong>entry</strong> tag. We have to parse contents of the tags inside those entry tags. I used PHP&#8217;s <a href="http://www.php.net/curl">curl</a> and <a href="http://www.php.net/simplexml_load_string">simplexml_load_file</a> to load and parse the atom feed (of search results for &#8220;twitter&#8221;). Here is the file that does the works : search.php</p>
<pre class="brush: php">
&lt;?php
//URL encode the query string
$q = urlencode(&quot;twitter&quot;);

//request URL
$request = &quot;http://search.twitter.com/search.atom?q=$q&amp;lang=en&quot;;

$curl= curl_init();

curl_setopt ($curl, CURLOPT_RETURNTRANSFER, 1);

curl_setopt ($curl, CURLOPT_URL,$request);

$response = curl_exec ($curl);

curl_close($curl);

//remove &quot;twitter:&quot; from the $response string
$response = str_replace(&quot;twitter:&quot;, &quot;&quot;, $response);

//convert response XML into an object
$xml = simplexml_load_string($response);

//wrapping the whole output with &lt;result&gt;&lt;/result&gt;
echo &quot;&lt;results&gt;&quot;;

//loop through all the entry(s) in the feed
for($i=0;$i&lt;count($xml-&gt;entry);$i++)
{

	//get the id from entry
	$id = $xml-&gt;entry[$i]-&gt;id;

	//explode the $id by &quot;:&quot;
	$id_parts = explode(&quot;:&quot;,$id);

	//the last part is the tweet id
	$tweet_id = array_pop($id_parts);

	//get the account link
	$account_link = $xml-&gt;entry[$i]-&gt;author-&gt;uri;

	//get the image link
	$image_link = $xml-&gt;entry[$i]-&gt;link[1]-&gt;attributes()-&gt;href;

	//get name from entry and trim the last &quot;)&quot;
	$name = trim($xml-&gt;entry[$i]-&gt;author-&gt;name, &quot;)&quot;);

	//explode $name by the rest &quot;(&quot; inside it
	$name_parts = explode(&quot;(&quot;, $name);

	//get the real name of user from the last part
	$real_name = trim(array_pop($name_parts));

	//the rest part is the screen name
	$screen_name = trim(array_pop($name_parts));

	//get the published time, replace T and Z with &quot; &quot; and trim the last &quot; &quot;
	$published_time = trim(str_replace(array(&quot;T&quot;,&quot;Z&quot;),&quot; &quot;,$xml-&gt;entry[$i]-&gt;published));

	//get the status link
	$status_link = $xml-&gt;entry[$i]-&gt;link[0]-&gt;attributes()-&gt;href;

	//get the tweet
	$tweet = $xml-&gt;entry[$i]-&gt;content;

	//remove &lt;b&gt; and &lt;/b&gt; from the tweet. If you want to show bold keyword then you can comment this line
	$tweet = str_replace(array(&quot;&lt;b&gt;&quot;, &quot;&lt;/b&gt;&quot;), &quot;&quot;, $tweet);

	//get the source link
	$source = $xml-&gt;entry[$i]-&gt;source;

	//the result div that holds the information
	echo &#039;&lt;div class=&quot;result&quot; id=&quot;&#039;. $tweet_id .&#039;&quot;&gt;
			&lt;div class=&quot;profile_image&quot;&gt;&lt;a href=&quot;&#039;. $account_link .&#039;&quot;&gt;&lt;img src=&quot;&#039;. $image_link .&#039;&quot;&gt;&lt;/a&gt;&lt;/div&gt;
			&lt;div class=&quot;status&quot;&gt;
				&lt;div class=&quot;content&quot;&gt;
					&lt;strong&gt;&lt;a href=&quot;&#039;. $account_link .&#039;&quot;&gt;&#039;.$screen_name.&#039;&lt;/a&gt;&lt;/strong&gt; &#039;. $tweet .&#039;
				&lt;/div&gt;
				&lt;div class=&quot;time&quot;&gt;
					&#039;. $real_name .&#039; at &lt;a href=&quot;&#039;. $status_link .&#039;&quot;&gt;&#039;. $published_time .&#039;&lt;/a&gt; via &#039;. $source .&#039;
				&lt;/div&gt;
			&lt;/div&gt;
		&lt;/div&gt;&#039;;
}

echo &quot;&lt;/results&gt;&quot;;

?&gt;
</pre>
<p>When I parsed the XML feed into object using <strong>simplexml_load_file</strong> first time, I found its not loading the contents of twitter:source and twitter:lang tags.</p>
<pre class="brush: xml">
&lt;twitter:source&gt;&lt;a href=&quot;http://twitter.com/&quot;&gt;web&lt;/a&gt;&lt;/twitter:source&gt;
&lt;twitter:lang&gt;en&lt;/twitter:lang&gt;
</pre>
<p>So, I removed <strong>twitter:</strong> from these tag names :</p>
<pre class="brush: php">
//remove &quot;twitter:&quot; from the $response string
$response = str_replace(&quot;twitter:&quot;, &quot;&quot;, $response);
</pre>
<p>That means these tags now became :</p>
<pre class="brush: xml">
&lt;source&gt;&lt;a href=&quot;http://twitter.com/&quot;&gt;web&lt;/a&gt;&lt;/source&gt;
&lt;lang&gt;en&lt;/lang&gt;
</pre>
<p>Now simplexml_load_file nicely parsed them.</p>
<p><strong>These are the other extra works I did after parsing some useful data from each entry :</strong></p>
<p>This is the id inside every entry:</p>
<pre class="brush: xml">
&lt;id&gt;tag:search.twitter.com,2005:1846263500&lt;/id&gt;
</pre>
<p>We just need <strong>1846263500</strong> from it.</p>
<pre class="brush: php">
//get the id from entry
$id = $xml-&gt;entry[$i]-&gt;id;

//explode the $id by &quot;:&quot;
$id_parts = explode(&quot;:&quot;,$id);

//the last part is the tweet id
$tweet_id = array_pop($id_parts);
</pre>
<p>The content of name tag is processed to separate real name and screen name :</p>
<pre class="brush: xml">
&lt;name&gt;bauerbauerbauer (Danny Bauer)&lt;/name&gt;
</pre>
<p>in this part :</p>
<pre class="brush: php">
//get name from entry and trim the last &quot;)&quot;
$name = trim($xml-&gt;entry[$i]-&gt;author-&gt;name, &quot;)&quot;);

//explode $name by the rest &quot;(&quot; inside it
$name_parts = explode(&quot;(&quot;, $name);

//get the real name of user from the last part
$real_name = trim(array_pop($name_parts));

//the rest part is the screen name
$screen_name = trim(array_pop($name_parts));
</pre>
<p>The published tag contains time of this tweet was published :</p>
<pre class="brush: xml">
&lt;published&gt;2009-05-19T11:32:11Z&lt;/published&gt;
</pre>
<p>it is processed here to remove the T and Z from it :</p>
<pre class="brush: php">
//get the published time, replace T and Z with &quot; &quot; and trim the last &quot; &quot;
$published_time = trim(str_replace(array(&quot;T&quot;,&quot;Z&quot;),&quot; &quot;,$xml-&gt;entry[$i]-&gt;published));
</pre>
<p>I will describe the output part later.</p>
<h3>The display page index.php</h3>
<p>This is the simple page to display the search results &#8211; index.php</p>
<pre class="brush: html">
&lt;html&gt;
&lt;head&gt;
	&lt;title&gt;Twitter Search&lt;/title&gt;
	&lt;link href=&quot;style.css&quot; rel=&quot;stylesheet&quot; type=&quot;text/css&quot;&gt;
	&lt;script type=&quot;text/javascript&quot; src=&quot;http://code.jquery.com/jquery-latest.pack.js&quot;&gt;&lt;/script&gt;
	&lt;script src=&quot;search.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
	&lt;div id=&quot;main&quot;&gt;
		&lt;h2&gt;Search Results for &quot;Twitter&quot;&lt;/h2&gt;
		&lt;div id=&quot;update-alert&quot;&gt;&lt;/div&gt;
		&lt;div id=&quot;twitter-update&quot;&gt;&lt;/div&gt;
	&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>This page acts as the client side page for AJAX. It sends request to search.php and displays response result in the #twitter-update div.</p>
<h3>The AJAX part using jQuery &#8211; search.js</h3>
<p>We saw that search.php was used to manage the search atom feed and it will act as the server side page. This is the javascript file from where we will send request to search.php and process the response &#8211; search.js :</p>
<pre class="brush: javascript">
function loadSearch()
{
	//show message when the feed is loading
	$(&quot;#update-alert&quot;).text(&quot;Loading new results...&quot;);

	//ajax request to get the results from search.php
	$.ajax({
			url : &quot;search.php&quot;,
			success : function(results){

			//counter to count new results
			var counter = 0;

			//find each &#039;div&#039; with class &#039;result&#039;  from the response and loop through them
			$(results).find(&quot;div.result&quot;).each(function(i){

				//get the id of the div
				var div_id = $(this).attr(&quot;id&quot;);

				//check if any div with the same id already exists
				if( $(&quot;#twitter-update div#&quot; + div_id).length == 0 )
				{
					//if doesn&#039;t exist then prepend this div
					$(&quot;#twitter-update&quot;).prepend(this);

					//increase the counter
					counter++;
				}
			});

			//show the number of new results
			if(counter == 0)
				$(&quot;#update-alert&quot;).text(&quot;no new result&quot;);
			else if(counter == 1)
				$(&quot;#update-alert&quot;).text(&quot;1 new result&quot;);
			else
				$(&quot;#update-alert&quot;).text(counter + &quot; new results&quot;);
   		}
	});

}

$(document).ready(function(){
	loadSearch();
	setInterval(&quot;loadSearch()&quot;,30000);
});
</pre>
<p><strong>loadSearch()</strong> is the main thing here. This function mainly does all the works related to AJAX and display. This function is called first time when the page is loaded and after then its called every 30 seconds. Now lets see the response that is sent from search.php :</p>
<pre class="brush: php">
//wrapping the whole output with &lt;result&gt;&lt;/result&gt;
echo &quot;&lt;results&gt;&quot;;

//loop through all the entry(s) in the feed
for($i=0;$i&lt;count($xml-&gt;entry);$i++)
{
.....................................................
other codes are truncated
.....................................................

	//the result div that holds the information
	echo &#039;&lt;div class=&quot;result&quot; id=&quot;&#039;. $tweet_id .&#039;&quot;&gt;
			&lt;div class=&quot;profile_image&quot;&gt;&lt;a href=&quot;&#039;. $account_link .&#039;&quot;&gt;&lt;img src=&quot;&#039;. $image_link .&#039;&quot;&gt;&lt;/a&gt;&lt;/div&gt;
			&lt;div class=&quot;status&quot;&gt;
				&lt;div class=&quot;content&quot;&gt;
					&lt;strong&gt;&lt;a href=&quot;&#039;. $account_link .&#039;&quot;&gt;&#039;.$screen_name.&#039;&lt;/a&gt;&lt;/strong&gt; &#039;. $tweet .&#039;
				&lt;/div&gt;
				&lt;div class=&quot;time&quot;&gt;
					&#039;. $real_name .&#039; at &lt;a href=&quot;&#039;. $status_link .&#039;&quot;&gt;&#039;. $published_time .&#039;&lt;/a&gt; via &#039;. $source .&#039;
				&lt;/div&gt;
			&lt;/div&gt;
		&lt;/div&gt;&#039;;
}

echo &quot;&lt;/results&gt;&quot;;
</pre>
<p>When I first made this part it was not wrapped with any tag like results. There were only the .result divs and I simply populated the #twitter-update div with the response. But for many keywords twitter search  atom feed does not change that much within 30 seconds. Search API&#8217;s search method sends 15 results every time (you can load more results by adding extra <a href="http://apiwiki.twitter.com/Twitter-Search-API-Method%3A-search">parameters</a> in the feed request URL) and in case of a slow feed it is not a new set of 15 results every 30 seconds, may be 2-3 new results and 12-13  old results which are already loaded in the #twitter-update div. So, I had to find a way to handle these duplicate entries. I added $tweet_id as id in the .result divs. So, every .result div got a unique id.</p>
<pre class="brush: php">
echo &#039;&lt;div class=&quot;result&quot; id=&quot;&#039;. $tweet_id .&#039;&quot;&gt;
</pre>
<p>And also I wrapped all .result divs with results tag :</p>
<pre class="brush: php">
echo &quot;&lt;results&gt;&quot;;
.........................
15 .result divs each having unique id
.........................
echo &quot;&lt;/results&gt;&quot;;
</pre>
<p>So, now the whole response will act as XML and it can be handled with jQuery. Here is the part that does the trick :</p>
<pre class="brush: javascript">
//find each &#039;div&#039; with class &#039;result&#039;  from the response and loop through them
$(results).find(&quot;div.result&quot;).each(function(i){

	//get the id of the div
	var div_id = $(this).attr(&quot;id&quot;);

	//check if any div with the same id already exists
	if( $(&quot;#twitter-update div#&quot; + div_id).length == 0 )
	{
		//if doesn&#039;t exist then prepend this div
		$(&quot;#twitter-update&quot;).prepend(this);

		//increase the counter
		counter++;
	}
});
</pre>
<p>As you can see, this part finds every .result div from the response XML (results) and loops though them. Then it gets id of the div and finds if the #twitter-update div already has any div with that id. If it does not find any .result div with that id then it prepends that new .result div inside #twitter-update. Unfortunately, this part won&#8217;t work in IE. IE won&#8217;t let you parse the response XML! Good news is that all the other browsers are very friendly with this part and they will behave very well!</p>
<p>The #update-alert div shows &#8220;Loading new results&#8230;&#8221;  and messages based on number of new search results (counter).</p>
<pre class="brush: javascript">
//show message when the feed is loading
$(&quot;#update-alert&quot;).text(&quot;Loading new results...&quot;);
.........................................................
.........................................................

//show the number of new results
if(counter == 0)
	$(&quot;#update-alert&quot;).text(&quot;no new result&quot;);
else if(counter == 1)
	$(&quot;#update-alert&quot;).text(&quot;1 new result&quot;);
else
	$(&quot;#update-alert&quot;).text(counter + &quot; new results&quot;);
</pre>
<h3>CSS Stylesheet &#8211; style.css</h3>
<p>I added a simple stylesheet to beautify the search results in index.php. You can check this file from <a href="http://iambari.com/twit/style.css">here</a>.</p>
<p><img src="http://iambari.com/wp-content/uploads/2009/05/twitter-search.jpg" alt="twitter-search" title="twitter-search" width="579" height="536" class="aligncenter size-full wp-image-76" /></p>
<h3><a href="http://dl.getdropbox.com/u/761758/search.zip">Download the source files</a></h3>
<p>Well, that&#8217;s all. I hope you enjoyed it. I will be grateful if anyone comes up with a working solution for the IE problem. I am waiting for your comments.</p>
]]></content:encoded>
			<wfw:commentRss>http://iambari.com/2009/05/19/parse-twitter-search-atom-feed-using-php-and-jquery/feed/</wfw:commentRss>
		<slash:comments>19</slash:comments>
		</item>
		<item>
		<title>Create an Event calendar using PHP and jQuery</title>
		<link>http://iambari.com/2009/05/05/create-an-event-calendar-using-php-and-jquery/</link>
		<comments>http://iambari.com/2009/05/05/create-an-event-calendar-using-php-and-jquery/#comments</comments>
		<pubDate>Tue, 05 May 2009 11:01:40 +0000</pubDate>
		<dc:creator>Bari</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://iambari.com/?p=59</guid>
		<description><![CDATA[When I first saw this cool astonishing iCal-like calendars with jQuery by Stefano Verna I became a fan of it. Its a great looking calendar. But it is static, if you want to show another month or want to change the current date you have to change the html code inside. So, I thought it [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;"><a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fiambari.com%2F2009%2F05%2F05%2Fcreate-an-event-calendar-using-php-and-jquery%2F"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fiambari.com%2F2009%2F05%2F05%2Fcreate-an-event-calendar-using-php-and-jquery%2F" height="61" width="51" /></a></div><p>When I first saw this cool <a href="http://www.stefanoverna.com/log/create-astonishing-ical-like-calendars-with-jquery">astonishing iCal-like calendars with jQuery</a> by Stefano Verna I became a fan of it. Its a great looking calendar. But it is static, if you want to show another month or want to change the current date you have to change the html code inside. So, I thought it would be great if I can make this calendar dynamic using PHP &amp; MySQL. So, here is my attempt to make this great thing more useful.</p>
<h3>MySQL table creation</h3>
<p>First of all we have to create a new table named &#8216;eventcal&#8217; in MySQL. We can create a new database to contain this new table or can use any exisiting database. Here is the SQL code :</p>
<pre class="brush: sql">

CREATE TABLE eventcal (
id int(10) unsigned NOT NULL auto_increment,
eventDate date,
eventTitle varchar(100),
eventContent text,
PRIMARY KEY  (id)
)
</pre>
<h3>Add New Event Area</h3>
<p>Now we will make a simple panel to add new events to the &#8216;eventcal&#8217; table in MySQL.</p>
<p>For this part we will use Keith Wood&#8217;s <a href="http://keith-wood.name/datepick.html">jQuery Datepicker</a> plugin.</p>
<p><img src="http://iambari.com/wp-content/uploads/2009/05/addevent.jpg" alt="addevent" title="addevent" width="589" height="299" class="alignnone size-full wp-image-60" /></p>
<p>Here is the source code of the page to add new events &#8211; addevents.php:</p>
<pre class="brush: php">
&lt;?php
//Database connection details
$host = &quot;localhost&quot;;
$mysql_user = &quot;dbusername&quot;;
$mysql_password = &quot;dbpassword&quot;;
$mysql_db = &quot;dbname&quot;;

//make connection with mysql and select the database
$mysql_connect = mysql_connect($host, $mysql_user, $mysql_password);
$db_select = mysql_select_db($mysql_db);

//$alert will be used to show alert message for success or error report
$alert = &quot;&quot;;

//check if the form is submitted
if(isset($_POST[&#039;add&#039;]))
{
	//check for empty inputs
	if((isset($_POST[&#039;date&#039;]) &amp;&amp; !empty($_POST[&#039;date&#039;])) &amp;&amp; (isset($_POST[&#039;eventTitle&#039;]) &amp;&amp; !empty($_POST[&#039;eventTitle&#039;])) &amp;&amp; (isset($_POST[&#039;eventContent&#039;]) &amp;&amp; !empty($_POST[&#039;eventContent&#039;])))
	{
		//add new event to the database
		$query = &quot;INSERT INTO eventcal (`eventDate`,`eventTitle`,`eventContent`) VALUES(&#039;&quot;. $_POST[&#039;date&#039;] .&quot;&#039;,&#039;&quot;. addslashes($_POST[&#039;eventTitle&#039;]) .&quot;&#039;,&#039;&quot;. addslashes($_POST[&#039;eventContent&#039;]) .&quot;&#039;)&quot;;

		$result = mysql_query($query);

		//check if the insertion is ok
		if($result)
			$alert = &quot;New Event successfully added&quot;;
		else
			$alert = &quot;Something is wrong. Try Again.&quot;;
	}
	else
	{
		//alert message for empty input
		$alert = &quot;No empty input please&quot;;
	}
}
?&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Add New Events&lt;/title&gt;
&lt;script type=&quot;text/javascript&quot; src=&quot;http://code.jquery.com/jquery-latest.pack.js&quot;&gt;&lt;/script&gt;
&lt;link rel=&quot;stylesheet&quot; href=&quot;datepick/jquery.datepick.css&quot; type=&quot;text/css&quot; media=&quot;screen&quot; charset=&quot;utf-8&quot; /&gt;
&lt;script type=&quot;text/javascript&quot; src=&quot;datepick/jquery.datepick.pack.js&quot;&gt;&lt;/script&gt;
&lt;script type=&quot;text/javascript&quot;&gt;
$(document).ready(function(){

	//configure the date format to match mysql date
	$(&#039;#date&#039;).datepick({dateFormat: &#039;yy-mm-dd&#039;});
});
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
	&lt;form action=&quot;&lt;?php echo $_SERVER[&#039;PHP_SELF&#039;]; ?&gt;&quot; method=&quot;POST&quot;&gt;
	&lt;table align=&quot;center&quot;&gt;
		&lt;tr&gt;
			&lt;td colspan=&quot;2&quot;&gt;
				&lt;h2&gt;Add a New Event&lt;/h2&gt;
			&lt;/td&gt;
		&lt;/tr&gt;
		&lt;tr&gt;
			&lt;td&gt;Date : &lt;/td&gt;
			&lt;td&gt;&lt;input id=&quot;date&quot; name=&quot;date&quot; size=&quot;30&quot;&gt;&lt;/td&gt;
		&lt;/tr&gt;
		&lt;tr&gt;
			&lt;td&gt;Event Title : &lt;/td&gt;
			&lt;td&gt;&lt;input id=&quot;eventTitle&quot; name=&quot;eventTitle&quot; size=&quot;50&quot;&gt;&lt;/td&gt;
		&lt;/tr&gt;
		&lt;tr&gt;
			&lt;td&gt;Event Details : &lt;/td&gt;
			&lt;td&gt;&lt;textarea cols=&quot;40&quot; rows=&quot;5&quot; name=&quot;eventContent&quot; id=&quot;eventContent&quot;&gt;&lt;/textarea&gt;&lt;/td&gt;
		&lt;/tr&gt;
		&lt;tr&gt;
			&lt;td colspan=&quot;2&quot;&gt;&lt;input type=&quot;submit&quot; value=&quot;Add Event&quot; name=&quot;add&quot;&gt;&lt;/td&gt;
		&lt;/tr&gt;
	&lt;/table&gt;
	&lt;/form&gt;
&lt;?php
//check if there is any alert message set to $alert
if(isset($alert) &amp;&amp; !empty($alert))
{
	//message alert
	echo &#039;&lt;script type=&quot;text/javascript&quot;&gt;alert(&quot;&#039;.$alert.&#039;&quot;);&lt;/script&gt;&#039;;
}
?&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>I configured the date format of the datepicker plugin to match the MySQL <strong>date</strong> data type.</p>
<pre class="brush: javascript">

$(&#039;#date&#039;).datepick({dateFormat: &#039;yy-mm-dd&#039;});
</pre>
<p>MySQL <a href="http://dev.mysql.com/doc/refman/5.0/en/datetime.html"><strong>date</strong></a> data type stores date in YYYY-MM-DD format (2009-04-05).</p>
<h3>The Calendar</h3>
<p>We came to the last part, generate the dynamic calendar.  I did some little changes to the original calendar made by Stefano Verna &#8211; removed the weekdays from the tfoot and added links for month navigation, changed the popup position in coda.js ( mainly to handle the problem of popups hiding in the top for first days of the month ) and added a little bit of css in the master.css for the month navigation links in tfoot.</p>
<p><img src="http://iambari.com/wp-content/uploads/2009/05/test.jpg" alt="test" title="test" width="500" height="463" class="alignnone size-full wp-image-61" /></p>
<p>Here is the source code of the calendar page calendar.php</p>
<pre class="brush: php">
&lt;?php
//Database connection details
$host = &quot;localhost&quot;;
$mysql_user = &quot;dbusername&quot;;
$mysql_password = &quot;dbpassword&quot;;
$mysql_db = &quot;dbname&quot;;

//make connection with mysql and select the database
$mysql_connect = mysql_connect($host, $mysql_user, $mysql_password);
$db_select = mysql_select_db($mysql_db);

//check if time is set in the URL
if(isset($_GET[&#039;time&#039;]))
	$time = $_GET[&#039;time&#039;];
else
	$time = time();

$today = date(&quot;Y/n/j&quot;, time());

$current_month = date(&quot;n&quot;, $time);

$current_year = date(&quot;Y&quot;, $time);

$current_month_text = date(&quot;F Y&quot;, $time);

$total_days_of_current_month = date(&quot;t&quot;, $time);

$events = array();

//query the database for events between the first date of the month and the last date of month
$result = mysql_query(&quot;SELECT DATE_FORMAT(eventDate,&#039;%d&#039;) AS day,eventContent,eventTitle FROM eventcal WHERE eventDate BETWEEN  &#039;$current_year/$current_month/01&#039; AND &#039;$current_year/$current_month/$total_days_of_current_month&#039;&quot;);

while($row_event = mysql_fetch_object($result))
{
	//loading the $events array with evenTitle and eventContent wrapped with &lt;span&gt; and &lt;li&gt;. We will add them inside &lt;ul&gt; in later part
	$events[intval($row_event-&gt;day)] .= &#039;&lt;li&gt;&lt;span class=&quot;title&quot;&gt;&#039;.stripslashes($row_event-&gt;eventTitle).&#039;&lt;/span&gt;&lt;span class=&quot;desc&quot;&gt;&#039;.stripslashes($row_event-&gt;eventContent).&#039;&lt;/span&gt;&lt;/li&gt;&#039;;
}

$first_day_of_month = mktime(0,0,0,$current_month,1,$current_year);

//geting Numeric representation for the first day of the month. 0 (for Sunday) through 6 (for Saturday).
$first_w_of_month = date(&quot;w&quot;, $first_day_of_month);

//calculate how many rows will be in the calendar to show the dates
$total_rows = ceil(($total_days_of_current_month + $first_w_of_month)/7);

//trick to show empty cell in the first row if the month doesn&#039;t start from Sunday
$day = -$first_w_of_month;

$next_month = mktime(0,0,0,$current_month+1,1,$current_year);
$next_month_text = date(&quot;F \&#039;y&quot;, $next_month);

$previous_month = mktime(0,0,0,$current_month-1,1,$current_year);
$previous_month_text = date(&quot;F \&#039;y&quot;, $previous_month);

$next_year = mktime(0,0,0,$current_month,1,$current_year+1);
$next_year_text = date(&quot;F \&#039;y&quot;, $next_year);

$previous_year = mktime(0,0,0,$current_month,1,$current_year-1);
$previous_year_text = date(&quot;F \&#039;y&quot;, $previous_year);
?&gt;

&lt;html&gt;
&lt;head&gt;
&lt;title&gt;&lt;?=$current_month_text?&gt;&lt;/title&gt;
&lt;link rel=&quot;stylesheet&quot; href=&quot;css/master.css&quot; type=&quot;text/css&quot; media=&quot;screen&quot; charset=&quot;utf-8&quot; /&gt;
&lt;script type=&quot;text/javascript&quot; src=&quot;http://code.jquery.com/jquery-latest.pack.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;js/coda.js&quot; type=&quot;text/javascript&quot;&gt; &lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
	&lt;h2&gt;&lt;?=$current_month_text?&gt;&lt;/h2&gt;
	&lt;table cellspacing=&quot;0&quot;&gt;
		&lt;thead&gt;
		&lt;tr&gt;
			&lt;th&gt;Sun&lt;/th&gt;
			&lt;th&gt;Mon&lt;/th&gt;
			&lt;th&gt;Tue&lt;/th&gt;
			&lt;th&gt;Wed&lt;/th&gt;
			&lt;th&gt;Thu&lt;/th&gt;
			&lt;th&gt;Fri&lt;/th&gt;
			&lt;th&gt;Sat&lt;/th&gt;
		&lt;/tr&gt;
		&lt;/thead&gt;
		&lt;tr&gt;
			&lt;?php
			for($i=0; $i&lt; $total_rows; $i++)
			{
				for($j=0; $j&lt;7;$j++)
				{
					$day++;

					if($day&gt;0 &amp;&amp; $day&lt;=$total_days_of_current_month)
					{
						//YYYY-MM-DD date format
						$date_form = &quot;$current_year/$current_month/$day&quot;;

						echo &#039;&lt;td&#039;;

						//check if the date is today
						if($date_form == $today)
						{
							echo &#039; class=&quot;today&quot;&#039;;
						}

						//check if any event stored for this date in $events array
						if(array_key_exists($day,$events))
						{
							//adding the date_has_event class to the &lt;td&gt; and close it
							echo &#039; class=&quot;date_has_event&quot;&gt; &#039;.$day;

							//adding the eventTitle and eventContent wrapped with &lt;span&gt; and &lt;li&gt; to &lt;ul&gt;
							echo &#039;&lt;div class=&quot;events&quot;&gt;&lt;ul&gt;&#039;.$events[$day].&#039;&lt;/ul&gt;&lt;/div&gt;&#039;;
						}
						else
						{
							//if there is not event on that date then just close the &lt;td&gt; tag
							echo &#039;&gt; &#039;.$day;
						}

						echo &quot;&lt;/td&gt;&quot;;
					}
					else
					{
						//showing empty cells in the first and last row
						echo &#039;&lt;td class=&quot;padding&quot;&gt;&amp;nbsp;&lt;/td&gt;&#039;;
					}
				}
				echo &quot;&lt;/tr&gt;&lt;tr&gt;&quot;;
			}

			?&gt;
		&lt;/tr&gt;
		&lt;tfoot&gt;
			&lt;th&gt;
				&lt;a href=&quot;&lt;?=$_SERVER[&#039;PHP_SELF&#039;]?&gt;?time=&lt;?=$previous_year?&gt;&quot; title=&quot;&lt;?=$previous_year_text?&gt;&quot;&gt;&amp;laquo;&amp;laquo;&lt;/a&gt;
			&lt;/th&gt;
			&lt;th&gt;
				&lt;a href=&quot;&lt;?=$_SERVER[&#039;PHP_SELF&#039;]?&gt;?time=&lt;?=$previous_month?&gt;&quot; title=&quot;&lt;?=$previous_month_text?&gt;&quot;&gt;&amp;laquo;&lt;/a&gt;
			&lt;/th&gt;
			&lt;th&gt;&amp;nbsp;&lt;/th&gt;
			&lt;th&gt;&amp;nbsp;&lt;/th&gt;
			&lt;th&gt;&amp;nbsp;&lt;/th&gt;
			&lt;th&gt;
				&lt;a href=&quot;&lt;?=$_SERVER[&#039;PHP_SELF&#039;]?&gt;?time=&lt;?=$next_month?&gt;&quot; title=&quot;&lt;?=$next_month_text?&gt;&quot;&gt;&amp;raquo;&lt;/a&gt;
			&lt;/th&gt;
			&lt;th&gt;
				&lt;a href=&quot;&lt;?=$_SERVER[&#039;PHP_SELF&#039;]?&gt;?time=&lt;?=$next_year?&gt;&quot; title=&quot;&lt;?=$next_year_text?&gt;&quot;&gt;&amp;raquo;&amp;raquo;&lt;/a&gt;
			&lt;/th&gt;
		&lt;/tfoot&gt;
	&lt;/table&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>You can see the changes in tfoot from the source code.</p>
<p>This is the change I did in coda.js</p>
<pre class="brush: css">
popup.css({
		top: 20,
		left: -76,
		display: &#039;block&#039; // brings the popup back in to view
	})
</pre>
<p>I just changed the bottom:20 from the code original to top:20.</p>
<p>This is the extra snippet of CSS I added to the original master.css for month navigation links.</p>
<pre class="brush: css">

th a{
text-decoration:none;
font-size:120%;
font-weight:bold;
color: #000;
outline-width:0;
}
</pre>
<p>Harish Chauhan&#8217;s <a href="http://www.phpclasses.org/browse/package/2213.html">Calendar Events</a> Class on <a href="http://www.phpclasses.org/">phpclasses.org</a> helped me a lot. Thanks to him.</p>
<h3><a href="http://iambari.com/demo1/calendar.php">View the online demo</a> (only the calendar)</h3>
<h3><a href="http://dl.getdropbox.com/u/761758/eventcal.zip">Download the source files</a></h3>
<p>Well, that&#8217;s all. This is the first blog post of my life. I hope you enjoyed it.</p>
]]></content:encoded>
			<wfw:commentRss>http://iambari.com/2009/05/05/create-an-event-calendar-using-php-and-jquery/feed/</wfw:commentRss>
		<slash:comments>82</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic page generated in 0.312 seconds. -->
<!-- Cached page generated by WP-Super-Cache on 2010-07-19 15:30:41 -->
