<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-5203032148063055732</id><updated>2012-02-14T02:24:12.048-08:00</updated><category term='jquery'/><category term='calendar'/><category term='javascript'/><category term='data/URI'/><category term='date'/><category term='html5'/><category term='quiz'/><category term='php'/><category term='ad management'/><category term='databases'/><title type='text'>PHP Tutorials</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://cool-php-tutorials.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5203032148063055732/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://cool-php-tutorials.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Ultralight Flyer</name><uri>http://www.blogger.com/profile/02349553397305809903</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>7</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-5203032148063055732.post-7827964989882467502</id><published>2012-01-12T09:33:00.000-08:00</published><updated>2012-01-12T09:33:37.891-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='calendar'/><category scheme='http://www.blogger.com/atom/ns#' term='php'/><category scheme='http://www.blogger.com/atom/ns#' term='date'/><title type='text'>Better PHP Calendar Class</title><content type='html'>I've dealt with creating monthly calendars in PHP and it has always been a bit of pain. How exactly to show the days in the month in a table without creating ugly and hard to maintain code? I believe this time I was able to find a good solution.&lt;br /&gt;&lt;br /&gt;&lt;h2&gt;A PHP Class&lt;/h2&gt;It's a PHP class that will output the dates of the given month in array. But in order to make the dates fit their place in the week, the class outputs zeros in the place of "empty" days. So the first week of a month starting in Wednesday would be this array:&lt;br /&gt;&lt;br /&gt;[0, 0, 1, 2, 3, 4, 5]&lt;br /&gt;&lt;br /&gt;This allows you to loop through the returned arrays without worrying where to start the week. If you use a table, you could simply output "&amp;nbsp;" when the date is 0 and output the number (and colored background) when the date is a number. Here is an example in HTML:&lt;br /&gt;&lt;br /&gt;&lt;h2&gt;Using the output in HTML:&lt;/h2&gt;&lt;br /&gt;&lt;code&gt;&lt;pre&gt;&amp;lt;table&amp;gt;&lt;br /&gt;    &amp;lt;tr&amp;gt;&amp;lt;th&amp;gt;Mon&amp;lt;/th&amp;gt;&amp;lt;th&amp;gt;Tue&amp;lt;/th&amp;gt;&amp;lt;th&amp;gt;Wed&amp;lt;/th&amp;gt;&amp;lt;th&amp;gt;Thu&amp;lt;/th&amp;gt;&amp;lt;th&amp;gt;Fri&amp;lt;/th&amp;gt;&amp;lt;th&amp;gt;Sat&amp;lt;/th&amp;gt;&amp;lt;th&amp;gt;Sun&amp;lt;/th&amp;gt;&amp;lt;/tr&amp;gt;&lt;br /&gt;    &amp;lt;?php for($i=1;$i&amp;lt;=$num_weeks;$i++):?&amp;gt;&lt;br /&gt;        &amp;lt;tr&amp;gt;&lt;br /&gt;            &amp;lt;?php $days=$_calendar-&amp;gt;days($m[0], $m[1], $i, $num_weeks);&lt;br /&gt;            foreach($days as $day):?&amp;gt;&lt;br /&gt;                &amp;lt;td&amp;gt;&amp;lt;?=$day?$day:&amp;quot;&amp;amp;nbsp;&amp;quot;?&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;            &amp;lt;?php endforeach;?&amp;gt;&lt;br /&gt;        &amp;lt;/tr&amp;gt;&lt;br /&gt;    &amp;lt;?php endfor;?&amp;gt;&lt;br /&gt;    &amp;lt;/table&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Easy, eh? We just output a table row for every week in the month ($num_weeks is also a method in the class as you'll see below) and for the days with date we output the date (otherwise just blank space).&lt;br /&gt;&lt;br /&gt;You can see how it works live in this &lt;a href="http://calendarscripts.info/free/period-calendar/"&gt;period calendar&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;&lt;h2&gt;The PHP Code&lt;/h2&gt;Now, here is how it all works. I created a class to hold 3 functions:&lt;br /&gt;&lt;br /&gt;&lt;b&gt;num_weeks($month, $year)&lt;/b&gt; - to calculate the number of weeks in the given month and year&lt;br /&gt;&lt;br /&gt;&lt;b&gt;first_day($month, $year)&lt;/b&gt; - to find out which day of the week is the first day of a given month.year&lt;br /&gt;&lt;br /&gt;&lt;b&gt;days($month, $year, $week, $num_weeks=0)&lt;/b&gt; - this is the main method that returns the array of 7 days. The week starts in Monday.&lt;br /&gt;&lt;br /&gt;Here is the full code below and some comments to it:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;pre&gt;function num_weeks($month, $year)&lt;br /&gt;    {&lt;br /&gt;        // every month has at least 4 weeks&lt;br /&gt;        $num_weeks=4;&lt;br /&gt;    &lt;br /&gt;        // finding where it starts&lt;br /&gt;        $first_day = $this-&gt;first_day($month, $year);  &lt;br /&gt;        &lt;br /&gt;        // if the first week doesn't start on monday &lt;br /&gt;        // we are sure that the month has at minimum 5 weeks&lt;br /&gt;        if($first_day!=1) $num_weeks++;&lt;br /&gt;        &lt;br /&gt;        // find the "widow" days (i.e. empty cells in the 1st week)&lt;br /&gt;        $widows=$first_day-1;  &lt;br /&gt;        $fw_days=7-$widows;&lt;br /&gt;        if($fw_days==7) $fw_days=0;       &lt;br /&gt;        &lt;br /&gt;        // number of days in the month&lt;br /&gt;        $numdays=date("t",mktime(2, 0, 0, $month, 1, $year));&lt;br /&gt;        &lt;br /&gt;        if( ($numdays - $fw_days) &gt; 28 ) $num_weeks++;&lt;br /&gt;         // that's it!&lt;br /&gt;        return $num_weeks;                  &lt;br /&gt;    }&lt;br /&gt;&lt;/pre&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Then the first_day function:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;pre&gt;function first_day($month, $year)&lt;br /&gt;    {&lt;br /&gt;        $first_day= date("w", mktime(2, 0, 0, $month, 1, $year));&lt;br /&gt;        if($first_day==0) $first_day=7; # convert Sunday&lt;br /&gt;        &lt;br /&gt;        return $first_day;&lt;br /&gt;    }&lt;br /&gt;&lt;/pre&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;And here is the most important one:&lt;br /&gt;&lt;code&gt;&lt;pre&gt;// here $week is the week number when we go in a loop&lt;br /&gt;// (see the html code that I posted earlier to get better idea)&lt;br /&gt;function days($month, $year, $week, $num_weeks=0)&lt;br /&gt;{&lt;br /&gt;        $days=array();&lt;br /&gt;&lt;br /&gt;        // this is just to avoid calling num_weeks every time &lt;br /&gt;        // when you loop through the weeks        &lt;br /&gt;        if($num_weeks==0) $num_weeks=$this-&gt;num_weeks($month, $year);&lt;br /&gt;        &lt;br /&gt;        // find which day of the week is 1st of the given month        &lt;br /&gt;        $first_day = $this-&gt;first_day($month, $year);&lt;br /&gt;                &lt;br /&gt;        // find widow days (first week)&lt;br /&gt;        $widows=$first_day-1;&lt;br /&gt;        &lt;br /&gt;        // first week days&lt;br /&gt;        $fw_days=7-$widows;&lt;br /&gt;        &lt;br /&gt;        // if $week==1 don't do further calculations&lt;br /&gt;        if($week==1)&lt;br /&gt;        {&lt;br /&gt;            for($i=0;$i&lt;$widows;$i++) $days[]=0;            &lt;br /&gt;            for($i=1;$i&lt;=$fw_days;$i++) $days[]=$i;            &lt;br /&gt;            return $days;&lt;br /&gt;        }&lt;br /&gt;        &lt;br /&gt;        // any other week&lt;br /&gt;        if($week!=$num_weeks)&lt;br /&gt;        {&lt;br /&gt;            $first=$fw_days+(($week-2)*7);&lt;br /&gt;            for($i=$first+1;$i&lt;=$first+7;$i++) $days[]=$i;            &lt;br /&gt;            return $days;&lt;br /&gt;        }&lt;br /&gt;        &lt;br /&gt;        &lt;br /&gt;        # only last week calculations below&lt;br /&gt;        &lt;br /&gt;        // number of days in the month&lt;br /&gt;        $numdays=date("t",mktime(2, 0, 0, $month, 1, $year));&lt;br /&gt;                &lt;br /&gt;        // find orphan days (last week)  &lt;br /&gt;        $orphans=$numdays-$fw_days-(($num_weeks-2)*7);                     &lt;br /&gt;        $empty=7-$orphans;&lt;br /&gt;        for($i=($numdays-$orphans)+1;$i&lt;=$numdays;$i++) $days[]=$i;&lt;br /&gt;        for($i=0;$i&lt;$empty;$i++) $days[]=0;&lt;br /&gt;        return $days;&lt;br /&gt;    }&lt;br /&gt;&lt;/pre&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;That's it! Any questions?&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5203032148063055732-7827964989882467502?l=cool-php-tutorials.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cool-php-tutorials.blogspot.com/feeds/7827964989882467502/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://cool-php-tutorials.blogspot.com/2012/01/better-php-calendar-class.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5203032148063055732/posts/default/7827964989882467502'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5203032148063055732/posts/default/7827964989882467502'/><link rel='alternate' type='text/html' href='http://cool-php-tutorials.blogspot.com/2012/01/better-php-calendar-class.html' title='Better PHP Calendar Class'/><author><name>Ultralight Flyer</name><uri>http://www.blogger.com/profile/02349553397305809903</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5203032148063055732.post-6897074609401581999</id><published>2011-05-19T09:24:00.000-07:00</published><updated>2011-05-20T07:41:44.012-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='javascript'/><category scheme='http://www.blogger.com/atom/ns#' term='jquery'/><category scheme='http://www.blogger.com/atom/ns#' term='html5'/><category scheme='http://www.blogger.com/atom/ns#' term='php'/><category scheme='http://www.blogger.com/atom/ns#' term='data/URI'/><title type='text'>Simple HTML5 Drawing App + Saving The Files With Ajax</title><content type='html'>This is a simple drawing board app that allows user draw on a canvas, pick several brush sizes and colors and save his image (I'll leave part of the last to you).&lt;br /&gt;&lt;br /&gt;You can see how this app works live on the &lt;a href="http://re.trotoys.com/drawarobot" target="_blank"&gt;Draw a Robot&lt;/a&gt; site. It uses HTML5 canvas and works in all modern browsers except &lt;span style="font-weight:bold;"&gt;Internet Explorer&lt;/span&gt;.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Some important parts of the javascript code are taken from &lt;a href="http://www.williammalone.com/articles/create-html5-canvas-javascript-drawing-app/" target=_blank&gt;this tutorial&lt;/a&gt;. William's app is more impressive.&lt;/b&gt; Mine is simpler and aims to explain it in understandable way (William's tutorial is a bit complex). Let's start:&lt;br /&gt;&lt;br /&gt;&lt;h2&gt;Creating the canvas&lt;/h2&gt;&lt;p&gt;&amp;lt;canvas id=&amp;quot;drawingCanvas&amp;quot; width=&amp;quot;550&amp;quot; height=&amp;quot;450&amp;quot; style=&amp;quot;border:1pt solid black;margin:auto;cursor:crosshair;clear:both;&amp;quot;&amp;gt;&lt;br /&gt;&amp;lt;/canvas&amp;gt;&lt;/p&gt;&lt;br /&gt;If you don't care about Internet explorer, that's it. We just add some styling to it and make the cursor crossed.&lt;br /&gt;&lt;br /&gt;&lt;h2&gt;Create the brush sizes and color selectors&lt;/h2&gt;&lt;br /&gt;&lt;b&gt;&amp;lt;div style=&amp;quot;float:left;&amp;quot;&amp;gt;Colors:&amp;lt;/div&amp;gt; &amp;lt;a href=&amp;quot;#&amp;quot; class=&amp;quot;colorPicker&amp;quot; onclick=&amp;quot;setColor('#FFF');return false;&amp;quot; style=&amp;quot;background:#FFF;&amp;quot;&amp;gt;&amp;amp;nbsp;&amp;lt;/a&amp;gt;&lt;br /&gt;&amp;lt;a class=&amp;quot;colorPicker&amp;quot; href=&amp;quot;#&amp;quot; onclick=&amp;quot;setColor('#000');return false;&amp;quot; style=&amp;quot;background:#000;&amp;quot;&amp;gt;&amp;amp;nbsp;&amp;lt;/a&amp;gt;&lt;br /&gt;&amp;lt;a class=&amp;quot;colorPicker&amp;quot; href=&amp;quot;#&amp;quot; onclick=&amp;quot;setColor('#FF0000');return false;&amp;quot; style=&amp;quot;background:#FF0000;&amp;quot;&amp;gt;&amp;amp;nbsp;&amp;lt;/a&amp;gt;&lt;br /&gt;&amp;lt;a class=&amp;quot;colorPicker&amp;quot; href=&amp;quot;#&amp;quot; onclick=&amp;quot;setColor('#00FF00');return false;&amp;quot; style=&amp;quot;background:#00FF00;&amp;quot;&amp;gt;&amp;amp;nbsp;&amp;lt;/a&amp;gt;&lt;br /&gt;&amp;lt;a class=&amp;quot;colorPicker&amp;quot; href=&amp;quot;#&amp;quot; onclick=&amp;quot;setColor('#0000FF');return false;&amp;quot; style=&amp;quot;background:#0000FF;&amp;quot;&amp;gt;&amp;amp;nbsp;&amp;lt;/a&amp;gt;&lt;br /&gt;&amp;lt;a class=&amp;quot;colorPicker&amp;quot; href=&amp;quot;#&amp;quot; onclick=&amp;quot;setColor('#FFFF00');return false;&amp;quot; style=&amp;quot;background:#FFFF00;&amp;quot;&amp;gt;&amp;amp;nbsp;&amp;lt;/a&amp;gt;&lt;br /&gt;&amp;lt;a class=&amp;quot;colorPicker&amp;quot; href=&amp;quot;#&amp;quot; onclick=&amp;quot;setColor('#00FFFF');return false;&amp;quot; style=&amp;quot;background:#00FFFF;&amp;quot;&amp;gt;&amp;amp;nbsp;&amp;lt;/a&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;amp;nbsp;&amp;lt;/div&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;div style=&amp;quot;float:left;&amp;quot;&amp;gt;Sizes:&amp;lt;/div&amp;gt; &lt;br /&gt;&amp;lt;a href=&amp;quot;#&amp;quot; class=&amp;quot;colorPicker&amp;quot; onclick=&amp;quot;setSize(2);return false;&amp;quot; style=&amp;quot;width:2px;height:2px;margin-left:15px;&amp;quot;&amp;gt;&amp;amp;nbsp;&amp;lt;/a&amp;gt;&lt;br /&gt;&amp;lt;a href=&amp;quot;#&amp;quot; class=&amp;quot;colorPicker&amp;quot; onclick=&amp;quot;setSize(5);return false;&amp;quot; style=&amp;quot;width:5px;height:5px;margin-left:15px;&amp;quot;&amp;gt;&amp;amp;nbsp;&amp;lt;/a&amp;gt;&lt;br /&gt;&amp;lt;a href=&amp;quot;#&amp;quot; class=&amp;quot;colorPicker&amp;quot; onclick=&amp;quot;setSize(10);return false;&amp;quot; style=&amp;quot;width:10px;height:10px;margin-left:15px;&amp;quot;&amp;gt;&amp;amp;nbsp;&amp;lt;/a&amp;gt;&lt;br /&gt;&amp;lt;a href=&amp;quot;#&amp;quot; class=&amp;quot;colorPicker&amp;quot; onclick=&amp;quot;setSize(25);return false;&amp;quot; style=&amp;quot;width:25px;height:25px;margin-left:15px;&amp;quot;&amp;gt;&amp;amp;nbsp;&amp;lt;/a&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;div style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;amp;nbsp;&amp;lt;/div&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;p style=&amp;quot;clear:both;&amp;quot;&amp;gt;&amp;lt;input type=&amp;quot;button&amp;quot; value=&amp;quot;Clear Canvas&amp;quot; onclick=&amp;quot;clearCanvas();&amp;quot;&amp;gt;&lt;br /&gt;&amp;lt;input type=&amp;quot;button&amp;quot; value=&amp;quot;Save My Drawing&amp;quot; onclick=&amp;quot;centerElt('saveDrawing',400,300);&amp;quot;&amp;gt;&amp;lt;/p&amp;gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;This code adds size selection buttons, color selectiors, and button for saving. &lt;br /&gt;&lt;br /&gt;Obviously you can use better CSS to save some code.&lt;br /&gt;&lt;br /&gt;Note that Save my drawing users a function that is not published here to keep things simpler. It centers a popup on the screen. You can find or code such function yourself, or simply display the saving form under the canvas without fancy effects. (Let me know in the comments if you need clarification.&lt;br /&gt;&lt;br /&gt;&lt;h2&gt;The save form&lt;/h2&gt;The save form is not important for this tutorial either. You can see the one at the Draw a Robot site, but the form can contain any fields you wish. Maybe image name, description, author name and so on.&lt;br /&gt;&lt;br /&gt;&lt;h2&gt;The Javascript&lt;/h2&gt;After opening a javascript tag, you'll need the following code. I'll input all the explanations to it as javascript comments so you can directly copy the code and use it.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Please note: the javascript below depends on jQuery!&lt;/b&gt; If you don't store local copy of jQuery, you need to insert this code in the header of your page:&lt;br /&gt;&amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;&lt;br /&gt;Now your javascript follows:&lt;br /&gt;&lt;br /&gt;&amp;lt;script type=&amp;quot;text/javascript&amp;quot;&amp;gt;&lt;br /&gt;/* some code used from http://www.williammalone.com/articles/create-html5-canvas-javascript-drawing-app/ */&lt;br /&gt;&lt;pre&gt;&lt;code&gt;&lt;br /&gt;/* Some global initializations follow. The first 2 arays will store all mouse positions &lt;br /&gt;on X and Y, the 3rd one stores the dragged positions. &lt;br /&gt;The variable &lt;b&gt;paint&lt;/b&gt; is a boolean, and then follow the default values &lt;br /&gt;which we use to start */&lt;br /&gt;var clickX = new Array();&lt;br /&gt;var clickY = new Array();&lt;br /&gt;var clickDrag = new Array();&lt;br /&gt;var paint;&lt;br /&gt;var defaultColor="#000";&lt;br /&gt;var defaultShape="round";&lt;br /&gt;var defaultWidth=5;&lt;br /&gt;&lt;br /&gt;// creating the canvas element&lt;br /&gt;var canvas = document.getElementById('drawingCanvas');&lt;br /&gt;&lt;br /&gt;if(canvas.getContext) &lt;br /&gt;{&lt;br /&gt;    // Initaliase a 2-dimensional drawing context&lt;br /&gt;    var context = canvas.getContext('2d');&lt;br /&gt;    &lt;br /&gt;    // set the defaults&lt;br /&gt;    context.strokeStyle = defaultColor;&lt;br /&gt;    context.lineJoin = defaultShape;&lt;br /&gt;    context.lineWidth = defaultWidth;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;// binding events to the canvas&lt;br /&gt;$('#drawingCanvas').mousedown(function(e){&lt;br /&gt;  var mouseX = e.pageX - this.offsetLeft;&lt;br /&gt;  var mouseY = e.pageY - this.offsetTop;&lt;br /&gt;  &lt;br /&gt;  paint = true; // start painting&lt;br /&gt;  addClick(e.pageX - this.offsetLeft, e.pageY - this.offsetTop);&lt;br /&gt;&lt;br /&gt;  // always call redraw&lt;br /&gt;  redraw();&lt;br /&gt;});&lt;br /&gt;&lt;br /&gt;$('#drawingCanvas').mousemove(function(e){&lt;br /&gt;  if(paint){&lt;br /&gt;    addClick(e.pageX - this.offsetLeft, e.pageY - this.offsetTop, true);&lt;br /&gt;    redraw();&lt;br /&gt;  }&lt;br /&gt;});&lt;br /&gt;&lt;br /&gt;// when mouse is released, stop painting, clear the arrays with dots&lt;br /&gt;$('#drawingCanvas').mouseup(function(e){&lt;br /&gt;  paint = false;&lt;br /&gt;  &lt;br /&gt;  clickX = new Array();&lt;br /&gt;  clickY = new Array();&lt;br /&gt;  clickDrag = new Array();&lt;br /&gt;});&lt;br /&gt;&lt;br /&gt;// stop painting when dragged out of the canvas&lt;br /&gt;$('#drawARobot').mouseleave(function(e){&lt;br /&gt;  paint = false;&lt;br /&gt;});&lt;br /&gt;&lt;br /&gt;// The function pushes to the three dot arrays&lt;br /&gt;function addClick(x, y, dragging)&lt;br /&gt;{&lt;br /&gt;  clickX.push(x);&lt;br /&gt;  clickY.push(y);&lt;br /&gt;  clickDrag.push(dragging);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;// this is where actual drawing happens&lt;br /&gt;// we add dots to the canvas&lt;br /&gt;function redraw(){&lt;br /&gt;    &lt;br /&gt;  for(var i=0; i &lt; clickX.length; i++)&lt;br /&gt;  {  &lt;br /&gt;    context.beginPath();&lt;br /&gt;    if(clickDrag[i] &amp;&amp; i){&lt;br /&gt;      context.moveTo(clickX[i-1], clickY[i-1]);&lt;br /&gt;     }else{&lt;br /&gt;       context.moveTo(clickX[i]-1, clickY[i]);&lt;br /&gt;     }&lt;br /&gt;     context.lineTo(clickX[i], clickY[i]);&lt;br /&gt;     context.closePath();&lt;br /&gt;     context.stroke();&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;// this is called when "clear canvas" button is pressed&lt;br /&gt;function clearCanvas()&lt;br /&gt;{&lt;br /&gt;    // both these lines are required to clear the canvas properly in all browsers&lt;br /&gt;    context.clearRect(0,0,canvas.width,canvas.height);&lt;br /&gt;    canvas.width = canvas.width;&lt;br /&gt;    &lt;br /&gt;    // we need to flush the arrays too&lt;br /&gt;    clickX = new Array();&lt;br /&gt;    clickY = new Array();&lt;br /&gt;    clickDrag = new Array();&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;/* Two simple functions, they just assign the selected color and size &lt;br /&gt;to the canvas object properties */ &lt;br /&gt;function setColor(col)&lt;br /&gt;{&lt;br /&gt;    context.strokeStyle = col;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;function setSize(px)&lt;br /&gt;{&lt;br /&gt;    context.lineWidth=px;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;/* Finally this will send your image to the server-side script which will &lt;br /&gt;save it to the database or where ever you want it saved.&lt;br /&gt;Note that this function should be called when the button in your save &lt;br /&gt;form is pressed. The variable frm is the form object. &lt;br /&gt;Basically the HTML will look like this:&lt;br /&gt;&amp;lt;input type=&amp;quot;button&amp;quot; value=&amp;quot;Save Drawing&amp;quot; onclick=&amp;quot;saveDrawing(this.form);&amp;quot;&amp;gt;&lt;br /&gt; */&lt;br /&gt;function saveDrawing(frm)&lt;br /&gt;{       &lt;br /&gt;    // converting the canvas to &lt;a href="http://en.wikipedia.org/wiki/Data_URI_scheme" target=_blank&gt;data URI&lt;/a&gt;&lt;br /&gt;    var strImageData = canvas.toDataURL();  &lt;br /&gt;        &lt;br /&gt;    $.ajax({&lt;br /&gt;        url: "", /* You need to enter the URL of your server side script*/&lt;br /&gt;        type: "post",&lt;br /&gt;          /* add the other variables here or serialize the entire form. &lt;br /&gt;          Image data must be URI encoded */&lt;br /&gt;        data: "save=1&amp;pic="+encodeURIComponent(strImageData), &lt;br /&gt;        success: function(msg)&lt;br /&gt;        {&lt;br /&gt;           // display some message and/or redirect&lt;br /&gt;        }&lt;br /&gt;    });&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;&lt;h2&gt;Your server side script&lt;/h2&gt;It really depends on you. In the Draw a Robot site we just save the robots to the database and then display them in a gallery. You can save the image data in a blob field in the DB. Just have in mind that data / URI images are displayed with the following HTML code:&lt;br /&gt;&lt;br /&gt;&amp;lt;img src=&amp;quot;encoded image data&amp;quot;&amp;gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5203032148063055732-6897074609401581999?l=cool-php-tutorials.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cool-php-tutorials.blogspot.com/feeds/6897074609401581999/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://cool-php-tutorials.blogspot.com/2011/05/simple-html5-drawing-app-with-saving.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5203032148063055732/posts/default/6897074609401581999'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5203032148063055732/posts/default/6897074609401581999'/><link rel='alternate' type='text/html' href='http://cool-php-tutorials.blogspot.com/2011/05/simple-html5-drawing-app-with-saving.html' title='Simple HTML5 Drawing App + Saving The Files With Ajax'/><author><name>Ultralight Flyer</name><uri>http://www.blogger.com/profile/02349553397305809903</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5203032148063055732.post-7531483018708783386</id><published>2010-03-25T13:09:00.000-07:00</published><updated>2010-03-25T13:48:42.136-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='ad management'/><title type='text'>Building a Fair and Convincing Banner Rotator</title><content type='html'>I have seen several tutorials how to write a banner rotator online. They are technically correct but looks like the programmers haven't really put their rotators in real environment. They assume that selecting a random banner from the list (or database) will do the work and show the banners equally. Good idea and correct in general. &lt;br /&gt;&lt;br /&gt;But as I have run such a rotator in a live site where more than 7-8 banners were rotating, I know this isn't a good enough solution. Why so? Although computer random algos or the SQL RAND() ensure all banners will be shown almost the same number of times in the long run, during 10-20 page refreshes you can keep getting the same 3-4 banners and other ones may not appear at all. This is a problem because the clients who purcased banners complain "Hey, I refreshed more than 10 times and my banner isn't showing even once!!!". Go explain them about random computer algos and that someone might be seeing their banner at the same in some other computer at the other side of the globe.&lt;br /&gt;&lt;br /&gt;To avoid such problem, we need not only fair, but also convincing banner rotator - such that will show you the banners equal times when you are refreshing the page.&lt;br /&gt;&lt;br /&gt;Let's see what we need:&lt;br /&gt;&lt;br /&gt;1. A list or DB table of banners&lt;br /&gt;2. Fair selection&lt;br /&gt;3. Counting the views and clicks&lt;br /&gt;&lt;br /&gt;That's in short. We will write two PHP scripts - one will select and output the banner (we'll assume you'll put it in an iframe or will add some other useful stuff to it so you can have a full page with content); and one will redirect the visitors to the targed URL and count the clicks. Let's start:&lt;br /&gt;&lt;br /&gt;&lt;b&gt;1. A list of banners&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;Because we need to save the number of clicks to our banners we'll use a MySQL DB table:&lt;br /&gt;&lt;br /&gt;Table &lt;span style="font-weight:bold;"&gt;banners&lt;/span&gt;:&lt;br /&gt;id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,&lt;br /&gt;image VARCHAR(255) NOT NULL,&lt;br /&gt;url VARCHAR(255) NOT NULL,&lt;br /&gt;clicks INT UNSIGNED NOT NULL;&lt;br /&gt;&lt;br /&gt;In the real life you will probably have also a field for customer name, email etc. but let's not worry about this now.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;2. Fair selection&lt;/b&gt;&lt;br /&gt;This is the most interesting part. Let's call our script rotator.php. It will need not only to select a supposingly random banner, but to ensure that the rotation will be equal even for every single user who is seeing the page. Here's how to do this: we will store the IDs of the banners shown in a session. When selecting, we will make sure we get a random banner from those which have not been shown yet. When all the banners have been shown once in the current session, we'll empty the data and start over. Let's talk code:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;rotator.php&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;code&gt;&lt;br /&gt;// in this array we'll store the shown banners&lt;br /&gt;if(!isset($_SESSION['banner_ids'])) $_SESSION['banner_ids']=array(0);&lt;br /&gt;&lt;br /&gt;// let's built the conditional SQL now&lt;br /&gt;$id_sql=implode(",",$_SESSION['banner_ids']);&lt;br /&gt;&lt;br /&gt;// now select random banner instead of these that were shown&lt;br /&gt;$q="SELECT * FROM banners WHERE id NOT IN ($id_sql) ORDER BY RAND() LIMIT 1";&lt;br /&gt;$banner=mysql_fetch_array(mysql_query($q));&lt;br /&gt;&lt;br /&gt;// enter this banner ID in the already seen ones&lt;br /&gt;$_SESSION['banner_ids'][]=$banner['id'];&lt;br /&gt;&lt;br /&gt;// now we need to check whether all the banners were shown once&lt;br /&gt;$q="SELECT COUNT(id) FROM banners";&lt;br /&gt;$num_banners=mysql_fetch_row(mysql_query($q));&lt;br /&gt;&lt;br /&gt;// we use "&amp;lt;" because our session array always contains one more ID - the zero which is &lt;br /&gt;// there to prevent mysql error&lt;br /&gt;if($num_banners[0] &amp;lt; sizeof($_SESSION['banner_ids']))&lt;br /&gt;{&lt;br /&gt;   unset($_SESSION['banner_ids']);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;// that's it! now just display the banner:&lt;br /&gt;echo "&amp;lt;a href='go.php?id=$banner[id]'&amp;gt;&amp;lt;img src='$banner[image]'&amp;gt;&amp;lt;/a&amp;gt;";&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Counting the views and clicks&lt;/b&gt;&lt;br /&gt;Now this is pretty straightforward:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;go.php&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;code&gt;&lt;br /&gt;// prevent hacking&lt;br /&gt;if(!is_numeric($_GET['id'])) exit;&lt;br /&gt;&lt;br /&gt;// select banner&lt;br /&gt;$q="SELECT * FROM banners WHERE id='$_GET[id]'";&lt;br /&gt;$banner=mysql_fetch_array(mysql_query($q));&lt;br /&gt;&lt;br /&gt;// update clicks&lt;br /&gt;$q="UPDATE banners SET clicks=clicks+1 WHERE id='$banner[id]'";&lt;br /&gt;mysql_query($q);&lt;br /&gt;&lt;br /&gt;// redirect&lt;br /&gt;header("Location: $banner[url]");&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;That's all!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5203032148063055732-7531483018708783386?l=cool-php-tutorials.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cool-php-tutorials.blogspot.com/feeds/7531483018708783386/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://cool-php-tutorials.blogspot.com/2010/03/building-fair-and-convincing-banner.html#comment-form' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5203032148063055732/posts/default/7531483018708783386'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5203032148063055732/posts/default/7531483018708783386'/><link rel='alternate' type='text/html' href='http://cool-php-tutorials.blogspot.com/2010/03/building-fair-and-convincing-banner.html' title='Building a Fair and Convincing Banner Rotator'/><author><name>Ultralight Flyer</name><uri>http://www.blogger.com/profile/02349553397305809903</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5203032148063055732.post-1281635997124196075</id><published>2010-02-22T12:27:00.001-08:00</published><updated>2010-02-22T12:50:46.436-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='databases'/><category scheme='http://www.blogger.com/atom/ns#' term='quiz'/><title type='text'>Designing a database for a personality quiz script</title><content type='html'>Since Blogger does not offer categories, I'll have to use tags for posts like this. And what's special about it? I'm not going to talk PHP this time - instead of that I'll talk databases.&lt;br /&gt;&lt;br /&gt;Let's discuss a database for a personality quiz script - you know, one that will build tests which will say "you are a good worker", "you like to spend too much money", "you are a loving person" etc. &lt;br /&gt;&lt;br /&gt;What is specific for the personality quizzes? The main part is that answers to questions must be matched to personality types. Some quizzes use point systems but they are less accurate because you may have answers for contrary personality types and the system may calculate that you belong to the middle one. For personality quizzes a lot more accurate is a system which will directly match the answer to the specific personality type and at the end show the personality type which collected the most answers.&lt;br /&gt;&lt;br /&gt;So here is my proposition for a database along with short explanations:&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Table Quizzes:&lt;/strong&gt;&lt;br /&gt;id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY&lt;br /&gt;title VARCHAR(255)&lt;br /&gt;description TEXT&lt;br /&gt;num_users INT UNSIGNED NOT NULL&lt;br /&gt;&lt;br /&gt;This table will contain one record for each quiz you want to run. You may want to add extra columns like date, tags etc. In num_users we will store the number of users who took the quiz.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Table Questions:&lt;/strong&gt;&lt;br /&gt;id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY&lt;br /&gt;quiz_id INT UNSIGNED FOREIGH KEY to quizzes.id&lt;br /&gt;question TEXT &lt;br /&gt;question_type&lt;br /&gt;&lt;br /&gt;This obviously is the table with questions. We need a foreign key to the table with quizzes and of course a field for the question itself. If you plan to have single choice and multiple choice questions, the field question_type will store the difference.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Table Answers:&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY&lt;br /&gt;quiz_id INT UNSIGNED FOREIGH KEY to quizzes.id&lt;br /&gt;question_id INT UNSIGNED FOREIGH KEY to questions.id&lt;br /&gt;answer TEXT&lt;br /&gt;result INT UNSIGNED FOREIGH KEY to results.id&lt;br /&gt;&lt;br /&gt;The table will have foreign keys to both Quizzes and Questions table. I know giving a key to Questions is logically enough, but I always prefer to have all the relations explicitly given in the table. This gives a lot more clarity especially if you are using ER diagrams.&lt;br /&gt;The "result" column may contain things like A, B, C which will &lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Table Results:&lt;/strong&gt;&lt;br /&gt;id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY&lt;br /&gt;quiz_id INT UNSIGNED FOREIGH KEY to quizzes.id&lt;br /&gt;value VARCHAR&lt;br /&gt;description TEXT&lt;br /&gt;&lt;br /&gt;This is the last table you need. It will contain the personality types. And because the Answers table has a foreign key to it, it will be very easy to calculate which result (personality type) has the most answers given.&lt;br /&gt;&lt;br /&gt;I'm leaving the PHP or other language implementation of this DB to you. If you want to check such thing in action, check out this &lt;a href="http://calendarscripts.info/quiz-script.html"&gt;php quiz script&lt;/a&gt;.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5203032148063055732-1281635997124196075?l=cool-php-tutorials.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cool-php-tutorials.blogspot.com/feeds/1281635997124196075/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://cool-php-tutorials.blogspot.com/2010/02/designing-database-for-personality-quiz.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5203032148063055732/posts/default/1281635997124196075'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5203032148063055732/posts/default/1281635997124196075'/><link rel='alternate' type='text/html' href='http://cool-php-tutorials.blogspot.com/2010/02/designing-database-for-personality-quiz.html' title='Designing a database for a personality quiz script'/><author><name>Ultralight Flyer</name><uri>http://www.blogger.com/profile/02349553397305809903</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5203032148063055732.post-7793200808327248340</id><published>2010-02-01T08:47:00.000-08:00</published><updated>2011-05-19T08:41:02.354-07:00</updated><title type='text'>Easy PHP Date Picker</title><content type='html'>There are many fancy javascript calendars for selecting dates - for example the one that comes as a jQuery plugin is really cool. There are however some disadvantages to using such calendars:&lt;br /&gt;&lt;br /&gt;&lt;ul&gt;&lt;br /&gt;   &lt;li&gt;If you only need a date picker, it's not worth to include all these Javascript libraries and overload.&lt;/li&gt;&lt;br /&gt;   &lt;li&gt;These javascript calendars are hard to use from people who don't see well or can't use a mouse&lt;/li&gt;&lt;br /&gt;   &lt;li&gt;In admin screens etc., where you may need to manage many dates in rows of records, clicking on the calendars can be much slower than picking dates from dropdown.&lt;/li&gt;&lt;br /&gt;&lt;/ul&gt;&lt;br /&gt;&lt;br /&gt;In cases like these and maybe more, it's better to create a simple dropdown date picker that will allow the user to select year, month and day (in fact this will be 3 dropdowns).&lt;br /&gt;&lt;br /&gt;Let's build such a function:&lt;br /&gt;&lt;br /&gt;First we'll build an array of months:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;$months=array('','January','February','March','April','May','June','July','August',&lt;br /&gt;'September','October','November','December');&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Note that I inserted the first element of the array as empty because I want the month numbers to start from 1 (for January).&lt;br /&gt;&lt;br /&gt;Then we'll construct the three dropdowns:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;code&gt;$html="&amp;lt;select name=\"".$name."month\"&amp;gt;";&lt;br /&gt; for($i=1;$i&lt;=12;$i++)&lt;br /&gt; {&lt;br /&gt;    $html.="&amp;lt;option value='$i'&amp;gt;$months[$i]&amp;lt;/option&amp;gt;";&lt;br /&gt; }&lt;br /&gt;$html.="&amp;lt;/select&amp;gt; ";&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;This was the months dropdown. Did you notice the "name" variable? We will pass it as argument to the function so we can control the name of the dropdowns and have many of them in a page. In very similar way you can create the days dropdown - from 1st to 31st. &lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;code&gt;&lt;br /&gt;$html.="&amp;lt;select name=\"".$name."day\"&amp;gt;";&lt;br /&gt; for($i=1;$i&lt;=31;$i++)&lt;br /&gt; {&lt;br /&gt;    $html.="&amp;lt;option value='$i'&amp;gt;$i&amp;lt;/option&amp;gt;";&lt;br /&gt; }&lt;br /&gt; $html.="&amp;lt;/select&amp;gt; ";&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;The years dropdown is just as simple. The only question in it is in what year to start and when to end. Your function can accept this as arguments or you can dynamically create start and end year accordingly to the current date. For example:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;code&gt;&lt;br /&gt;$startyear = date("Y")-100;&lt;br /&gt;$endyear= date("Y")+50;&lt;br /&gt;&lt;br /&gt; $html.="&amp;lt;select name=\"".$name."year\"&amp;gt;";&lt;br /&gt; for($i=$startyear;$i&lt;=$endyear;$i++)&lt;br /&gt; {&lt;br /&gt;   $chooser.="&amp;lt;option value='$i'&amp;gt;$i&amp;lt;/option&amp;gt;";&lt;br /&gt; }&lt;br /&gt; $html.="&amp;lt;/select&amp;gt; "; &lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;You can add some javascript to increase/reduce the number of days accordingly to the month, but this can be needlessly complicated. It's easier to solve this problem by javascript validation at the time when the form is submitted.&lt;br /&gt;&lt;br /&gt;&lt;h2&gt;So Here Is The Full Code (For Those Who Don't Get It):&lt;/h2&gt;&lt;br /&gt;&lt;br /&gt;Then put all this code into a function which accepts the argument $name:&lt;br /&gt;&lt;br /&gt;&lt;b&gt;&lt;pre&gt;&lt;code&gt;&lt;br /&gt;function date_picker($name, $startyear=NULL, $endyear=NULL)&lt;br /&gt;{&lt;br /&gt;    if($startyear==NULL) $startyear = date("Y")-100;&lt;br /&gt;    if($endyear==NULL) $endyear=date("Y")+50; &lt;br /&gt;&lt;br /&gt;    $months=array('','January','February','March','April','May',&lt;br /&gt;    'June','July','August', 'September','October','November','December');&lt;br /&gt;&lt;br /&gt;    // Month dropdown&lt;br /&gt;    $html="&amp;lt;select name=\"".$name."month\"&amp;gt;";&lt;br /&gt;&lt;br /&gt;    for($i=1;$i&lt;=12;$i++)&lt;br /&gt;    {&lt;br /&gt;       $html.="&amp;lt;option value='$i'&amp;gt;$months[$i]&amp;lt;/option&amp;gt;";&lt;br /&gt;    }&lt;br /&gt;    $html.="&amp;lt;/select&amp;gt; ";&lt;br /&gt;   &lt;br /&gt;    // Day dropdown&lt;br /&gt;    $html.="&amp;lt;select name=\"".$name."day\"&amp;gt;";&lt;br /&gt;    for($i=1;$i&lt;=31;$i++)&lt;br /&gt;    {&lt;br /&gt;       $html.="&amp;lt;option $selected value='$i'&amp;gt;$i&amp;lt;/option&amp;gt;";&lt;br /&gt;    }&lt;br /&gt;    $html.="&amp;lt;/select&amp;gt; ";&lt;br /&gt;&lt;br /&gt;    // Year dropdown&lt;br /&gt;    $html.="&amp;lt;select name=\"".$name."year\"&amp;gt;";&lt;br /&gt;&lt;br /&gt;    for($i=$startyear;$i&lt;=$endyear;$i++)&lt;br /&gt;    {      &lt;br /&gt;      $html.="&amp;lt;option value='$i'&amp;gt;$i&amp;lt;/option&amp;gt;";&lt;br /&gt;    }&lt;br /&gt;    $html.="&amp;lt;/select&amp;gt; ";&lt;br /&gt;&lt;br /&gt;    return $html;&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;And use the function as &lt;b&gt;echo date_picker("registration")&lt;/b&gt; (for example - "registration" is just a name you choose). The result that will come in $_POST after submitting such form will be in 3 variables:&lt;br /&gt;$_POST['registrationmonth'], $_POST['registrationday'] and $_POST['registrationyear'].&lt;br /&gt;&lt;br /&gt;You can easily construct a MySQL date from these variables.&lt;br /&gt;&lt;br /&gt;Now think how you can make this dropdown read stored data and pre-select its values accordingly to it (for "Edit record" forms).&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5203032148063055732-7793200808327248340?l=cool-php-tutorials.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cool-php-tutorials.blogspot.com/feeds/7793200808327248340/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://cool-php-tutorials.blogspot.com/2010/02/easy-php-date-picker.html#comment-form' title='9 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5203032148063055732/posts/default/7793200808327248340'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5203032148063055732/posts/default/7793200808327248340'/><link rel='alternate' type='text/html' href='http://cool-php-tutorials.blogspot.com/2010/02/easy-php-date-picker.html' title='Easy PHP Date Picker'/><author><name>Ultralight Flyer</name><uri>http://www.blogger.com/profile/02349553397305809903</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>9</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5203032148063055732.post-5217212324600849137</id><published>2010-01-16T08:34:00.000-08:00</published><updated>2010-01-16T10:23:34.978-08:00</updated><title type='text'>Building a Simple Monthly Calendar</title><content type='html'>&lt;p&gt;In this tutorial we are going to learn how to build a simple calendar in PHP. You have seen many of those calendar date pickers mostly in Javascript. This PHP based one has a slightly different purpose - instead of populating an input field, it outputs links for the different dates and can be used for example as a monthly archive in a dynamic DB-driven site.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;We will build this calendar following good coding practices and keeping the login in one PHP file and the presentation in one HTML file.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Let's start:&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;b&gt;First we need to define the current date OR a date coming by POST after the calendar form is submitted:&lt;/b&gt;&lt;/p&gt;&lt;br /&gt;&lt;pre&gt;&lt;code&gt;&lt;br /&gt;$today=getdate();&lt;br /&gt;$year=$_POST['year']?$_POST['year']:$today['year'];&lt;br /&gt;$month=$_POST['month']?$_POST['month']:$today['month'];&lt;br /&gt;$day=$_POST['day']?$_POST['day']:$event['day'];&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;PHP &lt;b&gt;getdate()&lt;/b&gt; function returns an array of year, month and day. We assign this to &lt;b&gt;$today&lt;/b&gt; variable and use it to define the calendar day, year and month - if they are coming from POST, the POST values have priority - otherwise we use $today values.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;b&gt;Then we need to define the number of days in the given month.&lt;/b&gt; We use function &lt;b&gt;cal_days_in_month()&lt;/b&gt; for that:&lt;/p&gt;&lt;br /&gt;&lt;pre&gt;&lt;code&gt;&lt;br /&gt;$numdays=cal_days_in_month(CAL_GREGORIAN, $month, $year);&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;b&gt;Now there's one specific.&lt;/b&gt; We don't want the calendar rows to be different number in different month. For that we'll make sure the calendar looks like the printed calendars and like this &lt;a href="http://calendarscripts.info/event-calendar-software.html"&gt;event calendar software&lt;/a&gt; by defining the week start day:&lt;/p&gt;&lt;br /&gt;&lt;pre&gt;&lt;code&gt;&lt;br /&gt;// calculate 'extra' days - these which need to come on the first&lt;br /&gt;// line - like 30-31- -- - -- - -- - -- - 1&lt;br /&gt;$estart=(7-$wday)+28;&lt;br /&gt;$extra=$numdays-$estart;&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;That's it, now we only need to include the design template:&lt;/p&gt;&lt;br /&gt;&lt;pre&gt;&lt;code&gt;&lt;br /&gt;require_once("mc_template.html");&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;b&gt;The rest is easy - display year and month dropdows:&lt;/b&gt;&lt;/p&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&amp;lt;select name="year" onchange="this.form.submit();"&amp;gt;&lt;br /&gt;&amp;ltphp&lt;br /&gt;for($i=$start_year;$i&lt;=2050;$i++)&lt;br /&gt;{&lt;br /&gt; if($i==$year) $selected='selected';&lt;br /&gt; else $selected='';&lt;br /&gt; echo "&amp;lt;option $selected value=$i&amp;gt;$i&amp;lt;/option&amp;gt;";&lt;br /&gt;}&lt;br /&gt;?&amp;gt;&lt;br /&gt;&amp;lt;/select&amp;gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;lt;select name="month" onchange="this.form.submit();"&amp;gt;&lt;br /&gt;&amp;lt;php&lt;br /&gt;for($i=1;$i&lt;=12;$i++)&lt;br /&gt;{&lt;br /&gt; if($i==$month) $selected='selected';&lt;br /&gt; else $selected='';&lt;br /&gt; echo "&amp;lt;option $selected value=$i&amp;gt;".$lmonths[$months[$i]]."&amp;lt;/option&amp;gt;";&lt;br /&gt;}&lt;br /&gt;// end PHP&lt;br /&gt;&amp;lt;/select&amp;gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;b&gt;And the calendar itself in a HTML table:&lt;/b&gt;&lt;/p&gt;&lt;br /&gt;&lt;pre&gt;&lt;code&gt;&lt;br /&gt;&lt;!--- CALENDAR ---&gt;&lt;br /&gt;&amp;ltp;?php&lt;br /&gt;if($extra&lt;0) $extra=0;&lt;br /&gt;for($i=$estart;$i&lt;$numdays;$i++)&lt;br /&gt;{     &lt;br /&gt;   // ...&lt;br /&gt;   // the full code can be downloaded at the bottom&lt;br /&gt;}&lt;br /&gt;?&gt; &lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;And your calendar is ready! You can download the free Microcal script that uses this same code &lt;a href="http://calendarscripts.info/free/microcal.zip"&gt;here&lt;/a&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5203032148063055732-5217212324600849137?l=cool-php-tutorials.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cool-php-tutorials.blogspot.com/feeds/5217212324600849137/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://cool-php-tutorials.blogspot.com/2010/01/building-simple-monthly-calendar.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5203032148063055732/posts/default/5217212324600849137'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5203032148063055732/posts/default/5217212324600849137'/><link rel='alternate' type='text/html' href='http://cool-php-tutorials.blogspot.com/2010/01/building-simple-monthly-calendar.html' title='Building a Simple Monthly Calendar'/><author><name>Ultralight Flyer</name><uri>http://www.blogger.com/profile/02349553397305809903</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5203032148063055732.post-1766396235897250891</id><published>2010-01-16T08:07:00.001-08:00</published><updated>2010-01-16T08:25:46.425-08:00</updated><title type='text'>Welcome... and first few tips!</title><content type='html'>Welcome to my new blog featuring some of my PHP tutorials. Here I'll try to share simple and elegant hacks that will help you not only solve varios problems but solve them in &lt;i&gt;easier, quicker and more elegant&lt;/i&gt; way.&lt;br /&gt;&lt;br /&gt;Being a great PHP developer doesn't mean to know each and every function and feature of the language. It means to solve the real world problems in efficient way. This is what the blog is going to help you for.&lt;br /&gt;&lt;br /&gt;&lt;h3&gt;Let's start with a few random smart tips&lt;/h3&gt;&lt;br /&gt;&lt;br /&gt;&lt;ul&gt;&lt;br /&gt;   &lt;li&gt;Using the &lt;a href="http://php.net/manual/en/control-structures.alternative-syntax.php"&gt;alternate PHP syntax&lt;/a&gt; in the HTML code will make it much cleaner and easier to follow.&lt;/li&gt;&lt;br /&gt;   &lt;li&gt;Make your own database object or download a class online instead of using the PHP built-in functions like mysql_query etc. It's not that you are going to change the DB engines (it's unlikely) - it's about using far more comfortable functions that return directly arrays of objects of the results you are selecting.&lt;/li&gt;&lt;br /&gt;   &lt;li&gt;Don't forget about the &lt;a href="http://www.phpf1.com/tutorial/php-heredoc-syntax.html"&gt;HEREDOC syntax&lt;/a&gt;.&lt;/li&gt;&lt;br /&gt;   &lt;li&gt;When passing IDs in the URL check them with &lt;b&gt;is_numeric()&lt;/b&gt; function. In many cases this is all you need to avoid attacks.&lt;/li&gt;&lt;br /&gt;   &lt;li&gt;Use good PHP editor such as &lt;a href="http://www.codelobster.com/"&gt;CodeLobster&lt;/a&gt;.&lt;/li&gt;&lt;br /&gt;   &lt;li&gt;Put a small comment at the beginning of all your PHP files. This is extremely useful when you return to old code.&lt;/li&gt;&lt;br /&gt;   &lt;li&gt;Use variable names that make sense. For example when selecting users from the DB call the result array &lt;b&gt;$users&lt;/b&gt; instead of &lt;b&gt;$rows&lt;/b&gt;. This will help you when the variable appears 10 lines later.&lt;/li&gt;&lt;br /&gt;&lt;/ul&gt;&lt;br /&gt;&lt;br /&gt;These tips are very basic, but I promise that's just the start. There are a lot more useful things coming in place here. Stay tuned!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5203032148063055732-1766396235897250891?l=cool-php-tutorials.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cool-php-tutorials.blogspot.com/feeds/1766396235897250891/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://cool-php-tutorials.blogspot.com/2010/01/welcome-and-first-few-tips.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5203032148063055732/posts/default/1766396235897250891'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5203032148063055732/posts/default/1766396235897250891'/><link rel='alternate' type='text/html' href='http://cool-php-tutorials.blogspot.com/2010/01/welcome-and-first-few-tips.html' title='Welcome... and first few tips!'/><author><name>Ultralight Flyer</name><uri>http://www.blogger.com/profile/02349553397305809903</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry></feed>
