<?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>David Walsh :: Legendary scribbles about JavaScript, HTML5, AJAX, PHP, CSS, and ∞. &#187; MySQL</title> <atom:link href="http://davidwalsh.name/tutorials/mysql/feed" rel="self" type="application/rss+xml" /><link>http://davidwalsh.name</link> <description>Legendary scribbles about JavaScript, HTML5, AJAX, PHP, CSS, and ∞.</description> <lastBuildDate>Thu, 02 Sep 2010 03:13:15 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.0.1</generator> <item><title>Quick Tip:  MySQL&#160;date_add</title><link>http://davidwalsh.name/mysql-date-add</link> <comments>http://davidwalsh.name/mysql-date-add#comments</comments> <pubDate>Thu, 20 May 2010 00:51:11 +0000</pubDate> <dc:creator>David Walsh</dc:creator> <category><![CDATA[MySQL]]></category><guid
isPermaLink="false">http://davidwalsh.name/?p=4971</guid> <description><![CDATA[Here&#8217;s a quick MySQL tip I wanted to throw your way. I created an event system a while back and one of the requirements of the system was to show events that happened yesterday forward, meaning events older than 2 days were to be hidden. MySQL&#8217;s date_add will allow you to do just that: The [...]<p><strong>Follow Me!</strong> <a
href="http://twitter.com/davidwalshblog">Twitter</a> | <a
href="http://www.facebook.com/#!/pages/David-Walsh-Blog/186644584869">Facebook</a> | <a
href="http://www.linkedin.com/in/davidjameswalsh">LinkedIn</a> | <a
href="http://mootools.net/forge/profile/davidwalsh">MooTools Forge.</a><br/><br/>Full David Walsh Blog Post: <a
href="http://davidwalsh.name/mysql-date-add">Quick Tip:  MySQL&nbsp;date_add</a></p>Related posts:<ol><li><a
href='http://davidwalsh.name/add-dates-mysql' rel='bookmark' title='Permanent Link: Adding Days To Dates In&nbsp;MySQL'>Adding Days To Dates In&nbsp;MySQL</a></li><li><a
href='http://davidwalsh.name/dates-mysql-php' rel='bookmark' title='Permanent Link: Breaking &#038; Fixing Dates from MySQL to&nbsp;PHP'>Breaking &#038; Fixing Dates from MySQL to&nbsp;PHP</a></li><li><a
href='http://davidwalsh.name/format-date-mysql-date_format' rel='bookmark' title='Permanent Link: Format Date Fields Using MySQL&nbsp;DATE_FORMAT()'>Format Date Fields Using MySQL&nbsp;DATE_FORMAT()</a></li><li><a
href='http://davidwalsh.name/return-random-records-mysql' rel='bookmark' title='Permanent Link: Return Random Records in&nbsp;MySQL'>Return Random Records in&nbsp;MySQL</a></li><li><a
href='http://davidwalsh.name/php-mysql-database-optimization-function' rel='bookmark' title='Permanent Link: PHP / MySQL Database Optimization&nbsp;Function'>PHP / MySQL Database Optimization&nbsp;Function</a></li></ol>]]></description> <content:encoded><![CDATA[<p>Here&#8217;s a quick MySQL tip I wanted to throw your way.  I created an event system a while back and one of the requirements of the system was to show events that happened yesterday forward, meaning events older than 2 days were to be hidden.  MySQL&#8217;s <span
class="function">date_add</span> will allow you to do just that:</p><h2>The MySQL&nbsp;Example</h2><pre class="sql">
SELECT title, venue, url, city, state, DATE_FORMAT(date_starts,\'%b %e\') as formatted_date
FROM events
WHERE date_starts >= DATE_ADD(NOW(), INTERVAL -1 DAY) // yesterday!
ORDER BY date_starts ASC
</pre><p>Note that I&#8217;m using a negative date value.  You can use a positive date value to get tomorrow, next week, next month, etc..</p><p>I like the idea of showing events the from past few days &#8212; they show that there&#8217;s been action recently. <a
href="http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-add" rel="nofollow">Click here</a> for more about MySQL <span
class="function">date_add</span>.</p><p><strong>Follow Me!</strong> <a
href="http://twitter.com/davidwalshblog">Twitter</a> | <a
href="http://www.facebook.com/#!/pages/David-Walsh-Blog/186644584869">Facebook</a> | <a
href="http://www.linkedin.com/in/davidjameswalsh">LinkedIn</a> | <a
href="http://mootools.net/forge/profile/davidwalsh">MooTools Forge.</a><br/><br/>Full David Walsh Blog Post: <a
href="http://davidwalsh.name/mysql-date-add">Quick Tip:  MySQL&nbsp;date_add</a></p><p>Related posts:<ol><li><a
href='http://davidwalsh.name/add-dates-mysql' rel='bookmark' title='Permanent Link: Adding Days To Dates In&nbsp;MySQL'>Adding Days To Dates In&nbsp;MySQL</a></li><li><a
href='http://davidwalsh.name/dates-mysql-php' rel='bookmark' title='Permanent Link: Breaking &#038; Fixing Dates from MySQL to&nbsp;PHP'>Breaking &#038; Fixing Dates from MySQL to&nbsp;PHP</a></li><li><a
href='http://davidwalsh.name/format-date-mysql-date_format' rel='bookmark' title='Permanent Link: Format Date Fields Using MySQL&nbsp;DATE_FORMAT()'>Format Date Fields Using MySQL&nbsp;DATE_FORMAT()</a></li><li><a
href='http://davidwalsh.name/return-random-records-mysql' rel='bookmark' title='Permanent Link: Return Random Records in&nbsp;MySQL'>Return Random Records in&nbsp;MySQL</a></li><li><a
href='http://davidwalsh.name/php-mysql-database-optimization-function' rel='bookmark' title='Permanent Link: PHP / MySQL Database Optimization&nbsp;Function'>PHP / MySQL Database Optimization&nbsp;Function</a></li></ol></p>]]></content:encoded> <wfw:commentRss>http://davidwalsh.name/mysql-date-add/feed</wfw:commentRss> <slash:comments>10</slash:comments> </item> <item><title>Create Custom Element Property Getters and Setters with&#160;MooTools</title><link>http://davidwalsh.name/mootools-custom-element</link> <comments>http://davidwalsh.name/mootools-custom-element#comments</comments> <pubDate>Mon, 03 May 2010 12:15:08 +0000</pubDate> <dc:creator>David Walsh</dc:creator> <category><![CDATA[MySQL]]></category><guid
isPermaLink="false">http://davidwalsh.name/?p=4961</guid> <description><![CDATA[One of the goals of the MooTools projects is to create very flexible methods by which developers may modify or extend the behavior of given classes. One example of that flexibility is the ability to customize the Element class&#8217; get and set methods for specified values. The default behavior of the get and set methods [...]<p><strong>Follow Me!</strong> <a
href="http://twitter.com/davidwalshblog">Twitter</a> | <a
href="http://www.facebook.com/#!/pages/David-Walsh-Blog/186644584869">Facebook</a> | <a
href="http://www.linkedin.com/in/davidjameswalsh">LinkedIn</a> | <a
href="http://mootools.net/forge/profile/davidwalsh">MooTools Forge.</a><br/><br/>Full David Walsh Blog Post: <a
href="http://davidwalsh.name/mootools-custom-element">Create Custom Element Property Getters and Setters with&nbsp;MooTools</a></p>Related posts:<ol><li><a
href='http://davidwalsh.name/javascript-dollar-function' rel='bookmark' title='Permanent Link: Create Your Own Custom JavaScript Dollar Function To Select An&nbsp;Element'>Create Your Own Custom JavaScript Dollar Function To Select An&nbsp;Element</a></li><li><a
href='http://davidwalsh.name/mootools-custom-event' rel='bookmark' title='Permanent Link: Create Custom Events in MooTools&nbsp;1.2'>Create Custom Events in MooTools&nbsp;1.2</a></li><li><a
href='http://davidwalsh.name/create-custom-pseudo-selector-mootools-selected' rel='bookmark' title='Permanent Link: Create a Custom &#8220;:selected&#8221; Pseudo Selector in&nbsp;MooTools'>Create a Custom &#8220;:selected&#8221; Pseudo Selector in&nbsp;MooTools</a></li><li><a
href='http://davidwalsh.name/slick-pseudo' rel='bookmark' title='Permanent Link: Create Custom Pseudo Class Selectors Using the Slick Selector&nbsp;Engine'>Create Custom Pseudo Class Selectors Using the Slick Selector&nbsp;Engine</a></li><li><a
href='http://davidwalsh.name/checked-pseudo-selector-mootools' rel='bookmark' title='Permanent Link: Create a Custom &#8220;:checked&#8221; Pseudo Selector for MooTools&nbsp;1.2'>Create a Custom &#8220;:checked&#8221; Pseudo Selector for MooTools&nbsp;1.2</a></li></ol>]]></description> <content:encoded><![CDATA[<p>One of the goals of the MooTools projects is to create very flexible methods by which developers may modify or extend the behavior of given classes.  One example of that flexibility is the ability to customize the Element class&#8217; <span
class="function">get</span> and <span
class="function">set</span> methods for specified values.</p><p>The default behavior of the get and set methods for an object are to return/modify the given Element attribute.  Take the following for example:</p><pre class="js">
var alt = document.id('myImage').get('title');  //returns the image's title attribute text
</pre><p>The above code modifies the title attribute for the given element.  Simple, of course, but then MooTools also uses the <span
class="function">get</span>/<span
class="function">set</span> methods to set and retrieve items that aren&#8217;t element attributes:</p><pre class="js">
var tag = document.id('myImage').get('tag'); //returns images's tag (img)
</pre><p>The above code retrieves the given element&#8217;s tag name.  How does MooTools do this?  Simple:  the <span
class="function">Element.Properties</span> object.  You can set custom get and set handling by adding your desired string to the <span
class="function">Element.Properties</span> object.  Here&#8217;s an example:</p><pre class="js">
Element.Properties.yourCustomGetOrSetName = {
	get: function() {
		//your custom programming to accomplish what should be returned...
		return yourReturnValue;
	},
	set: function(input) {
		//your custom programming to accomplish what should be set...
		//don't need to return anything
	},
	erase: function() { //erase works too!
		//programming to erase this
	}
};
</pre><p>Here&#8217;s the code MooTools uses internally to accomplish the <span
class="function">get(&#8216;tag&#8217;)</span> ability:</p><pre class="js">
Element.Properties.tag = {
	get: function(){
		return this.tagName.toLowerCase();
	}
};
</pre><p>So how could you use this?  You could use <span
class="function">Element.Properties</span> to create a <span
class="function">parent</span> getter:</p><pre class="js">
Element.Properties.parent = {
	get: function() {
		return this.getParent(); //returns the element's parent
	}
	//no need to do a setter or eraser
};
//.....
//usage
var parent = document.id('myElement').get('parent'); //returns myElement's parent element.
</pre><p>Pretty sweet, no?  Have any other items you&#8217;d like to see in the get or set methods?  Share!</p><p><strong>Follow Me!</strong> <a
href="http://twitter.com/davidwalshblog">Twitter</a> | <a
href="http://www.facebook.com/#!/pages/David-Walsh-Blog/186644584869">Facebook</a> | <a
href="http://www.linkedin.com/in/davidjameswalsh">LinkedIn</a> | <a
href="http://mootools.net/forge/profile/davidwalsh">MooTools Forge.</a><br/><br/>Full David Walsh Blog Post: <a
href="http://davidwalsh.name/mootools-custom-element">Create Custom Element Property Getters and Setters with&nbsp;MooTools</a></p><p>Related posts:<ol><li><a
href='http://davidwalsh.name/javascript-dollar-function' rel='bookmark' title='Permanent Link: Create Your Own Custom JavaScript Dollar Function To Select An&nbsp;Element'>Create Your Own Custom JavaScript Dollar Function To Select An&nbsp;Element</a></li><li><a
href='http://davidwalsh.name/mootools-custom-event' rel='bookmark' title='Permanent Link: Create Custom Events in MooTools&nbsp;1.2'>Create Custom Events in MooTools&nbsp;1.2</a></li><li><a
href='http://davidwalsh.name/create-custom-pseudo-selector-mootools-selected' rel='bookmark' title='Permanent Link: Create a Custom &#8220;:selected&#8221; Pseudo Selector in&nbsp;MooTools'>Create a Custom &#8220;:selected&#8221; Pseudo Selector in&nbsp;MooTools</a></li><li><a
href='http://davidwalsh.name/slick-pseudo' rel='bookmark' title='Permanent Link: Create Custom Pseudo Class Selectors Using the Slick Selector&nbsp;Engine'>Create Custom Pseudo Class Selectors Using the Slick Selector&nbsp;Engine</a></li><li><a
href='http://davidwalsh.name/checked-pseudo-selector-mootools' rel='bookmark' title='Permanent Link: Create a Custom &#8220;:checked&#8221; Pseudo Selector for MooTools&nbsp;1.2'>Create a Custom &#8220;:checked&#8221; Pseudo Selector for MooTools&nbsp;1.2</a></li></ol></p>]]></content:encoded> <wfw:commentRss>http://davidwalsh.name/mootools-custom-element/feed</wfw:commentRss> <slash:comments>4</slash:comments> </item> <item><title>Create a Basic Web Service Using PHP, MySQL, XML, and&#160;JSON</title><link>http://davidwalsh.name/web-service-php-mysql-xml-json</link> <comments>http://davidwalsh.name/web-service-php-mysql-xml-json#comments</comments> <pubDate>Wed, 06 May 2009 12:36:25 +0000</pubDate> <dc:creator>David Walsh</dc:creator> <category><![CDATA[APIs]]></category> <category><![CDATA[JavaScript]]></category> <category><![CDATA[Markup]]></category> <category><![CDATA[MySQL]]></category> <category><![CDATA[PHP]]></category><guid
isPermaLink="false">http://davidwalsh.name/?p=2324</guid> <description><![CDATA[Web services are taking over the world. I credit Twitter&#8217;s epic rise to the availability of a simple but rich API. Why not use the same model for your own sites? Here&#8217;s how to create a basic web service that provides an XML or JSON response using some PHP and MySQL. The PHP /&#160;MySQL /* [...]<p><strong>Follow Me!</strong> <a
href="http://twitter.com/davidwalshblog">Twitter</a> | <a
href="http://www.facebook.com/#!/pages/David-Walsh-Blog/186644584869">Facebook</a> | <a
href="http://www.linkedin.com/in/davidjameswalsh">LinkedIn</a> | <a
href="http://mootools.net/forge/profile/davidwalsh">MooTools Forge.</a><br/><br/>Full David Walsh Blog Post: <a
href="http://davidwalsh.name/web-service-php-mysql-xml-json">Create a Basic Web Service Using PHP, MySQL, XML, and&nbsp;JSON</a></p>Related posts:<ol><li><a
href='http://davidwalsh.name/get-reddit-score' rel='bookmark' title='Permanent Link: Get a URL&#8217;s Reddit Score Using PHP and&nbsp;JSON'>Get a URL&#8217;s Reddit Score Using PHP and&nbsp;JSON</a></li><li><a
href='http://davidwalsh.name/mootools-scrollspy-load' rel='bookmark' title='Permanent Link: Using MooTools ScrollSpy to Load More Items via&nbsp;JSON/AJAX'>Using MooTools ScrollSpy to Load More Items via&nbsp;JSON/AJAX</a></li><li><a
href='http://davidwalsh.name/wordpress-recent-posts' rel='bookmark' title='Permanent Link: Create a &#8220;Recent Posts&#8221; Module Outside of&nbsp;WordPress'>Create a &#8220;Recent Posts&#8221; Module Outside of&nbsp;WordPress</a></li><li><a
href='http://davidwalsh.name/bitly-php' rel='bookmark' title='Permanent Link: Create Bit.ly Short URLs Using&nbsp;PHP'>Create Bit.ly Short URLs Using&nbsp;PHP</a></li><li><a
href='http://davidwalsh.name/bitly-api-php' rel='bookmark' title='Permanent Link: Create Bit.ly Short URLs Using PHP: API Version&nbsp;3'>Create Bit.ly Short URLs Using PHP: API Version&nbsp;3</a></li></ol>]]></description> <content:encoded><![CDATA[<p>Web services are taking over the world.  I credit Twitter&#8217;s epic rise to the availability of a simple but rich API.  Why not use the same model for your own sites?  Here&#8217;s how to create a basic web service that provides an XML or JSON response using some PHP and MySQL.</p><h2>The PHP /&nbsp;MySQL</h2><pre class="php">/* require the user as the parameter */
if(isset($_GET['user']) &#038;&#038; intval($_GET['user'])) {

	/* soak in the passed variable or set our own */
	$number_of_posts = isset($_GET['num']) ? intval($_GET['num']) : 10; //10 is the default
	$format = strtolower($_GET['format']) == 'json' ? 'json' : 'xml'; //xml is the default
	$user_id = intval($_GET['user']); //no default

	/* connect to the db */
	$link = mysql_connect('localhost','username','password') or die('Cannot connect to the DB');
	mysql_select_db('db_name',$link) or die('Cannot select the DB');

	/* grab the posts from the db */
	$query = "SELECT post_title, guid FROM wp_posts WHERE post_author = $user_id AND post_status = 'publish' ORDER BY ID DESC LIMIT $number_of_posts";
	$result = mysql_query($query,$link) or die('Errant query:  '.$query);

	/* create one master array of the records */
	$posts = array();
	if(mysql_num_rows($result)) {
		while($post = mysql_fetch_assoc($result)) {
			$posts[] = array('post'=&gt;$post);
		}
	}

	/* output in necessary format */
	if($format == 'json') {
		header('Content-type: application/json');
		echo json_encode(array('posts'=&gt;$posts));
	}
	else {
		header('Content-type: text/xml');
		echo '&lt;posts&gt;';
		foreach($posts as $index =&gt; $post) {
			if(is_array($post)) {
				foreach($post as $key =&gt; $value) {
					echo '&lt;',$key,'&gt;';
					if(is_array($value)) {
						foreach($value as $tag =&gt; $val) {
							echo '&lt;',$tag,'&gt;',htmlentities($val),'&lt;/',$tag,'&gt;';
						}
					}
					echo '&lt;/',$key,'&gt;';
				}
			}
		}
		echo '&lt;/posts&gt;';
	}

	/* disconnect from the db */
	@mysql_close($link);
}</pre><p>With the number of persons hitting your web service (hopefully), you&#8217;ll need to do adequate validation before attempting to connect to the database to avoid injection attacks.  Once we get the desired results from the database, we cycle through the results to populate our return results array.  Depending upon the response type desired, we output the proper header and content in the desired format.</p><p>Take the following sample URL for example:</p><pre class="html">http://mydomain.com/web-service.php?user=2&amp;num=10</pre><p>Now, we can take a look at the possible results of the URL.</p><h2>The XML&nbsp;Output</h2><pre class="xml">&lt;posts&gt;
	&lt;post&gt;
		&lt;post_title&gt;SSLmatic SSL Certificate Giveaway Winners&lt;/post_title&gt;
		&lt;guid&gt;http://davidwalsh.name/?p=2304&lt;/guid&gt;
	&lt;/post&gt;
	&lt;post&gt;
		&lt;post_title&gt;MooTools FileManager&lt;/post_title&gt;
		&lt;guid&gt;http://davidwalsh.name/?p=2288&lt;/guid&gt;
	&lt;/post&gt;
	&lt;post&gt;
		&lt;post_title&gt;PHPTVDB: Using PHP to Retrieve TV Show Information&lt;/post_title&gt;
		&lt;guid&gt;http://davidwalsh.name/?p=2266&lt;/guid&gt;
	&lt;/post&gt;
	&lt;post&gt;
		&lt;post_title&gt;David Walsh: The Lost MooTools Plugins&lt;/post_title&gt;
		&lt;guid&gt;http://davidwalsh.name/?p=2258&lt;/guid&gt;
	&lt;/post&gt;
	&lt;post&gt;
		&lt;post_title&gt;Create Short URLs Using U.Nu&lt;/post_title&gt;
		&lt;guid&gt;http://davidwalsh.name/?p=2218&lt;/guid&gt;
	&lt;/post&gt;
	&lt;post&gt;
		&lt;post_title&gt;Create Bit.ly Short URLs Using PHP&lt;/post_title&gt;
		&lt;guid&gt;http://davidwalsh.name/?p=2194&lt;/guid&gt;
	&lt;/post&gt;
	&lt;post&gt;
		&lt;post_title&gt;Represent Your Repositories Using the GitHub Badge!&lt;/post_title&gt;
		&lt;guid&gt;http://davidwalsh.name/?p=2178&lt;/guid&gt;
	&lt;/post&gt;
	&lt;post&gt;
		&lt;post_title&gt;ZebraTable&lt;/post_title&gt;
		&lt;guid&gt;http://davidwalsh.name/?page_id=2172&lt;/guid&gt;
	&lt;/post&gt;
	&lt;post&gt;
		&lt;post_title&gt;MooTools Zebra Table Plugin&lt;/post_title&gt;
		&lt;guid&gt;http://davidwalsh.name/?p=2168&lt;/guid&gt;
	&lt;/post&gt;
	&lt;post&gt;
		&lt;post_title&gt;SSLmatic: Quality, Cheap SSL Certificates and Giveaway!&lt;/post_title&gt;
		&lt;guid&gt;http://davidwalsh.name/?p=2158&lt;/guid&gt;
	&lt;/post&gt;
&lt;/posts&gt;</pre><p>Take this next sample URL for example:</p><pre class="html">http://mydomain.com/web-service.php?user=2&amp;num=10&amp;format=json</pre><p>Now, we can take a look at the possible results of the URL.</p><h2>The JSON&nbsp;Output</h2><pre class="js">{"posts":[{"post":{"post_title":"SSLmatic SSL Certificate Giveaway Winners","guid":"http:\/\/davidwalsh.name\/?p=2304"}},{"post":{"post_title":"MooTools FileManager","guid":"http:\/\/davidwalsh.name\/?p=2288"}},{"post":{"post_title":"PHPTVDB: Using PHP to Retrieve TV Show Information","guid":"http:\/\/davidwalsh.name\/?p=2266"}},{"post":{"post_title":"David Walsh: The Lost MooTools Plugins","guid":"http:\/\/davidwalsh.name\/?p=2258"}},{"post":{"post_title":"Create Short URLs Using U.Nu","guid":"http:\/\/davidwalsh.name\/?p=2218"}},{"post":{"post_title":"Create Bit.ly Short URLs Using PHP","guid":"http:\/\/davidwalsh.name\/?p=2194"}},{"post":{"post_title":"Represent Your Repositories Using the GitHub Badge!","guid":"http:\/\/davidwalsh.name\/?p=2178"}},{"post":{"post_title":"ZebraTable","guid":"http:\/\/davidwalsh.name\/?page_id=2172"}},{"post":{"post_title":"MooTools Zebra Table Plugin","guid":"http:\/\/davidwalsh.name\/?p=2168"}},{"post":{"post_title":"SSLmatic: Quality, Cheap SSL Certificates and Giveaway!","guid":"http:\/\/davidwalsh.name\/?p=2158"}}]}</pre><p>Creating a basic web service is very simple and encourages your users to spread the word about your website or service.  Want more traffic?  Want your website to grow without you putting in all the effort?  Create a web service!</p><p><strong>Follow Me!</strong> <a
href="http://twitter.com/davidwalshblog">Twitter</a> | <a
href="http://www.facebook.com/#!/pages/David-Walsh-Blog/186644584869">Facebook</a> | <a
href="http://www.linkedin.com/in/davidjameswalsh">LinkedIn</a> | <a
href="http://mootools.net/forge/profile/davidwalsh">MooTools Forge.</a><br/><br/>Full David Walsh Blog Post: <a
href="http://davidwalsh.name/web-service-php-mysql-xml-json">Create a Basic Web Service Using PHP, MySQL, XML, and&nbsp;JSON</a></p><p>Related posts:<ol><li><a
href='http://davidwalsh.name/get-reddit-score' rel='bookmark' title='Permanent Link: Get a URL&#8217;s Reddit Score Using PHP and&nbsp;JSON'>Get a URL&#8217;s Reddit Score Using PHP and&nbsp;JSON</a></li><li><a
href='http://davidwalsh.name/mootools-scrollspy-load' rel='bookmark' title='Permanent Link: Using MooTools ScrollSpy to Load More Items via&nbsp;JSON/AJAX'>Using MooTools ScrollSpy to Load More Items via&nbsp;JSON/AJAX</a></li><li><a
href='http://davidwalsh.name/wordpress-recent-posts' rel='bookmark' title='Permanent Link: Create a &#8220;Recent Posts&#8221; Module Outside of&nbsp;WordPress'>Create a &#8220;Recent Posts&#8221; Module Outside of&nbsp;WordPress</a></li><li><a
href='http://davidwalsh.name/bitly-php' rel='bookmark' title='Permanent Link: Create Bit.ly Short URLs Using&nbsp;PHP'>Create Bit.ly Short URLs Using&nbsp;PHP</a></li><li><a
href='http://davidwalsh.name/bitly-api-php' rel='bookmark' title='Permanent Link: Create Bit.ly Short URLs Using PHP: API Version&nbsp;3'>Create Bit.ly Short URLs Using PHP: API Version&nbsp;3</a></li></ol></p>]]></content:encoded> <wfw:commentRss>http://davidwalsh.name/web-service-php-mysql-xml-json/feed</wfw:commentRss> <slash:comments>39</slash:comments> </item> <item><title>Backup Your Database into an XML File Using&#160;PHP</title><link>http://davidwalsh.name/backup-database-xml-php</link> <comments>http://davidwalsh.name/backup-database-xml-php#comments</comments> <pubDate>Fri, 27 Feb 2009 14:19:35 +0000</pubDate> <dc:creator>David Walsh</dc:creator> <category><![CDATA[Markup]]></category> <category><![CDATA[MySQL]]></category> <category><![CDATA[PHP]]></category><guid
isPermaLink="false">http://davidwalsh.name/?p=1409</guid> <description><![CDATA[Backing up data is extremely important. Most of the time the database is the most important piece of the puzzle. Imagine losing all of the data in your database &#8212; it would be tragic. Here&#8217;s a PHP snippet that outputs your database as XML. The&#160;PHP //connect $link = mysql_connect($host,$user,$pass); mysql_select_db($name,$link); //get all the tables $query [...]<p><strong>Follow Me!</strong> <a
href="http://twitter.com/davidwalshblog">Twitter</a> | <a
href="http://www.facebook.com/#!/pages/David-Walsh-Blog/186644584869">Facebook</a> | <a
href="http://www.linkedin.com/in/davidjameswalsh">LinkedIn</a> | <a
href="http://mootools.net/forge/profile/davidwalsh">MooTools Forge.</a><br/><br/>Full David Walsh Blog Post: <a
href="http://davidwalsh.name/backup-database-xml-php">Backup Your Database into an XML File Using&nbsp;PHP</a></p>Related posts:<ol><li><a
href='http://davidwalsh.name/backup-mysql-database-php' rel='bookmark' title='Permanent Link: Backup Your MySQL Database Using&nbsp;PHP'>Backup Your MySQL Database Using&nbsp;PHP</a></li><li><a
href='http://davidwalsh.name/php-mysql-database-optimization-function' rel='bookmark' title='Permanent Link: PHP / MySQL Database Optimization&nbsp;Function'>PHP / MySQL Database Optimization&nbsp;Function</a></li><li><a
href='http://davidwalsh.name/format-date-mysql-date_format' rel='bookmark' title='Permanent Link: Format Date Fields Using MySQL&nbsp;DATE_FORMAT()'>Format Date Fields Using MySQL&nbsp;DATE_FORMAT()</a></li><li><a
href='http://davidwalsh.name/return-random-records-mysql' rel='bookmark' title='Permanent Link: Return Random Records in&nbsp;MySQL'>Return Random Records in&nbsp;MySQL</a></li><li><a
href='http://davidwalsh.name/html-mysql-php' rel='bookmark' title='Permanent Link: Build HTML Tables From MySQL Tables with&nbsp;PHP'>Build HTML Tables From MySQL Tables with&nbsp;PHP</a></li></ol>]]></description> <content:encoded><![CDATA[<p>Backing up data is extremely important.  Most of the time the database is the most important piece of the puzzle.  Imagine losing all of the data in your database &#8212; it would be tragic.  Here&#8217;s a PHP snippet that outputs your database as XML.</p><h2>The&nbsp;PHP</h2><pre class="php">
//connect
$link = mysql_connect($host,$user,$pass);
mysql_select_db($name,$link);

//get all the tables
$query = 'SHOW TABLES FROM '.$name;
$result = mysql_query($query,$link) or die('cannot show tables');
if(mysql_num_rows($result))
{
	//prep output
	$tab = "\t";
	$br = "\n";
	$xml = '&lt;?xml version="1.0" encoding="UTF-8"?&gt;'.$br;
	$xml.= '&lt;database name="'.$name.'"&gt;'.$br;
	
	//for every table...
	while($table = mysql_fetch_row($result))
	{
		//prep table out
		$xml.= $tab.'&lt;table name="'.$table[0].'"&gt;'.$br;
		
		//get the rows
		$query3 = 'SELECT * FROM '.$table[0];
		$records = mysql_query($query3,$link) or die('cannot select from table: '.$table[0]);
		
		//table attributes
		$attributes = array('name','blob','maxlength','multiple_key','not_null','numeric','primary_key','table','type','default','unique_key','unsigned','zerofill');
		$xml.= $tab.$tab.'&lt;columns&gt;'.$br;
		$x = 0;
		while($x &lt; mysql_num_fields($records))
		{
			$meta = mysql_fetch_field($records,$x);
			$xml.= $tab.$tab.$tab.'&lt;column ';
			foreach($attributes as $attribute)
			{
				$xml.= $attribute.'="'.$meta-&gt;$attribute.'" ';
			}
			$xml.= '/&gt;'.$br;
			$x++;
		}
		$xml.= $tab.$tab.'&lt;/columns&gt;'.$br;
		
		//stick the records
		$xml.= $tab.$tab.'&lt;records&gt;'.$br;
		while($record = mysql_fetch_assoc($records))
		{
			$xml.= $tab.$tab.$tab.'&lt;record&gt;'.$br;
			foreach($record as $key=&gt;$value)
			{
				$xml.= $tab.$tab.$tab.$tab.'&lt;'.$key.'&gt;'.htmlspecialchars(stripslashes($value)).'&lt;/'.$key.'&gt;'.$br;
			}
			$xml.= $tab.$tab.$tab.'&lt;/record&gt;'.$br;
		}
		$xml.= $tab.$tab.'&lt;/records&gt;'.$br;
		$xml.= $tab.'&lt;/table&gt;'.$br;
	}
	$xml.= '&lt;/database&gt;';
	
	//save file
	$handle = fopen($name.'-backup-'.time().'.xml','w+');
	fwrite($handle,$xml);
	fclose($handle);
}
</pre><p>You probably don&#8217;t NEED to add column nodes but I like including as much data as possible and they don&#8217;t add very much to the total file size.</p><h2>The Sample&nbsp;Output</h2><pre class="xml">
&lt;database name="my_database"&gt;
	&lt;table name="wp_comments"&gt;
		&lt;columns&gt;
			&lt;column name="comment_ID" blob="0" maxlength="" multiple_key="0" not_null="1" numeric="1" primary_key="1" table="wp_comments" type="int" default="" unique_key="0" unsigned="1" zerofill="0" /&gt;
			&lt;column name="comment_post_ID" blob="0" maxlength="" multiple_key="1" not_null="1" numeric="1" primary_key="0" table="wp_comments" type="int" default="" unique_key="0" unsigned="0" zerofill="0" /&gt;
			&lt;column name="comment_author" blob="1" maxlength="" multiple_key="0" not_null="1" numeric="0" primary_key="0" table="wp_comments" type="blob" default="" unique_key="0" unsigned="0" zerofill="0" /&gt;
			&lt;column name="comment_author_email" blob="0" maxlength="" multiple_key="0" not_null="1" numeric="0" primary_key="0" table="wp_comments" type="string" default="" unique_key="0" unsigned="0" zerofill="0" /&gt;
			&lt;column name="comment_author_url" blob="0" maxlength="" multiple_key="0" not_null="1" numeric="0" primary_key="0" table="wp_comments" type="string" default="" unique_key="0" unsigned="0" zerofill="0" /&gt;
			&lt;column name="comment_author_IP" blob="0" maxlength="" multiple_key="0" not_null="1" numeric="0" primary_key="0" table="wp_comments" type="string" default="" unique_key="0" unsigned="0" zerofill="0" /&gt;
			&lt;column name="comment_date" blob="0" maxlength="" multiple_key="0" not_null="1" numeric="0" primary_key="0" table="wp_comments" type="datetime" default="" unique_key="0" unsigned="0" zerofill="0" /&gt;
			&lt;column name="comment_date_gmt" blob="0" maxlength="" multiple_key="0" not_null="1" numeric="0" primary_key="0" table="wp_comments" type="datetime" default="" unique_key="0" unsigned="0" zerofill="0" /&gt;
			&lt;column name="comment_content" blob="1" maxlength="" multiple_key="0" not_null="1" numeric="0" primary_key="0" table="wp_comments" type="blob" default="" unique_key="0" unsigned="0" zerofill="0" /&gt;
			&lt;column name="comment_karma" blob="0" maxlength="" multiple_key="0" not_null="1" numeric="1" primary_key="0" table="wp_comments" type="int" default="" unique_key="0" unsigned="0" zerofill="0" /&gt;
			&lt;column name="comment_approved" blob="0" maxlength="" multiple_key="1" not_null="1" numeric="0" primary_key="0" table="wp_comments" type="string" default="" unique_key="0" unsigned="0" zerofill="0" /&gt;
			&lt;column name="comment_agent" blob="0" maxlength="" multiple_key="0" not_null="1" numeric="0" primary_key="0" table="wp_comments" type="string" default="" unique_key="0" unsigned="0" zerofill="0" /&gt;
			&lt;column name="comment_type" blob="0" maxlength="" multiple_key="0" not_null="1" numeric="0" primary_key="0" table="wp_comments" type="string" default="" unique_key="0" unsigned="0" zerofill="0" /&gt;
			&lt;column name="comment_parent" blob="0" maxlength="" multiple_key="0" not_null="1" numeric="1" primary_key="0" table="wp_comments" type="int" default="" unique_key="0" unsigned="0" zerofill="0" /&gt;
			&lt;column name="user_id" blob="0" maxlength="" multiple_key="0" not_null="1" numeric="1" primary_key="0" table="wp_comments" type="int" default="" unique_key="0" unsigned="0" zerofill="0" /&gt;
		&lt;/columns&gt;
		&lt;records&gt;
			&lt;record&gt;
				&lt;comment_ID&gt;2&lt;/comment_ID&gt;
				&lt;comment_post_ID&gt;4&lt;/comment_post_ID&gt;
				&lt;comment_author&gt;Ryan&lt;/comment_author&gt;
				&lt;comment_author_email&gt;&#114;&#121;anta&#115;&#116;&#97;d&#64;h&#111;&#116;&#109;&#97;i&#108;&#46;&#99;o&#109;&lt;/comment_author_email&gt;
				&lt;comment_author_url&gt;&lt;/comment_author_url&gt;
				&lt;comment_author_IP&gt;66.84.199.242&lt;/comment_author_IP&gt;
				&lt;comment_date&gt;2007-12-06 10:10:38&lt;/comment_date&gt;
				&lt;comment_date_gmt&gt;2007-12-06 16:10:38&lt;/comment_date_gmt&gt;
				&lt;comment_content&gt;Roethlisberger is coming to town!?  Sorry, Fred.&lt;/comment_content&gt;
				&lt;comment_karma&gt;0&lt;/comment_karma&gt;
				&lt;comment_approved&gt;1&lt;/comment_approved&gt;
				&lt;comment_agent&gt;Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322)&lt;/comment_agent&gt;
				&lt;comment_type&gt;&lt;/comment_type&gt;
				&lt;comment_parent&gt;0&lt;/comment_parent&gt;
				&lt;user_id&gt;0&lt;/user_id&gt;
			&lt;/record&gt;
		&lt;/records&gt;
	&lt;/table&gt;
	&lt;table name="wp_links"&gt;
		&lt;columns&gt;
			&lt;column name="link_id" blob="0" maxlength="" multiple_key="0" not_null="1" numeric="1" primary_key="1" table="wp_links" type="int" default="" unique_key="0" unsigned="0" zerofill="0" /&gt;
			&lt;column name="link_url" blob="0" maxlength="" multiple_key="0" not_null="1" numeric="0" primary_key="0" table="wp_links" type="string" default="" unique_key="0" unsigned="0" zerofill="0" /&gt;
			&lt;column name="link_name" blob="0" maxlength="" multiple_key="0" not_null="1" numeric="0" primary_key="0" table="wp_links" type="string" default="" unique_key="0" unsigned="0" zerofill="0" /&gt;
			&lt;column name="link_image" blob="0" maxlength="" multiple_key="0" not_null="1" numeric="0" primary_key="0" table="wp_links" type="string" default="" unique_key="0" unsigned="0" zerofill="0" /&gt;
			&lt;column name="link_target" blob="0" maxlength="" multiple_key="0" not_null="1" numeric="0" primary_key="0" table="wp_links" type="string" default="" unique_key="0" unsigned="0" zerofill="0" /&gt;
			&lt;column name="link_category" blob="0" maxlength="" multiple_key="1" not_null="1" numeric="1" primary_key="0" table="wp_links" type="int" default="" unique_key="0" unsigned="0" zerofill="0" /&gt;
			&lt;column name="link_description" blob="0" maxlength="" multiple_key="0" not_null="1" numeric="0" primary_key="0" table="wp_links" type="string" default="" unique_key="0" unsigned="0" zerofill="0" /&gt;
			&lt;column name="link_visible" blob="0" maxlength="" multiple_key="1" not_null="1" numeric="0" primary_key="0" table="wp_links" type="string" default="" unique_key="0" unsigned="0" zerofill="0" /&gt;
			&lt;column name="link_owner" blob="0" maxlength="" multiple_key="0" not_null="1" numeric="1" primary_key="0" table="wp_links" type="int" default="" unique_key="0" unsigned="0" zerofill="0" /&gt;
			&lt;column name="link_rating" blob="0" maxlength="" multiple_key="0" not_null="1" numeric="1" primary_key="0" table="wp_links" type="int" default="" unique_key="0" unsigned="0" zerofill="0" /&gt;
			&lt;column name="link_updated" blob="0" maxlength="" multiple_key="0" not_null="1" numeric="0" primary_key="0" table="wp_links" type="datetime" default="" unique_key="0" unsigned="0" zerofill="0" /&gt;
			&lt;column name="link_rel" blob="0" maxlength="" multiple_key="0" not_null="1" numeric="0" primary_key="0" table="wp_links" type="string" default="" unique_key="0" unsigned="0" zerofill="0" /&gt;
			&lt;column name="link_notes" blob="1" maxlength="" multiple_key="0" not_null="1" numeric="0" primary_key="0" table="wp_links" type="blob" default="" unique_key="0" unsigned="0" zerofill="0" /&gt;
			&lt;column name="link_rss" blob="0" maxlength="" multiple_key="0" not_null="1" numeric="0" primary_key="0" table="wp_links" type="string" default="" unique_key="0" unsigned="0" zerofill="0" /&gt;
		&lt;/columns&gt;
		&lt;records&gt;
			&lt;record&gt;
				&lt;link_id&gt;1&lt;/link_id&gt;
				&lt;link_url&gt;http://codex.wordpress.org/&lt;/link_url&gt;
				&lt;link_name&gt;Documentation&lt;/link_name&gt;
				&lt;link_image&gt;&lt;/link_image&gt;
				&lt;link_target&gt;&lt;/link_target&gt;
				&lt;link_category&gt;0&lt;/link_category&gt;
				&lt;link_description&gt;&lt;/link_description&gt;
				&lt;link_visible&gt;Y&lt;/link_visible&gt;
				&lt;link_owner&gt;1&lt;/link_owner&gt;
				&lt;link_rating&gt;0&lt;/link_rating&gt;
				&lt;link_updated&gt;0000-00-00 00:00:00&lt;/link_updated&gt;
				&lt;link_rel&gt;&lt;/link_rel&gt;
				&lt;link_notes&gt;&lt;/link_notes&gt;
				&lt;link_rss&gt;&lt;/link_rss&gt;
			&lt;/record&gt;
			&lt;record&gt;
				&lt;link_id&gt;2&lt;/link_id&gt;
				&lt;link_url&gt;http://wordpress.org/development/&lt;/link_url&gt;
				&lt;link_name&gt;Development Blog&lt;/link_name&gt;
				&lt;link_image&gt;&lt;/link_image&gt;
				&lt;link_target&gt;&lt;/link_target&gt;
				&lt;link_category&gt;0&lt;/link_category&gt;
				&lt;link_description&gt;&lt;/link_description&gt;
				&lt;link_visible&gt;Y&lt;/link_visible&gt;
				&lt;link_owner&gt;1&lt;/link_owner&gt;
				&lt;link_rating&gt;0&lt;/link_rating&gt;
				&lt;link_updated&gt;0000-00-00 00:00:00&lt;/link_updated&gt;
				&lt;link_rel&gt;&lt;/link_rel&gt;
				&lt;link_notes&gt;&lt;/link_notes&gt;
				&lt;link_rss&gt;http://wordpress.org/development/feed/&lt;/link_rss&gt;
			&lt;/record&gt;
		&lt;/records&gt;
	&lt;/table&gt;
&lt;/database&gt;
</pre><p>XML isn&#8217;t the easiest format to restore a table with so you may prefer to <a
href="http://davidwalsh.name/backup-mysql-database-php">export the table as SQL statements</a>.  I enjoy the additional XML backup because it&#8217;s easy to read.</p><p><strong>Follow Me!</strong> <a
href="http://twitter.com/davidwalshblog">Twitter</a> | <a
href="http://www.facebook.com/#!/pages/David-Walsh-Blog/186644584869">Facebook</a> | <a
href="http://www.linkedin.com/in/davidjameswalsh">LinkedIn</a> | <a
href="http://mootools.net/forge/profile/davidwalsh">MooTools Forge.</a><br/><br/>Full David Walsh Blog Post: <a
href="http://davidwalsh.name/backup-database-xml-php">Backup Your Database into an XML File Using&nbsp;PHP</a></p><p>Related posts:<ol><li><a
href='http://davidwalsh.name/backup-mysql-database-php' rel='bookmark' title='Permanent Link: Backup Your MySQL Database Using&nbsp;PHP'>Backup Your MySQL Database Using&nbsp;PHP</a></li><li><a
href='http://davidwalsh.name/php-mysql-database-optimization-function' rel='bookmark' title='Permanent Link: PHP / MySQL Database Optimization&nbsp;Function'>PHP / MySQL Database Optimization&nbsp;Function</a></li><li><a
href='http://davidwalsh.name/format-date-mysql-date_format' rel='bookmark' title='Permanent Link: Format Date Fields Using MySQL&nbsp;DATE_FORMAT()'>Format Date Fields Using MySQL&nbsp;DATE_FORMAT()</a></li><li><a
href='http://davidwalsh.name/return-random-records-mysql' rel='bookmark' title='Permanent Link: Return Random Records in&nbsp;MySQL'>Return Random Records in&nbsp;MySQL</a></li><li><a
href='http://davidwalsh.name/html-mysql-php' rel='bookmark' title='Permanent Link: Build HTML Tables From MySQL Tables with&nbsp;PHP'>Build HTML Tables From MySQL Tables with&nbsp;PHP</a></li></ol></p>]]></content:encoded> <wfw:commentRss>http://davidwalsh.name/backup-database-xml-php/feed</wfw:commentRss> <slash:comments>27</slash:comments> </item> <item><title>Animated AJAX Record Deletion Using&#160;MooTools</title><link>http://davidwalsh.name/animated-ajax-record-deletion-mootools</link> <comments>http://davidwalsh.name/animated-ajax-record-deletion-mootools#comments</comments> <pubDate>Thu, 29 Jan 2009 13:54:51 +0000</pubDate> <dc:creator>David Walsh</dc:creator> <category><![CDATA[AJAX]]></category> <category><![CDATA[MooTools]]></category> <category><![CDATA[MySQL]]></category> <category><![CDATA[PHP]]></category><guid
isPermaLink="false">http://davidwalsh.name/?p=1289</guid> <description><![CDATA[I&#8217;m a huge fan of WordPress&#8217; method of individual article deletion. You click the delete link, the menu item animates red, and the item disappears. Here&#8217;s how to achieve that functionality with MooTools JavaScript. View Demo The PHP &#8211; Content &#38;&#160;Header The following snippet goes at the top of the page: if(isset($_GET['delete'])) { $query = [...]<p><strong>Follow Me!</strong> <a
href="http://twitter.com/davidwalshblog">Twitter</a> | <a
href="http://www.facebook.com/#!/pages/David-Walsh-Blog/186644584869">Facebook</a> | <a
href="http://www.linkedin.com/in/davidjameswalsh">LinkedIn</a> | <a
href="http://mootools.net/forge/profile/davidwalsh">MooTools Forge.</a><br/><br/>Full David Walsh Blog Post: <a
href="http://davidwalsh.name/animated-ajax-record-deletion-mootools">Animated AJAX Record Deletion Using&nbsp;MooTools</a></p>Related posts:<ol><li><a
href='http://davidwalsh.name/animated-ajax-jquery' rel='bookmark' title='Permanent Link: Animated AJAX Record Deletion Using&nbsp;jQuery'>Animated AJAX Record Deletion Using&nbsp;jQuery</a></li><li><a
href='http://davidwalsh.name/dojo-ajax' rel='bookmark' title='Permanent Link: Animated AJAX Record Deletion Using&nbsp;Dojo'>Animated AJAX Record Deletion Using&nbsp;Dojo</a></li><li><a
href='http://davidwalsh.name/text-selection-ajax' rel='bookmark' title='Permanent Link: Record Text Selections Using MooTools or jQuery&nbsp;AJAX'>Record Text Selections Using MooTools or jQuery&nbsp;AJAX</a></li><li><a
href='http://davidwalsh.name/editable-content-mootools-php-mysql' rel='bookmark' title='Permanent Link: Editable Content Using MooTools 1.2, PHP, and&nbsp;MySQL'>Editable Content Using MooTools 1.2, PHP, and&nbsp;MySQL</a></li><li><a
href='http://davidwalsh.name/basic-ajax-requests-mootools' rel='bookmark' title='Permanent Link: Basic AJAX Requests Using MooTools&nbsp;1.2'>Basic AJAX Requests Using MooTools&nbsp;1.2</a></li></ol>]]></description> <content:encoded><![CDATA[<p>I&#8217;m a huge fan of WordPress&#8217; method of individual article deletion.  You click the delete link, the menu item animates red, and the item disappears.  Here&#8217;s how to achieve that functionality with MooTools JavaScript.</p><div
class="actions"> <a
href="http://davidwalsh.name/dw-content/mootools-record-delete.php" class="demo">View Demo</a><div
class="clear"></div></div><h2>The PHP &#8211; Content &amp;&nbsp;Header</h2><p>The following snippet goes at the top of the page:</p><pre class="php">
if(isset($_GET['delete']))
{
	$query = 'DELETE FROM my_table WHERE item_id = '.(int)$_GET['delete'];
	$result = mysql_query($query,$link);
}
</pre><p>The following is used to display the records:</p><pre class="php">
$query = 'SELECT * FROM my_table ORDER BY title ASC';
$result = mysql_query($query,$link);
while($row = mysql_fetch_assoc($result))
{
	echo '&lt;div class="record" id="record-',$row['item_id'],'"&gt;
				&lt;a href="?delete=',$row['item_id'],'" class="delete"&gt;Delete&lt;/a&gt;
				&lt;strong&gt;',$row['title'],'&lt;/strong&gt;
			&lt;/div&gt;';
}
</pre><h2>The MooTools&nbsp;JavaScript</h2><pre class="js">
window.addEvent('domready',function() {
	$$('a.delete').each(function(el) {
		el.addEvent('click',function(e) {
			e.stop();
			var parent = el.getParent('div');
			var request = new Request({
				url: 'mootools-record-delete.php',
				link: 'chain',
				method: 'get',
				data: {
					'delete': parent.get('id').replace('record-',''),
					ajax: 1
				},
				onRequest: function() {
					new Fx.Tween(parent,{
						duration:300
					}).start('background-color', '#fb6c6c');
				},
				onSuccess: function() {
					new Fx.Slide(parent,{
						duration:300,
						onComplete: function() {
							parent.dispose();
						}
					}).slideOut();
				}
			}).send();
		});
	});
});
</pre><p>For every link, we add a click event that triggers the AJAX request.  During the request, we tween the containing element to a red background.  When the AJAX request returns a &#8220;success&#8221; response, we slide the element off of the screen.</p><div
class="actions"> <a
href="http://davidwalsh.name/dw-content/mootools-record-delete.php" class="demo">View Demo</a><div
class="clear"></div></div><p>How would you use this?  Share!</p><p><strong>Follow Me!</strong> <a
href="http://twitter.com/davidwalshblog">Twitter</a> | <a
href="http://www.facebook.com/#!/pages/David-Walsh-Blog/186644584869">Facebook</a> | <a
href="http://www.linkedin.com/in/davidjameswalsh">LinkedIn</a> | <a
href="http://mootools.net/forge/profile/davidwalsh">MooTools Forge.</a><br/><br/>Full David Walsh Blog Post: <a
href="http://davidwalsh.name/animated-ajax-record-deletion-mootools">Animated AJAX Record Deletion Using&nbsp;MooTools</a></p><p>Related posts:<ol><li><a
href='http://davidwalsh.name/animated-ajax-jquery' rel='bookmark' title='Permanent Link: Animated AJAX Record Deletion Using&nbsp;jQuery'>Animated AJAX Record Deletion Using&nbsp;jQuery</a></li><li><a
href='http://davidwalsh.name/dojo-ajax' rel='bookmark' title='Permanent Link: Animated AJAX Record Deletion Using&nbsp;Dojo'>Animated AJAX Record Deletion Using&nbsp;Dojo</a></li><li><a
href='http://davidwalsh.name/text-selection-ajax' rel='bookmark' title='Permanent Link: Record Text Selections Using MooTools or jQuery&nbsp;AJAX'>Record Text Selections Using MooTools or jQuery&nbsp;AJAX</a></li><li><a
href='http://davidwalsh.name/editable-content-mootools-php-mysql' rel='bookmark' title='Permanent Link: Editable Content Using MooTools 1.2, PHP, and&nbsp;MySQL'>Editable Content Using MooTools 1.2, PHP, and&nbsp;MySQL</a></li><li><a
href='http://davidwalsh.name/basic-ajax-requests-mootools' rel='bookmark' title='Permanent Link: Basic AJAX Requests Using MooTools&nbsp;1.2'>Basic AJAX Requests Using MooTools&nbsp;1.2</a></li></ol></p>]]></content:encoded> <wfw:commentRss>http://davidwalsh.name/animated-ajax-record-deletion-mootools/feed</wfw:commentRss> <slash:comments>46</slash:comments> </item> <item><title>Breaking &amp; Fixing Dates from MySQL to&#160;PHP</title><link>http://davidwalsh.name/dates-mysql-php</link> <comments>http://davidwalsh.name/dates-mysql-php#comments</comments> <pubDate>Wed, 19 Nov 2008 12:05:55 +0000</pubDate> <dc:creator>David Walsh</dc:creator> <category><![CDATA[MySQL]]></category> <category><![CDATA[PHP]]></category><guid
isPermaLink="false">http://davidwalsh.name/?p=776</guid> <description><![CDATA[The way that databases store &#8220;date&#8221; field data is: 2008-11-19 // year - month - day The way that us humans read dates (at least in the U.S.) is: 11-19-2008 // month - day - year I write a lot of administrative panel modules that either display dates or, better yet, allow customers to their [...]<p><strong>Follow Me!</strong> <a
href="http://twitter.com/davidwalshblog">Twitter</a> | <a
href="http://www.facebook.com/#!/pages/David-Walsh-Blog/186644584869">Facebook</a> | <a
href="http://www.linkedin.com/in/davidjameswalsh">LinkedIn</a> | <a
href="http://mootools.net/forge/profile/davidwalsh">MooTools Forge.</a><br/><br/>Full David Walsh Blog Post: <a
href="http://davidwalsh.name/dates-mysql-php">Breaking &#038; Fixing Dates from MySQL to&nbsp;PHP</a></p>Related posts:<ol><li><a
href='http://davidwalsh.name/format-date-mysql-date_format' rel='bookmark' title='Permanent Link: Format Date Fields Using MySQL&nbsp;DATE_FORMAT()'>Format Date Fields Using MySQL&nbsp;DATE_FORMAT()</a></li><li><a
href='http://davidwalsh.name/add-dates-mysql' rel='bookmark' title='Permanent Link: Adding Days To Dates In&nbsp;MySQL'>Adding Days To Dates In&nbsp;MySQL</a></li><li><a
href='http://davidwalsh.name/backup-mysql-database-php' rel='bookmark' title='Permanent Link: Backup Your MySQL Database Using&nbsp;PHP'>Backup Your MySQL Database Using&nbsp;PHP</a></li><li><a
href='http://davidwalsh.name/mysql-date-add' rel='bookmark' title='Permanent Link: Quick Tip:  MySQL&nbsp;date_add'>Quick Tip:  MySQL&nbsp;date_add</a></li><li><a
href='http://davidwalsh.name/php-mysql-database-optimization-function' rel='bookmark' title='Permanent Link: PHP / MySQL Database Optimization&nbsp;Function'>PHP / MySQL Database Optimization&nbsp;Function</a></li></ol>]]></description> <content:encoded><![CDATA[<p>The way that databases store &#8220;date&#8221; field data is:</p><pre  class="js">
2008-11-19 // year - month - day
</pre><p>The way that us humans read dates (at least in the U.S.) is:</p><pre  class="js">
11-19-2008 // month - day - year
</pre><p>I write a lot of administrative panel modules that either display dates or, better yet, allow customers to their own dates for articles, events, etc.  I&#8217;ve created a couple of PHP functions that allow me to easily handle the transition from MySQL to PHP and visa versa.</p><h2>The PHP:  MySQL to&nbsp;PHP</h2><pre  class="php">
	/* break a date */
	function break_date($date)
	{
		$date = explode('-',str_replace('/','-',$date)); 
		return $date[1].'-'.$date[2].'-'.$date[0];
	}
</pre><h2>The PHP:  PHP to&nbsp;MySQL</h2><pre  class="php">
	/* fix a date */
	function fix_date($date)
	{
		$date = explode('-',str_replace('/','-',$date)); 
		return $date[2].'-'.$date[0].'-'.$date[1];
	}
</pre><p>You&#8217;ll see that I automatically replace &#8220;/&#8221; within the given date, break apart the date string, and rearrange the items to make the user and the database happy.  Hopefully this saves someone some time!</p><p><strong>Follow Me!</strong> <a
href="http://twitter.com/davidwalshblog">Twitter</a> | <a
href="http://www.facebook.com/#!/pages/David-Walsh-Blog/186644584869">Facebook</a> | <a
href="http://www.linkedin.com/in/davidjameswalsh">LinkedIn</a> | <a
href="http://mootools.net/forge/profile/davidwalsh">MooTools Forge.</a><br/><br/>Full David Walsh Blog Post: <a
href="http://davidwalsh.name/dates-mysql-php">Breaking &#038; Fixing Dates from MySQL to&nbsp;PHP</a></p><p>Related posts:<ol><li><a
href='http://davidwalsh.name/format-date-mysql-date_format' rel='bookmark' title='Permanent Link: Format Date Fields Using MySQL&nbsp;DATE_FORMAT()'>Format Date Fields Using MySQL&nbsp;DATE_FORMAT()</a></li><li><a
href='http://davidwalsh.name/add-dates-mysql' rel='bookmark' title='Permanent Link: Adding Days To Dates In&nbsp;MySQL'>Adding Days To Dates In&nbsp;MySQL</a></li><li><a
href='http://davidwalsh.name/backup-mysql-database-php' rel='bookmark' title='Permanent Link: Backup Your MySQL Database Using&nbsp;PHP'>Backup Your MySQL Database Using&nbsp;PHP</a></li><li><a
href='http://davidwalsh.name/mysql-date-add' rel='bookmark' title='Permanent Link: Quick Tip:  MySQL&nbsp;date_add'>Quick Tip:  MySQL&nbsp;date_add</a></li><li><a
href='http://davidwalsh.name/php-mysql-database-optimization-function' rel='bookmark' title='Permanent Link: PHP / MySQL Database Optimization&nbsp;Function'>PHP / MySQL Database Optimization&nbsp;Function</a></li></ol></p>]]></content:encoded> <wfw:commentRss>http://davidwalsh.name/dates-mysql-php/feed</wfw:commentRss> <slash:comments>38</slash:comments> </item> <item><title>Using MooTools 1.2 For Drag, Drop, Sort,&#160;Save</title><link>http://davidwalsh.name/mootools-drag-drop</link> <comments>http://davidwalsh.name/mootools-drag-drop#comments</comments> <pubDate>Fri, 14 Nov 2008 13:57:36 +0000</pubDate> <dc:creator>David Walsh</dc:creator> <category><![CDATA[AJAX]]></category> <category><![CDATA[CSS]]></category> <category><![CDATA[Markup]]></category> <category><![CDATA[MooTools]]></category> <category><![CDATA[MySQL]]></category> <category><![CDATA[PHP]]></category><guid
isPermaLink="false">http://davidwalsh.name/?p=328</guid> <description><![CDATA[This post has been updated: Using jQuery or MooTools For Drag, Drop, Sort, Save. The code on this page is no longer best practice. The following is a repost of an article that ran on Script &#038; Style a few months ago&#8230;. My customers love being able to control their website&#8217;s content so I build [...]<p><strong>Follow Me!</strong> <a
href="http://twitter.com/davidwalshblog">Twitter</a> | <a
href="http://www.facebook.com/#!/pages/David-Walsh-Blog/186644584869">Facebook</a> | <a
href="http://www.linkedin.com/in/davidjameswalsh">LinkedIn</a> | <a
href="http://mootools.net/forge/profile/davidwalsh">MooTools Forge.</a><br/><br/>Full David Walsh Blog Post: <a
href="http://davidwalsh.name/mootools-drag-drop">Using MooTools 1.2 For Drag, Drop, Sort,&nbsp;Save</a></p>Related posts:<ol><li><a
href='http://davidwalsh.name/mootools-drag-ajax' rel='bookmark' title='Permanent Link: Using jQuery or MooTools For Drag, Drop, Sort,&nbsp;Save'>Using jQuery or MooTools For Drag, Drop, Sort,&nbsp;Save</a></li><li><a
href='http://davidwalsh.name/mootools-drag-drop-lock' rel='bookmark' title='Permanent Link: Drag. Drop.&nbsp;Lock.'>Drag. Drop.&nbsp;Lock.</a></li><li><a
href='http://davidwalsh.name/drag-drop-stacking' rel='bookmark' title='Permanent Link: Drag and Drop Z-Index&nbsp;Stacking'>Drag and Drop Z-Index&nbsp;Stacking</a></li><li><a
href='http://davidwalsh.name/drag-drop-elements-trash-mootools' rel='bookmark' title='Permanent Link: Drag &#038; Drop Elements to the Trash with MooTools&nbsp;1.2'>Drag &#038; Drop Elements to the Trash with MooTools&nbsp;1.2</a></li><li><a
href='http://davidwalsh.name/mootools-scroller' rel='bookmark' title='Permanent Link: Create a Simple News Scroller Using MooTools, Part I:  The&nbsp;Basics'>Create a Simple News Scroller Using MooTools, Part I:  The&nbsp;Basics</a></li></ol>]]></description> <content:encoded><![CDATA[<p
class="post-updated">This post has been updated: <a
href="http://davidwalsh.name/mootools-drag-ajax">Using jQuery or MooTools For Drag, Drop, Sort, Save</a>.  The code on this page is no longer best practice.</p><p><em>The following is a repost of an article that ran on <a
href="http://scriptandstyle.com">Script &#038; Style</a> a few months ago&#8230;.</em></p><p> <a
href="http://davidwalsh.name/dw-content/sort-save.php"><img
class="image" src="http://davidwalsh.name/dw-content/sort-save.jpg" alt="" /></a> My customers love being able to control their website&#8217;s content so I build a lot of administrative control into their website.  One administrative control I frequently build is a News control.  I allow the customer to add, edit, delete, and sort news items.  My customers especially love sorting their articles because of the fashion of which they can sort:  drag and drop.   Here&#8217;s how I do it.</p><div
class="actions"> <a
href="http://davidwalsh.name/dw-content/sort-save.php" class="demo">View Demo</a><div
class="clear"></div></div><h2>The MySQL&nbsp;Table</h2><table
border="0" cellspacing="0" cellpadding="0" class="poll-results"><tr><th>id</th><th>title</th><th>sort_order</th></tr><tr><td>1</td><td>Article 1</td><td>1</td></tr><tr><td>2</td><td>Article 2</td><td>2</td></tr><tr><td>3</td><td>Article 3</td><td>3</td></tr><tr><td>4</td><td>Article 4</td><td>4</td></tr><tr><td>5</td><td>Article 5</td><td>5</td></tr><tr><td>6</td><td>Article 6</td><td>6</td></tr></table><p>My news table contains more fields but these are the important fields per this example.</p><h2>The PHP /&nbsp;XHTML</h2><pre  class="php">&lt;?php
&lt;div id="message-box"&gt;&lt;?php echo $message; ?&gt; Waiting for sortation submission...&lt;/div&gt;

&lt;form id="dd-form" action="&lt;?php echo $_SERVER['REQUEST_URI']; ?&gt;" method="post"&gt;
&lt;p&gt;&lt;input type="checkbox" value="1" name="auto_submit" id="auto_submit" &lt;?php if($_POST['auto_submit']) { echo 'checked="checked"'; } ?&gt; /&gt; &lt;label for="auto_submit"&gt;Automatically submit on drop event&lt;/label&gt;&lt;/p&gt;

&lt;ul id="sortable-list"&gt;
	&lt;?php 
		$sort_order = array();
		while($item = mysql_fetch_assoc($result))
		{
			echo '&lt;li class="sortme" alt="',$item['id'],'"&gt;',$item['title'],'&lt;/li&gt;';
			$sort_order[] = $item['sort_order'];
		}
	?&gt;
&lt;/ul&gt;
&lt;br /&gt;
&lt;input type="hidden" name="sort_order" id="sort_order" value="&lt;?php echo implode($sort_order,'|'); ?&gt;" /&gt;
&lt;input type="submit" name="do_submit" value="Submit Sortation" class="button" /&gt;
&lt;/form&gt;
&lt;?php } else { ?&gt;
	
	&lt;p&gt;Sorry!  There are no items in the system.&lt;/p&gt;
	
&lt;?php } ?&gt;
</pre><p>We query the database to get every news item.  What&#8217;s extremely important is that the query sorts the items by their original sort order.  We set the &#8220;rel&#8221; attribute equal to the article&#8217;s ID and the list item&#8217;s text to the article title.</p><h2>The&nbsp;CSS</h2><pre  class="css">#sortable-list				{ padding:0; }
li.sortme 		 			{ padding:4px 8px; color:#000; cursor:move; list-style:none; width:500px; background:#ddd; margin:10px 0; border:1px solid #999; }
#message-box				{ background:#fffea1; border:2px solid #fc0; padding:4px 8px; margin:0 0 14px 0; width:500px; }
</pre><p> I use the above CSS to format the news items so that the customer knows each news item may be dragged.  None of the CSS is essential to this system.</p><h2>The MooTools&nbsp;JavaScript</h2><pre  class="js">/* when the DOM is ready */
/* create sortables */
	var sb = new Sortables('sortable-list', {
		/* set options */
		clone:true,
		revert: true,
		/* initialization stuff here */
		initialize: function() { 
			
		},
		/* once an item is selected */
		onStart: function(el) { 
			el.setStyle('background','#add8e6');
		},
		/* when a drag is complete */
		onComplete: function(el) {
			el.setStyle('background','#ddd');
			//build a string of the order
			var sort_order = '';
			$$('#sortable-list li').each(function(li) { sort_order = sort_order +  li.get('alt')  + '|'; });
			$('sort_order').value = sort_order;
			
			//autosubmit if the checkbox says to
			if($('auto_submit').checked) {
				//do an ajax request
				var req = new Request({
					url:'<?php echo $_SERVER['PHP_SELF']; ?>',
					method:'post',
					autoCancel:true,
					data:'sort_order=' + sort_order + '&#038;ajax=' + $('auto_submit').checked + '&#038;do_submit=1&#038;byajax=1',
					onRequest: function() {
						$('message-box').set('text','Updating the sort order in the database.');
					},
					onSuccess: function() {
						$('message-box').set('text','Database has been updated.');
					}
				}).send();
			}
		}
	});
});
</pre><p>We use Moo 1.2&#8242;s Sortables plugin class to select all element within the list and make them sortable (drag and drop).  Every time the sort order is changed, the hidden <span
class="parameter">sort_order</span> element is built and reset using a &#8220;|&#8221; as a separator.  If the checkbox is checked, an ajax call is made to update the sort order in the database.  Otherwise, the regular form submission via submit button will also save the sortation.</p><h2>The &#8220;Header&#8221; PHP /&nbsp;MySQL</h2><pre  class="php">/* on form submission */
if(isset($_POST['do_submit'])) 
{
	/* split the value of the sortation */
	$ids = explode('|',$_POST['sort_order']);
	
	/* run the update query for each id */
	foreach($ids as $index=>$id)
	{
		if($id != '')
		{
			$query = 'UPDATE test_table SET sort_order = '.$index.' WHERE id = '.$id;
			$result = mysql_query($query,$connection) or die(mysql_error().': '.$query);
		}
	}
	
	/* now what? */
	if($_POST['byajax']) { die(); } else { $message = 'Sortation has been saved.'; }
}
</pre><div
class="actions"> <a
href="http://davidwalsh.name/dw-content/sort-save.php" class="demo">View Demo</a><div
class="clear"></div></div><p>The header is where the new sort order is committed.  We split the <span
class="param">sort_order</span> form value by the &#8220;|&#8221; and perform a query for each item to update its order.  Lastly, if the <span
class="param">byajax</span> flag is sent, we just die out the PHP script &#8212; if not, we continue to load the page.</p><p>Hot system, right? Dragging and dropping is by far the fastest way to sort a list of items.  What are your thoughts?  Have any ideas for improvements?</p><p><strong>Follow Me!</strong> <a
href="http://twitter.com/davidwalshblog">Twitter</a> | <a
href="http://www.facebook.com/#!/pages/David-Walsh-Blog/186644584869">Facebook</a> | <a
href="http://www.linkedin.com/in/davidjameswalsh">LinkedIn</a> | <a
href="http://mootools.net/forge/profile/davidwalsh">MooTools Forge.</a><br/><br/>Full David Walsh Blog Post: <a
href="http://davidwalsh.name/mootools-drag-drop">Using MooTools 1.2 For Drag, Drop, Sort,&nbsp;Save</a></p><p>Related posts:<ol><li><a
href='http://davidwalsh.name/mootools-drag-ajax' rel='bookmark' title='Permanent Link: Using jQuery or MooTools For Drag, Drop, Sort,&nbsp;Save'>Using jQuery or MooTools For Drag, Drop, Sort,&nbsp;Save</a></li><li><a
href='http://davidwalsh.name/mootools-drag-drop-lock' rel='bookmark' title='Permanent Link: Drag. Drop.&nbsp;Lock.'>Drag. Drop.&nbsp;Lock.</a></li><li><a
href='http://davidwalsh.name/drag-drop-stacking' rel='bookmark' title='Permanent Link: Drag and Drop Z-Index&nbsp;Stacking'>Drag and Drop Z-Index&nbsp;Stacking</a></li><li><a
href='http://davidwalsh.name/drag-drop-elements-trash-mootools' rel='bookmark' title='Permanent Link: Drag &#038; Drop Elements to the Trash with MooTools&nbsp;1.2'>Drag &#038; Drop Elements to the Trash with MooTools&nbsp;1.2</a></li><li><a
href='http://davidwalsh.name/mootools-scroller' rel='bookmark' title='Permanent Link: Create a Simple News Scroller Using MooTools, Part I:  The&nbsp;Basics'>Create a Simple News Scroller Using MooTools, Part I:  The&nbsp;Basics</a></li></ol></p>]]></content:encoded> <wfw:commentRss>http://davidwalsh.name/mootools-drag-drop/feed</wfw:commentRss> <slash:comments>61</slash:comments> </item> <item><title>Format Date Fields Using MySQL&#160;DATE_FORMAT()</title><link>http://davidwalsh.name/format-date-mysql-date_format</link> <comments>http://davidwalsh.name/format-date-mysql-date_format#comments</comments> <pubDate>Fri, 03 Oct 2008 12:51:01 +0000</pubDate> <dc:creator>David Walsh</dc:creator> <category><![CDATA[MySQL]]></category><guid
isPermaLink="false">http://davidwalsh.name/?p=410</guid> <description><![CDATA[I use MySQL DATE and DATETIME fields almost as much as VARCHAR fields. I use them mostly to track record creation, record updates, and record expiration dates. The default date format in MYSQL is: 2008-09-10 22:50:02 When I pull this information into the page, I want to show the date in human-readable format. That&#8217;s where [...]<p><strong>Follow Me!</strong> <a
href="http://twitter.com/davidwalshblog">Twitter</a> | <a
href="http://www.facebook.com/#!/pages/David-Walsh-Blog/186644584869">Facebook</a> | <a
href="http://www.linkedin.com/in/davidjameswalsh">LinkedIn</a> | <a
href="http://mootools.net/forge/profile/davidwalsh">MooTools Forge.</a><br/><br/>Full David Walsh Blog Post: <a
href="http://davidwalsh.name/format-date-mysql-date_format">Format Date Fields Using MySQL&nbsp;DATE_FORMAT()</a></p>Related posts:<ol><li><a
href='http://davidwalsh.name/dates-mysql-php' rel='bookmark' title='Permanent Link: Breaking &#038; Fixing Dates from MySQL to&nbsp;PHP'>Breaking &#038; Fixing Dates from MySQL to&nbsp;PHP</a></li><li><a
href='http://davidwalsh.name/mysql-date-add' rel='bookmark' title='Permanent Link: Quick Tip:  MySQL&nbsp;date_add'>Quick Tip:  MySQL&nbsp;date_add</a></li><li><a
href='http://davidwalsh.name/php-event-calendar' rel='bookmark' title='Permanent Link: Add Events to the PHP&nbsp;Calendar'>Add Events to the PHP&nbsp;Calendar</a></li><li><a
href='http://davidwalsh.name/php-calendar-controls' rel='bookmark' title='Permanent Link: Add Controls to the PHP&nbsp;Calendar'>Add Controls to the PHP&nbsp;Calendar</a></li><li><a
href='http://davidwalsh.name/php-validatie-numeric-digits' rel='bookmark' title='Permanent Link: PHP:  Validating Numeric Values and&nbsp;Digits'>PHP:  Validating Numeric Values and&nbsp;Digits</a></li></ol>]]></description> <content:encoded><![CDATA[<p>I use MySQL <span
class="param">DATE</span> and <span
class="param">DATETIME</span> fields almost as much as <span
class="param">VARCHAR</span> fields.  I use them mostly to track record creation, record updates, and record expiration dates.  The default date format in MYSQL is:</p><pre  class="sql">
2008-09-10 22:50:02
</pre><p>When I pull this information into the page, I want to show the date in human-readable format.  That&#8217;s where MySQL&#8217;s <span
class="param">DATE_FORMAT</span> functionality comes in. <span
class="param">DATE_FORMAT</span> allows me to format the date in any manner you&#8217;d like.</p><h2>MySQL DATE_FORMAT()&nbsp;Example</h2><pre  class="sql">
DATE_FORMAT(NOW(),'%W, %M %e, %Y @ %h:%i %p')
#yields 'Sunday, September 20, 2008 @ 12:45 PM'
</pre><h2>MySQL DATE_FORMAT() Letter&nbsp;Representations</h2><table
cellpadding="0" cellspacing="0" class="poll-results"><tr><th><span
class="bold"><strong>Specifier</strong></span></th><th><span
class="bold"><strong>Description</strong></span></th></tr><tr><td><code
class="literal"><b>%a</b></code></td><td>Abbreviated weekday name
(<code
class="literal">Sun</code>..<code
class="literal">Sat</code>)</td></tr><tr><td><code
class="literal"><b>%b</b></code></td><td>Abbreviated month name (<code
class="literal">Jan</code>..<code
class="literal">Dec</code>)</td></tr><tr><td><code
class="literal"><b>%c</b></code></td><td>Month, numeric (<code
class="literal">0</code>..<code
class="literal">12</code>)</td></tr><tr><td><code
class="literal"><b>%D</b></code></td><td>Day of the month with English suffix (<code
class="literal">0th</code>, <code
class="literal">1st</code>, <code
class="literal">2nd</code>, <code
class="literal">3rd</code>, …)</td></tr><tr><td><code
class="literal"><b>%d</b></code></td><td>Day of the month, numeric (<code
class="literal">00</code>..<code
class="literal">31</code>)</td></tr><tr><td><code
class="literal"><b>%e</b></code></td><td>Day of the month, numeric (<code
class="literal">0</code>..<code
class="literal">31</code>)</td></tr><tr><td><code
class="literal"><b>%f</b></code></td><td>Microseconds (<code
class="literal">000000</code>..<code
class="literal">999999</code>)</td></tr><tr><td><code
class="literal"><b>%H</b></code></td><td>Hour (<code
class="literal">00</code>..<code
class="literal">23</code>)</td></tr><tr><td><code
class="literal"><b>%h</b></code></td><td>Hour (<code
class="literal">01</code>..<code
class="literal">12</code>)</td></tr><tr><td><code
class="literal"><b>%I</b></code></td><td>Hour (<code
class="literal">01</code>..<code
class="literal">12</code>)</td></tr><tr><td><code
class="literal"><b>%i</b></code></td><td>Minutes, numeric (<code
class="literal">00</code>..<code
class="literal">59</code>)</td></tr><tr><td><code
class="literal"><b>%j</b></code></td><td>Day of year (<code
class="literal">001</code>..<code
class="literal">366</code>)</td></tr><tr><td><code
class="literal"><b>%k</b></code></td><td>Hour (<code
class="literal">0</code>..<code
class="literal">23</code>)</td></tr><tr><td><code
class="literal"><b>%l</b></code></td><td>Hour (<code
class="literal">1</code>..<code
class="literal">12</code>)</td></tr><tr><td><code
class="literal"><b>%M</b></code></td><td>Month name (<code
class="literal">January</code>..<code
class="literal">December</code>)</td></tr><tr><td><code
class="literal"><b>%m</b></code></td><td>Month, numeric (<code
class="literal">00</code>..<code
class="literal">12</code>)</td></tr><tr><td><code
class="literal"><b>%p</b></code></td><td> <code
class="literal">AM</code> or <code
class="literal">PM</code></td></tr><tr><td><code
class="literal"><b>%r</b></code></td><td>Time, 12-hour (<code
class="literal">hh:mm:ss</code> followed by <code
class="literal">AM</code> or <code
class="literal">PM</code>)</td></tr><tr><td><code
class="literal"><b>%S</b></code></td><td>Seconds (<code
class="literal">00</code>..<code
class="literal">59</code>)</td></tr><tr><td><code
class="literal"><b>%s</b></code></td><td>Seconds (<code
class="literal">00</code>..<code
class="literal">59</code>)</td></tr><tr><td><code
class="literal"><b>%T</b></code></td><td>Time, 24-hour (<code
class="literal">hh:mm:ss</code>)</td></tr><tr><td><code
class="literal"><b>%U</b></code></td><td>Week (<code
class="literal">00</code>..<code
class="literal">53</code>), where Sunday is the
first day of the week</td></tr><tr><td><code
class="literal"><b>%u</b></code></td><td>Week (<code
class="literal">00</code>..<code
class="literal">53</code>), where Monday is the
first day of the week</td></tr><tr><td><code
class="literal"><b>%V</b></code></td><td>Week (<code
class="literal">01</code>..<code
class="literal">53</code>), where Sunday is the
first day of the week; used with <code
class="literal">%X</code></td></tr><tr><td><code
class="literal"><b>%v</b></code></td><td>Week (<code
class="literal">01</code>..<code
class="literal">53</code>), where Monday is the
first day of the week; used with <code
class="literal">%x</code></td></tr><tr><td><code
class="literal"><b>%W</b></code></td><td>Weekday name (<code
class="literal">Sunday</code>..<code
class="literal">Saturday</code>)</td></tr><tr><td><code
class="literal"><b>%w</b></code></td><td>Day of the week
(<code
class="literal">0</code>=Sunday..<code
class="literal">6</code>=Saturday)</td></tr><tr><td><code
class="literal"><b>%X</b></code></td><td>Year for the week where Sunday is the first day of the week, numeric,
four digits; used with <code
class="literal">%V</code></td></tr><tr><td><code
class="literal"><b>%x</b></code></td><td>Year for the week, where Monday is the first day of the week, numeric,
four digits; used with <code
class="literal">%v</code></td></tr><tr><td><code
class="literal"><b>%Y</b></code></td><td>Year, numeric, four digits</td></tr><tr><td><code
class="literal"><b>%y</b></code></td><td>Year, numeric (two digits)</td></tr><tr><td><code
class="literal"><b>%%</b></code></td><td>A literal “<span
class="quote"><code
class="literal">%</code></span>” character</td></tr><tr><td><code
class="literal"><b>%<em
class="replaceable"><code>x</code></em></b></code></td><td> <em
class="replaceable"><code>x</code></em>, for any
“<span
class="quote"><em
class="replaceable"><code>x</code></em></span>” not listed
above</td></tr></table><p>Be kind to your users and format the date for them!</p><p><strong>Follow Me!</strong> <a
href="http://twitter.com/davidwalshblog">Twitter</a> | <a
href="http://www.facebook.com/#!/pages/David-Walsh-Blog/186644584869">Facebook</a> | <a
href="http://www.linkedin.com/in/davidjameswalsh">LinkedIn</a> | <a
href="http://mootools.net/forge/profile/davidwalsh">MooTools Forge.</a><br/><br/>Full David Walsh Blog Post: <a
href="http://davidwalsh.name/format-date-mysql-date_format">Format Date Fields Using MySQL&nbsp;DATE_FORMAT()</a></p><p>Related posts:<ol><li><a
href='http://davidwalsh.name/dates-mysql-php' rel='bookmark' title='Permanent Link: Breaking &#038; Fixing Dates from MySQL to&nbsp;PHP'>Breaking &#038; Fixing Dates from MySQL to&nbsp;PHP</a></li><li><a
href='http://davidwalsh.name/mysql-date-add' rel='bookmark' title='Permanent Link: Quick Tip:  MySQL&nbsp;date_add'>Quick Tip:  MySQL&nbsp;date_add</a></li><li><a
href='http://davidwalsh.name/php-event-calendar' rel='bookmark' title='Permanent Link: Add Events to the PHP&nbsp;Calendar'>Add Events to the PHP&nbsp;Calendar</a></li><li><a
href='http://davidwalsh.name/php-calendar-controls' rel='bookmark' title='Permanent Link: Add Controls to the PHP&nbsp;Calendar'>Add Controls to the PHP&nbsp;Calendar</a></li><li><a
href='http://davidwalsh.name/php-validatie-numeric-digits' rel='bookmark' title='Permanent Link: PHP:  Validating Numeric Values and&nbsp;Digits'>PHP:  Validating Numeric Values and&nbsp;Digits</a></li></ol></p>]]></content:encoded> <wfw:commentRss>http://davidwalsh.name/format-date-mysql-date_format/feed</wfw:commentRss> <slash:comments>15</slash:comments> </item> <item><title>Backup Your MySQL Database Using&#160;PHP</title><link>http://davidwalsh.name/backup-mysql-database-php</link> <comments>http://davidwalsh.name/backup-mysql-database-php#comments</comments> <pubDate>Mon, 18 Aug 2008 13:26:14 +0000</pubDate> <dc:creator>David Walsh</dc:creator> <category><![CDATA[MySQL]]></category> <category><![CDATA[PHP]]></category><guid
isPermaLink="false">http://davidwalsh.name/?p=336</guid> <description><![CDATA[One of the most important tasks any developer needs to do often is back up their MySQL database. In many cases, the database is what drives most of the site. While most web hosts do a daily backup of a customer&#8217;s database, relying on them to make backups and provide them at no cost is [...]<p><strong>Follow Me!</strong> <a
href="http://twitter.com/davidwalshblog">Twitter</a> | <a
href="http://www.facebook.com/#!/pages/David-Walsh-Blog/186644584869">Facebook</a> | <a
href="http://www.linkedin.com/in/davidjameswalsh">LinkedIn</a> | <a
href="http://mootools.net/forge/profile/davidwalsh">MooTools Forge.</a><br/><br/>Full David Walsh Blog Post: <a
href="http://davidwalsh.name/backup-mysql-database-php">Backup Your MySQL Database Using&nbsp;PHP</a></p>Related posts:<ol><li><a
href='http://davidwalsh.name/php-mysql-database-optimization-function' rel='bookmark' title='Permanent Link: PHP / MySQL Database Optimization&nbsp;Function'>PHP / MySQL Database Optimization&nbsp;Function</a></li><li><a
href='http://davidwalsh.name/html-mysql-php' rel='bookmark' title='Permanent Link: Build HTML Tables From MySQL Tables with&nbsp;PHP'>Build HTML Tables From MySQL Tables with&nbsp;PHP</a></li><li><a
href='http://davidwalsh.name/backup-database-xml-php' rel='bookmark' title='Permanent Link: Backup Your Database into an XML File Using&nbsp;PHP'>Backup Your Database into an XML File Using&nbsp;PHP</a></li><li><a
href='http://davidwalsh.name/web-service-php-mysql-xml-json' rel='bookmark' title='Permanent Link: Create a Basic Web Service Using PHP, MySQL, XML, and&nbsp;JSON'>Create a Basic Web Service Using PHP, MySQL, XML, and&nbsp;JSON</a></li><li><a
href='http://davidwalsh.name/return-random-records-mysql' rel='bookmark' title='Permanent Link: Return Random Records in&nbsp;MySQL'>Return Random Records in&nbsp;MySQL</a></li></ol>]]></description> <content:encoded><![CDATA[<p>One of the most important tasks any developer needs to do often is back up their MySQL database.  In many cases, the database is what drives most of the site.  While most web hosts do a daily backup of a customer&#8217;s database, relying on them to make backups and provide them at no cost is risky to say the least.  That&#8217;s why I&#8217;ve created a database backup function that I can call whenever I want &#8212; including nightly CRONs.</p><h2>The PHP &amp; MySQL&nbsp;Code</h2><pre  class="php">
backup_tables('localhost','username','password','blog');


/* backup the db OR just a table */
function backup_tables($host,$user,$pass,$name,$tables = '*')
{
	
	$link = mysql_connect($host,$user,$pass);
	mysql_select_db($name,$link);
	
	//get all of the tables
	if($tables == '*')
	{
		$tables = array();
		$result = mysql_query('SHOW TABLES');
		while($row = mysql_fetch_row($result))
		{
			$tables[] = $row[0];
		}
	}
	else
	{
		$tables = is_array($tables) ? $tables : explode(',',$tables);
	}
	
	//cycle through
	foreach($tables as $table)
	{
		$result = mysql_query('SELECT * FROM '.$table);
		$num_fields = mysql_num_fields($result);
		
		$return.= 'DROP TABLE '.$table.';';
		$row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE '.$table));
		$return.= "\n\n".$row2[1].";\n\n";
		
		for ($i = 0; $i < $num_fields; $i++) 
		{
			while($row = mysql_fetch_row($result))
			{
				$return.= 'INSERT INTO '.$table.' VALUES(';
				for($j=0; $j<$num_fields; $j++) 
				{
					$row[$j] = addslashes($row[$j]);
					$row[$j] = ereg_replace("\n","\\n",$row[$j]);
					if (isset($row[$j])) { $return.= '"'.$row[$j].'"' ; } else { $return.= '""'; }
					if ($j<($num_fields-1)) { $return.= ','; }
				}
				$return.= ");\n";
			}
		}
		$return.="\n\n\n";
	}
	
	//save file
	$handle = fopen('db-backup-'.time().'-'.(md5(implode(',',$tables))).'.sql','w+');
	fwrite($handle,$return);
	fclose($handle);
}
</pre><p>Of course, you'll need to provide database credentials to the function, as well as an array of tables you'd like to backup.  If you provide a "*" or no tables, a complete database backup will run.  The script does the rest!</p><p>Never take chances when your website is on the line.  Make frequent backups or pay the price later!</p><p><strong>Follow Me!</strong> <a
href="http://twitter.com/davidwalshblog">Twitter</a> | <a
href="http://www.facebook.com/#!/pages/David-Walsh-Blog/186644584869">Facebook</a> | <a
href="http://www.linkedin.com/in/davidjameswalsh">LinkedIn</a> | <a
href="http://mootools.net/forge/profile/davidwalsh">MooTools Forge.</a><br/><br/>Full David Walsh Blog Post: <a
href="http://davidwalsh.name/backup-mysql-database-php">Backup Your MySQL Database Using&nbsp;PHP</a></p><p>Related posts:<ol><li><a
href='http://davidwalsh.name/php-mysql-database-optimization-function' rel='bookmark' title='Permanent Link: PHP / MySQL Database Optimization&nbsp;Function'>PHP / MySQL Database Optimization&nbsp;Function</a></li><li><a
href='http://davidwalsh.name/html-mysql-php' rel='bookmark' title='Permanent Link: Build HTML Tables From MySQL Tables with&nbsp;PHP'>Build HTML Tables From MySQL Tables with&nbsp;PHP</a></li><li><a
href='http://davidwalsh.name/backup-database-xml-php' rel='bookmark' title='Permanent Link: Backup Your Database into an XML File Using&nbsp;PHP'>Backup Your Database into an XML File Using&nbsp;PHP</a></li><li><a
href='http://davidwalsh.name/web-service-php-mysql-xml-json' rel='bookmark' title='Permanent Link: Create a Basic Web Service Using PHP, MySQL, XML, and&nbsp;JSON'>Create a Basic Web Service Using PHP, MySQL, XML, and&nbsp;JSON</a></li><li><a
href='http://davidwalsh.name/return-random-records-mysql' rel='bookmark' title='Permanent Link: Return Random Records in&nbsp;MySQL'>Return Random Records in&nbsp;MySQL</a></li></ol></p>]]></content:encoded> <wfw:commentRss>http://davidwalsh.name/backup-mysql-database-php/feed</wfw:commentRss> <slash:comments>97</slash:comments> </item> <item><title>Editable Content Using MooTools 1.2, PHP, and&#160;MySQL</title><link>http://davidwalsh.name/editable-content-mootools-php-mysql</link> <comments>http://davidwalsh.name/editable-content-mootools-php-mysql#comments</comments> <pubDate>Thu, 26 Jun 2008 12:42:56 +0000</pubDate> <dc:creator>David Walsh</dc:creator> <category><![CDATA[AJAX]]></category> <category><![CDATA[CSS]]></category> <category><![CDATA[Markup]]></category> <category><![CDATA[MooTools]]></category> <category><![CDATA[MySQL]]></category> <category><![CDATA[PHP]]></category><guid
isPermaLink="false">http://davidwalsh.name/?p=309</guid> <description><![CDATA[Everybody and their aerobics instructor wants to be able to edit their own website these days. And why wouldn&#8217;t they? I mean, they have a $500 budget, no HTML/CSS experience, and extraordinary expectations. Enough ranting though. Having a website that allows for editable content blocks is the dream of many customers. I&#8217;ve taken a few [...]<p><strong>Follow Me!</strong> <a
href="http://twitter.com/davidwalshblog">Twitter</a> | <a
href="http://www.facebook.com/#!/pages/David-Walsh-Blog/186644584869">Facebook</a> | <a
href="http://www.linkedin.com/in/davidjameswalsh">LinkedIn</a> | <a
href="http://mootools.net/forge/profile/davidwalsh">MooTools Forge.</a><br/><br/>Full David Walsh Blog Post: <a
href="http://davidwalsh.name/editable-content-mootools-php-mysql">Editable Content Using MooTools 1.2, PHP, and&nbsp;MySQL</a></p>Related posts:<ol><li><a
href='http://davidwalsh.name/control-save' rel='bookmark' title='Permanent Link: Save Web Form Content Using Control +&nbsp;S'>Save Web Form Content Using Control +&nbsp;S</a></li><li><a
href='http://davidwalsh.name/mootools-drag-drop' rel='bookmark' title='Permanent Link: Using MooTools 1.2 For Drag, Drop, Sort,&nbsp;Save'>Using MooTools 1.2 For Drag, Drop, Sort,&nbsp;Save</a></li><li><a
href='http://davidwalsh.name/mootools-autocomplete' rel='bookmark' title='Permanent Link: MooTools&#8217; AutoCompleter&nbsp;Plugin'>MooTools&#8217; AutoCompleter&nbsp;Plugin</a></li><li><a
href='http://davidwalsh.name/mootools-form-field-default-plugin' rel='bookmark' title='Permanent Link: MooTools Form Field Default&nbsp;Plugin'>MooTools Form Field Default&nbsp;Plugin</a></li><li><a
href='http://davidwalsh.name/jquery-comment-preview' rel='bookmark' title='Permanent Link: jQuery Comment&nbsp;Preview'>jQuery Comment&nbsp;Preview</a></li></ol>]]></description> <content:encoded><![CDATA[<p>Everybody and their aerobics instructor wants to be able to edit their own website these days.  And why wouldn&#8217;t they?  I mean, they have a $500 budget, no HTML/CSS experience, and extraordinary expectations.  Enough ranting though.  Having a website that allows for editable content blocks is the dream of many customers.  I&#8217;ve taken a few hours to develop a system for in-page, editable content blocks.</p><div
class="actions"> <a
href="http://davidwalsh.name/dw-content/mootools-editable-content.php" class="demo">View Demo</a><div
class="clear"></div></div><h2>The&nbsp;XHTML</h2><pre  class="html">
&lt;h1 class="editable" rel="32" title="Article Title"&gt;Editable Title&lt;/h1&gt;
&lt;p class="editable textarea" title="Content Paragraph" rel="33"&gt;
	This is an editable paragraph.  This is an editable paragraph.  This is an editable paragraph.  This is an editable paragraph.
	This is an editable paragraph.  This is an editable paragraph.  This is an editable paragraph.  This is an editable paragraph.
	This is an editable paragraph.  This is an editable paragraph.  This is an editable paragraph.  This is an editable paragraph.
	This is an editable paragraph.  This is an editable paragraph.  This is an editable paragraph.  This is an editable paragraph.
	This is an editable paragraph.  This is an editable paragraph.  This is an editable paragraph.  This is an editable paragraph.
	This is an editable paragraph.  This is an editable paragraph.  This is an editable paragraph.  This is an editable paragraph.
&lt;/p&gt;
</pre><p>Note that any editable area is given the <span
class="parameter">editable<span> class.  The &#8220;rel&#8221; attribute reflects the ID of the record that will be updated in the database.  Also, if we want the form element to be a textarea instead of an input, we&#8217;ll give the editable element the &#8220;textarea&#8221; class.</p><h2>The&nbsp;CSS</h2><pre  class="css">.editable:hover	{ background:#eee; }
.textarea textarea{ height:200px; padding:3px; }
.editable-empty	{ background:#fffea1; padding:20px; border:1px dashed #fc0; }
.box		{ border:1px solid #ccc; padding:5px; display:block; width:95%; }</pre><p>The &#8220;box&#8221; class gets applied to both inputs and textareas.  The rest is for gloss factor.</p><h2>The MooTools&nbsp;JavaScript</h2><pre  class="js">//once the dom is ready
window.addEvent('domready', function() {
	//find the editable areas
	$$('.editable').each(function(el) {
		//add double-click and blur events
		el.addEvent('dblclick',function() {
			//store "before" message
			var before = el.get('html').trim();
			//erase current
			el.set('html','');
			//replace current text/content with input or textarea element
			if(el.hasClass('textarea'))
			{
				var input = new Element('textarea', { 'class':'box', 'text':before });
			}
			else
			{
				var input = new Element('input', { 'class':'box', 'value':before });
				//blur input when they press "Enter"
				input.addEvent('keydown', function(e) { if(e.key == 'enter') { this.fireEvent('blur'); } });
			}
			input.inject(el).select();
			//add blur event to input
			input.addEvent('blur', function() {
				//get value, place it in original element
				val = input.get('value').trim();
				el.set('text',val).addClass(val != '' ? '' : 'editable-empty');
				
				//save respective record
				var url = 'mootools-editable-content.php?id=' + el.get('rel') + '&#038;content=' + el.get('text');
				var request = new Request({
					url:url,
					method:'post',
					onRequest: function() {
						alert('making ajax call :: ' + url);
					}
				}).send();
			});
		});
	});
});</pre><p>Once the DOM is ready, we find all of the elements with the <span
class="parameter">editable</span> class.  We attach a &#8220;doubleclick&#8221; event to each editable item that inserts an input element (or textarea, depending on whether the original elements has the &#8220;textarea&#8221; class).  Once we inject the input/textarea element, we attach a &#8220;blur&#8221; event to the form element which triggers an AJAX request that will save the content.</p><h2>The PHP &amp;&nbsp;MySQL</h2><pre  class="php">
if($_SESSION['is_admin'] &#038;&#038; is_numeric($_POST['id']) &#038;&#038; isset($_POST['content']))
{
	$query = "UPDATE content_table SET content = '".mysql_real_escape_string(stripslashes($_POST['content']))."' WHERE content_id = ".(int)$_POST['id'];
	$result = mysql_query($query,$db_link);
}
</pre><p>When the PHP file receives the $_POST request, it validates credentials and updates the database with the new content.  Simple! <a
href="http://davidwalsh.name/dw-content/mootools-editable-content.php">Click here</a> to see it in action!</p><div
class="actions"> <a
href="http://davidwalsh.name/dw-content/mootools-editable-content.php" class="demo">View Demo</a><div
class="clear"></div></div><p>A few notes about the code.  First, this system isn&#8217;t meant to be for a big website.  This system requires that editable content placed inside of a database with a unique ID for each area.  Ideally, you&#8217;d use this one a small, simple site.  Second, the textarea does not have a WYSIWYG editor attached to it so the editor (person) would need some knowledge of HTML if they wanted to add links and such.  If you do want them to have that capability, download and configure FCKEditor, TinyMCE, or WMD.</p><p>Thoughts?  Is this rubbish?  Useful?  Hit me!</p><p><strong>Follow Me!</strong> <a
href="http://twitter.com/davidwalshblog">Twitter</a> | <a
href="http://www.facebook.com/#!/pages/David-Walsh-Blog/186644584869">Facebook</a> | <a
href="http://www.linkedin.com/in/davidjameswalsh">LinkedIn</a> | <a
href="http://mootools.net/forge/profile/davidwalsh">MooTools Forge.</a><br/><br/>Full David Walsh Blog Post: <a
href="http://davidwalsh.name/editable-content-mootools-php-mysql">Editable Content Using MooTools 1.2, PHP, and&nbsp;MySQL</a></p><p>Related posts:<ol><li><a
href='http://davidwalsh.name/control-save' rel='bookmark' title='Permanent Link: Save Web Form Content Using Control +&nbsp;S'>Save Web Form Content Using Control +&nbsp;S</a></li><li><a
href='http://davidwalsh.name/mootools-drag-drop' rel='bookmark' title='Permanent Link: Using MooTools 1.2 For Drag, Drop, Sort,&nbsp;Save'>Using MooTools 1.2 For Drag, Drop, Sort,&nbsp;Save</a></li><li><a
href='http://davidwalsh.name/mootools-autocomplete' rel='bookmark' title='Permanent Link: MooTools&#8217; AutoCompleter&nbsp;Plugin'>MooTools&#8217; AutoCompleter&nbsp;Plugin</a></li><li><a
href='http://davidwalsh.name/mootools-form-field-default-plugin' rel='bookmark' title='Permanent Link: MooTools Form Field Default&nbsp;Plugin'>MooTools Form Field Default&nbsp;Plugin</a></li><li><a
href='http://davidwalsh.name/jquery-comment-preview' rel='bookmark' title='Permanent Link: jQuery Comment&nbsp;Preview'>jQuery Comment&nbsp;Preview</a></li></ol></p>]]></content:encoded> <wfw:commentRss>http://davidwalsh.name/editable-content-mootools-php-mysql/feed</wfw:commentRss> <slash:comments>60</slash:comments> </item> </channel> </rss>
<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk
Page Caching using disk (enhanced) (user agent is rejected)
Database Caching 141/391 queries in 6.542 seconds using disk

Served from: davidwalsh.name @ 2010-09-02 23:11:55 -->