<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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/" version="2.0">

<channel>
	<title>Scriptish</title>
	
	<link>http://scriptish.com</link>
	<description>Just another WordPress site</description>
	<lastBuildDate>Sun, 26 Feb 2012 20:27:03 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/scriptish_rss" /><feedburner:info xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" uri="scriptish_rss" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>MySQL in Python</title>
		<link>http://scriptish.com/2012/02/26/mysql-in-python/</link>
		<comments>http://scriptish.com/2012/02/26/mysql-in-python/#comments</comments>
		<pubDate>Sun, 26 Feb 2012 20:27:03 +0000</pubDate>
		<dc:creator>Martti Laine</dc:creator>
				<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://scriptish.com/?p=143</guid>
		<description><![CDATA[Connecting to and using a MySQL-database in Python is easy. Here&#8217;s a full example code with comments: # MySQLdb needs to be installed import MySQLdb # Connect to a database db = MySQLdb.connect(host='localhost', user='username', passwd='password', db='database') # Create a cursor cursor = db.cursor() # Perform a query (returns the amount of rows affected/found) num_rows = [...]]]></description>
			<content:encoded><![CDATA[<p>Connecting to and using a MySQL-database in Python is easy. Here&#8217;s a full example code with comments:</p>

<pre class="prettyprint"><code># MySQLdb needs to be installed
import MySQLdb

# Connect to a database
db = MySQLdb.connect(host='localhost', user='username', passwd='password', db='database')

# Create a cursor
cursor = db.cursor()

# Perform a query (returns the amount of rows affected/found)
num_rows = cursor.execute(query)

# Work with the results

# Close the connection
db.close()
</code></pre>

<p>If you do a <code>SELECT</code>-query, you can either retrieve one row or loop through all of them. Here&#8217;s an example of both:</p>

<pre class="prettyprint"><code># Fetch only one row (mostly used for e.g. "SELECT COUNT(*)")
data = cursor.fetchone()

# Loop through all rows
data = cursor.fetchall()
for row in data:
    # ...
</code></pre>

<p>All rows are represented as an array, <strong>without keys</strong>:</p>

<pre class="prettyprint"><code>['First column', 'Another column', 'Etc...']
</code></pre>
]]></content:encoded>
			<wfw:commentRss>http://scriptish.com/2012/02/26/mysql-in-python/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>WordPress Theme Development – Cheatsheet</title>
		<link>http://scriptish.com/2012/02/07/wordpress-theme-development-cheatsheet/</link>
		<comments>http://scriptish.com/2012/02/07/wordpress-theme-development-cheatsheet/#comments</comments>
		<pubDate>Tue, 07 Feb 2012 10:26:08 +0000</pubDate>
		<dc:creator>Martti Laine</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://scriptish.com/?p=140</guid>
		<description><![CDATA[I take the chance to promote a one-pager I just published: WordPress Theme Development Cheatsheet. It contains handy snippets for reference while making awesome WordPress themes. Any suggestions/modifications are welcome! =)]]></description>
			<content:encoded><![CDATA[<p>I take the chance to promote a one-pager I just published: <a href="http://codeclown.net/wp-theme-cheatsheet">WordPress Theme Development Cheatsheet</a>.</p>

<p>It contains handy snippets for reference while making awesome WordPress themes.</p>

<p>Any suggestions/modifications are welcome! =)</p>
]]></content:encoded>
			<wfw:commentRss>http://scriptish.com/2012/02/07/wordpress-theme-development-cheatsheet/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JSON via $.post()</title>
		<link>http://scriptish.com/2011/12/28/json-via-post/</link>
		<comments>http://scriptish.com/2011/12/28/json-via-post/#comments</comments>
		<pubDate>Wed, 28 Dec 2011 21:15:16 +0000</pubDate>
		<dc:creator>Martti Laine</dc:creator>
				<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://scriptish.com/?p=125</guid>
		<description><![CDATA[If you ever needed to transfer JSON to front-end with jQuery, you are probably familiar with $.getJSON(). But what if you want to send POST-input to back-end? You could always use the original method, for which $.getJSON() etc. are shortcuts for, like this: $.ajax({ url: url, type: 'POST', dataType: 'json', data: data, success: callback }); [...]]]></description>
			<content:encoded><![CDATA[<p>If you ever needed to transfer JSON to front-end with jQuery, you are probably familiar with <code>$.getJSON()</code>. But what if you want to send POST-input to back-end? You could always use the original method, for which <code>$.getJSON()</code> etc. are shortcuts for, like this:</p>

<pre class="prettyprint"><code>$.ajax({
    url: url,
    type: 'POST',
    dataType: 'json',
    data: data,
    success: callback
});
</code></pre>

<p>But you can also add a 4th parameter to <code>$.post()</code>. See this example:</p>

<pre class="prettyprint"><code>$.post(url, data, callback, 'json');
</code></pre>

<p>This will do the same trick.</p>
]]></content:encoded>
			<wfw:commentRss>http://scriptish.com/2011/12/28/json-via-post/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Opera Transition Bug</title>
		<link>http://scriptish.com/2011/12/18/opera-transition-bug/</link>
		<comments>http://scriptish.com/2011/12/18/opera-transition-bug/#comments</comments>
		<pubDate>Sun, 18 Dec 2011 19:36:36 +0000</pubDate>
		<dc:creator>Martti Laine</dc:creator>
				<category><![CDATA[CSS]]></category>

		<guid isPermaLink="false">http://scriptish.com/?p=117</guid>
		<description><![CDATA[See this fiddle in Opera: http://jsfiddle.net/mzk3X/ You can see that the color in the animation makes a weird shift to dark. This happens if the background is set to transparent. Here&#8217;s the problem fixed (background set): http://jsfiddle.net/mzk3X/1/]]></description>
			<content:encoded><![CDATA[<p>See this fiddle in Opera: <a href="http://jsfiddle.net/mzk3X/">http://jsfiddle.net/mzk3X/</a></p>

<p>You can see that the color in the animation makes a weird shift to dark. This happens if the <code>background</code> is set to <code>transparent</code>.</p>

<p>Here&#8217;s the problem fixed (<code>background</code> set): <a href="http://jsfiddle.net/mzk3X/1/">http://jsfiddle.net/mzk3X/1/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://scriptish.com/2011/12/18/opera-transition-bug/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Refresh draggable positions in jQuery</title>
		<link>http://scriptish.com/2011/12/05/refresh-draggable-positions-in-jquery/</link>
		<comments>http://scriptish.com/2011/12/05/refresh-draggable-positions-in-jquery/#comments</comments>
		<pubDate>Mon, 05 Dec 2011 13:03:19 +0000</pubDate>
		<dc:creator>Martti Laine</dc:creator>
				<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://scriptish.com/?p=114</guid>
		<description><![CDATA[I recently ran into a situation where I had to combine sortable and droppable. One issue occurred: when moving the list item (and manipulating the DOM) the droppable areas did not update. This resulted in weird behaviour. Here&#8217;s a fiddle. If you move any list item so that a droppable item moves, you will still [...]]]></description>
			<content:encoded><![CDATA[<p>I recently ran into a situation where I had to combine <code>sortable</code> and <code>droppable</code>. One issue occurred: when moving the list item (and manipulating the DOM) the droppable areas did not update. This resulted in weird behaviour.</p>

<p>Here&#8217;s a <a href="http://jsfiddle.net/NRkwx/1/">fiddle</a>. If you move any list item so that a droppable item moves, you will still be able to drop to that specific element from its old position, not from the new one. I needed to refresh the droppable area position. I searched and ended up in tearing the jQuery UI source. Suddenly I found this inside the <code>draggable</code>:</p>

<pre class="prettyprint"><code>//If you have a highly dynamic page, you might try this option. It renders positions every time you move the mouse.
if(draggable.options.refreshPositions) $.ui.ddmanager.prepareOffsets(draggable, event);
</code></pre>

<p>The problem was <a href="http://jsfiddle.net/NRkwx/2/">solved</a> by adding the option <code>refreshPositions</code> for the <code>sortable</code>:</p>

<pre class="prettyprint"><code>$('#sortable').sortable({
    refreshPositions: true
});
</code></pre>

<p>With <code>refreshPositions</code> set to true the positions will be updated every time the mouse is moved.</p>
]]></content:encoded>
			<wfw:commentRss>http://scriptish.com/2011/12/05/refresh-draggable-positions-in-jquery/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Display latest songs from last.fm in PHP</title>
		<link>http://scriptish.com/2011/12/03/display-latest-songs-last-fm-php/</link>
		<comments>http://scriptish.com/2011/12/03/display-latest-songs-last-fm-php/#comments</comments>
		<pubDate>Sat, 03 Dec 2011 13:31:10 +0000</pubDate>
		<dc:creator>Martti Laine</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[last.fm php music]]></category>

		<guid isPermaLink="false">http://scriptish.com/?p=111</guid>
		<description><![CDATA[Want to display a list of recent plays in last.fm on your website? Last.fm API makes it easy. To start you need to have an API key, you can do that here. Copy your API key and then let&#8217;s get on with the code. No authentication is needed. Remember to add your own API key. [...]]]></description>
			<content:encoded><![CDATA[<p>Want to display a list of recent plays in last.fm on your website? Last.fm API makes it easy. To start you need to have an API key, you can do that <a href="http://www.last.fm/api">here</a>. Copy your API key and then let&#8217;s get on with the code.</p>

<p>No authentication is needed. Remember to add your own API key.</p>

<pre class="prettyprint"><code>$api_key = '';
$username = 'codeclown';
$limit = 3;

/* Construct the url */
$url = 'http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&amp;user='.$username.'&amp;api_key='.$api_key.'&amp;limit='.$limit;

/* Make a cURL-request (cURL is prefered over e.g. `file_get_contents`) */
$curl = curl_init();
curl_setopt($curl,CURLOPT_HEADER, 0);
curl_setopt($curl,CURLOPT_URL,$url);
curl_setopt($curl,CURLOPT_RETURNTRANSFER,true);
$result = curl_exec($curl);
curl_close($curl);

/* Process the results with `SimpleXMLElement` */
$xml = new SimpleXMLElement($result);
foreach($xml-&gt;recenttracks-&gt;track as $track) {

    /* Data in variables */
    $artist = $track-&gt;artist;
    $name = $track-&gt;name;
    $album = $track-&gt;album;
    $lastfm_url = $track-&gt;url;

    /* This does not exist, if the track is being currently played */
    $date = $track-&gt;date;

    /* An array that contains 4 different sizes */
    $albumart = $track-&gt;image;

    /* Nowplaying is embedded in attributes */
    $nowplaying = (bool) $track-&gt;attributes()-&gt;nowplaying;

    /* Use the data here */
    echo $artist.' - '.$name.'&lt;hr /&gt;';

}
</code></pre>
]]></content:encoded>
			<wfw:commentRss>http://scriptish.com/2011/12/03/display-latest-songs-last-fm-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Add a non-breaking space between two last words</title>
		<link>http://scriptish.com/2011/10/20/non-breaking-space-between-last-words-php/</link>
		<comments>http://scriptish.com/2011/10/20/non-breaking-space-between-last-words-php/#comments</comments>
		<pubDate>Thu, 20 Oct 2011 20:22:50 +0000</pubDate>
		<dc:creator>Martti Laine</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://scriptish.com/?p=98</guid>
		<description><![CDATA[This is very useful snippet, that removes the situation in which a header happens to be a bit too wide, and the last word will wrap to next line. This piece of code add a non-breaking space (&#38;nbsp;) between the last two words, so that if something wraps, it&#8217;s two words instead of one. $title [...]]]></description>
			<content:encoded><![CDATA[<p>This is very useful snippet, that removes the situation in which a header happens to be a bit too wide, and the last word will wrap to next line. This piece of code add a non-breaking space (<code>&amp;nbsp;</code>) between the last two words, so that if something wraps, it&#8217;s two words instead of one.</p>

<pre class="prettyprint"><code>$title = preg_replace('/(.*)\s([^\s]+)/', '$1&amp;nbsp;$2', $original_title);
</code></pre>

<p>If you don&#8217;t have an idea what I&#8217;m talking about, see <a href="http://jsfiddle.net/9a2yT/">this fiddle</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://scriptish.com/2011/10/20/non-breaking-space-between-last-words-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP properties with Getters and Setters</title>
		<link>http://scriptish.com/2011/10/12/php-properties-with-getters-and-setters/</link>
		<comments>http://scriptish.com/2011/10/12/php-properties-with-getters-and-setters/#comments</comments>
		<pubDate>Wed, 12 Oct 2011 11:00:08 +0000</pubDate>
		<dc:creator>Jeroen Weeink</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://scriptish.com/?p=95</guid>
		<description><![CDATA[Imagine you have an object with numerous properties, and you want to have easy access to them in your code while ensuring encapsulation for these properties. Like in the snippet below, where you want to ensure that only strings can be assigned to name: $test = new Test(); $test-&#62;name = 'This is my name!'; $test-&#62;name [...]]]></description>
			<content:encoded><![CDATA[<p>Imagine you have an object with numerous properties, and you want to have easy access to them in your code while ensuring encapsulation for these properties. Like in the snippet below, where you want to ensure that only strings can be assigned to <code>name</code>:</p>

<pre class="prettyprint"><code>$test = new Test();
$test-&gt;name = 'This is my name!';
$test-&gt;name = 500; // name can't be an int!
</code></pre>

<p>This can be easily accomplished by using the magic methods <code>__get()</code> and <code>__set()</code> of an object. In the code below, the properties of the class that extend <code>Base</code> can be accessed if a getter or setter method has been declared for that property. The getter or setter is simply a method with the same name as the property, prefixed with <code>get</code> or <code>set</code>.</p>

<p>This way you can restrict the values being assigned to these properties, and alter the values being returned by the getters.</p>

<p>The full example:</p>

<pre class="prettyprint"><code>class Base {

    public function __get($name) {
        $method = "get$name";

        $this-&gt;checkPropertyAccess($method, $name);
        return $this-&gt;$method();
    }

    public function __set($name, $value) {
        $method = "set$name";

        $this-&gt;checkPropertyAccess($method, $name);
        return $this-&gt;$method($value);
    }

    private function checkPropertyAccess($method, $property) {
        if (!method_exists($this, $method)) {
            throw new Exception("Property '$property' not accessible");
        }
    }
}

class Test extends Base {

    private $_name;

    public function getName() {
        return $this-&gt;_name;
    }

    public function setName($name) {
        if (is_string($name)) {
            $this-&gt;_name = $name;
        }
    }
}

$test = new Test();
$test-&gt;name = 'This is my name!';
$test-&gt;name = 500;

echo $test-&gt;name;
</code></pre>
]]></content:encoded>
			<wfw:commentRss>http://scriptish.com/2011/10/12/php-properties-with-getters-and-setters/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Add Items to Existing WordPress Menu</title>
		<link>http://scriptish.com/2011/10/10/add-items-to-existing-wordpress-menu/</link>
		<comments>http://scriptish.com/2011/10/10/add-items-to-existing-wordpress-menu/#comments</comments>
		<pubDate>Mon, 10 Oct 2011 11:00:20 +0000</pubDate>
		<dc:creator>Jeroen Weeink</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://scriptish.com/?p=79</guid>
		<description><![CDATA[Menu&#8217;s in WordPress are quite customizable, items can be added and removed. Imagine you have a menu that must always include certain options, but you want to prevent them from being accidentally removed by the user. This can be done by using the wp_nav_menu_items filter. All the code for this goes into your theme&#8217;s functions.php. [...]]]></description>
			<content:encoded><![CDATA[<p>Menu&#8217;s in WordPress are quite customizable, items can be added and removed. Imagine you have a menu that must always include certain options, but you want to prevent them from being accidentally removed by the user. This can be done by using the <code>wp_nav_menu_items</code> filter. All the code for this goes into your theme&#8217;s <code>functions.php</code>.</p>

<p>First, make sure your menu is registered and included in the page:</p>

<pre class="prettyprint"><code>register_nav_menus(array(
    'main-menu' =&gt; __('Main Navigation')
));

wp_nav_menu(array(
    'theme_location' =&gt; 'main-menu',
    'menu_id' =&gt; 'main-menu'
));
</code></pre>

<p>Next we&#8217;re going to add a filter:</p>

<pre class="prettyprint"><code>add_filter('wp_nav_menu_items', 'custom_nav_menu_items', 10, 2);
</code></pre>

<p>The filter is executed for each menu on the page. The first argument is the name of the filter, the second the name of our function that will add the menu item. The third is the priority, ten by default, and the final one is the number of arguments our custom function expects.</p>

<p>The <code>custom_nav_menu_items</code> function is implemented like this:</p>

<pre class="prettyprint"><code>function custom_nav_menu_items($items, $args) {
    if ($args-&gt;theme_location == 'main-menu') {
        $home = '&lt;li&gt;&lt;a href="' . home_url() . '"&gt;Home&lt;/a&gt;&lt;/li&gt;';
        return $home . $items;
    }
    return $items;
}
</code></pre>

<p>We&#8217;re expecting two arguments: <code>$items</code> is the HTML for the other menu items, and <code>$args</code> contains information about the menu. <code>$args-&gt;theme_location</code> is used to make sure the item is added to a specific menu on the page. Note how <code>$home</code> is prepended to <code>$items</code>, if it were appended it would show as the last option in the menu.</p>

<p>That&#8217;s it! Now refresh the page and you&#8217;ll see the &#8216;Home&#8217; option appearing.</p>
]]></content:encoded>
			<wfw:commentRss>http://scriptish.com/2011/10/10/add-items-to-existing-wordpress-menu/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Stop queueing animations in jQuery</title>
		<link>http://scriptish.com/2011/10/09/stop-queueing-animations-in-jquery/</link>
		<comments>http://scriptish.com/2011/10/09/stop-queueing-animations-in-jquery/#comments</comments>
		<pubDate>Sun, 09 Oct 2011 12:41:14 +0000</pubDate>
		<dc:creator>Martti Laine</dc:creator>
				<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://scriptish.com/?p=85</guid>
		<description><![CDATA[Even these days I see this problem on dozens of professional websites. What I mean is that developers use animations on mouse-events, but forget to dequeue them. $('#element').bind('mouseover',function(){ $(this).animate({ width: 200 },500); }).bind('mouseout',function(){ $(this).animate({ width: 50 },500); }); Click here for a living example. Move your mouse over the element rapidly back and forth to [...]]]></description>
			<content:encoded><![CDATA[<p>Even these days I see this problem on dozens of professional websites. What I mean is that developers use animations on mouse-events, but forget to dequeue them.</p>

<pre class="prettyprint"><code>$('#element').bind('mouseover',function(){
    $(this).animate({
        width: 200
    },500);
}).bind('mouseout',function(){
    $(this).animate({
        width: 50
    },500);
});
</code></pre>

<p>Click <a href="http://jsfiddle.net/Sdc3D/1/">here</a> for a living example. Move your mouse over the element rapidly back and forth to see, how even after you&#8217;ve stopped hovering the element, the animation continues.</p>

<p>One function is needed to fix this issue, <code>stop()</code>. Usage is very simple. See this code:</p>

<pre class="prettyprint"><code>$('#element').bind('mouseover',function(){
    $(this).stop(true,false).animate({
        width: 200
    },500);
}).bind('mouseout',function(){
    $(this).stop(true,false).animate({
        width: 50
    },500);
});
</code></pre>

<p>See <a href="http://jsfiddle.net/Sdc3D/2/">here</a> for a demonstration. The animation stops when the mouse is moved off the element.</p>

<p>The first parameter, which is set to <code>true</code>, defines if we want to clear the queue. In this particular piece of code it doesn&#8217;t really make any difference.</p>

<p>The second parameter defines, if we want to jump to the end of the current animation. This may be needed on occasion, <a href="http://jsfiddle.net/Sdc3D/3/">here&#8217;s</a> how it looks. Try moving your mouse over the element and quickly (before the first animation is complete) moving it out. You can see the first animation is &#8220;completed&#8221; instantly.</p>

<p>See the <a href="http://api.jquery.com/stop/">official documentation</a> for more detailed usage.</p>
]]></content:encoded>
			<wfw:commentRss>http://scriptish.com/2011/10/09/stop-queueing-animations-in-jquery/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
