<?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>36Flavours.com &#187; function</title>
	<atom:link href="http://36flavours.com/tag/function/feed/" rel="self" type="application/rss+xml" />
	<link>http://36flavours.com</link>
	<description>A taste of something different...</description>
	<lastBuildDate>Mon, 19 Apr 2010 20:01:53 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>WP dbDelta function cannot modify unique keys</title>
		<link>http://36flavours.com/2010/04/wp-dbdelta-function-cannot-modify-unique-keys/</link>
		<comments>http://36flavours.com/2010/04/wp-dbdelta-function-cannot-modify-unique-keys/#comments</comments>
		<pubDate>Fri, 02 Apr 2010 21:12:39 +0000</pubDate>
		<dc:creator>Steve Whiteley</dc:creator>
				<category><![CDATA[WordPress]]></category>
		<category><![CDATA[dbdelta]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[query]]></category>
		<category><![CDATA[sql]]></category>

		<guid isPermaLink="false">http://36flavours.com/?p=685</guid>
		<description><![CDATA[Whilst making some updates to one of my WordPress plugins earlier today, I discovered an small issue when trying to make use of the dbDelta function. Although it&#8217;s advised you avoid creating tables unless absolutely necessary, it seemed like the correct method to store the data used by this particular plugin. This method of storage [...]]]></description>
			<content:encoded><![CDATA[<p class="first">Whilst making some updates to one of my WordPress plugins earlier today, I discovered an small issue when trying to make use of the dbDelta function.</p>
<p>Although it&#8217;s advised you avoid creating tables unless absolutely necessary, it seemed like the correct method to store the data used by this particular plugin.<span id="more-685"></span></p>
<p>This method of storage does have it&#8217;s downsides, especially when it comes to modifying the table structure. Luckily the dbDelta function comes in really handy in aiding the update process.</p>
<p>There is however one issue that I came across, discovering that is unable to modify a unique key (in this situation a composite index).</p>
<p>After browsing through the source code for the dbDelta function I spotted that something was missing. When including an index in the query, it is not dropped before attempting to recreate it and is therefore ignored when performing an update.</p>
<p>Take for example the following code snippet:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$wpdb</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$table</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$wpdb</span><span style="color: #339933;">-</span>prefix<span style="color: #339933;">.</span><span style="color: #0000ff;">'mytable'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;CREATE TABLE <span style="color: #006699; font-weight: bold;">$table</span> (
	id mediumint(9) NOT NULL AUTO_INCREMENT,
	calendar varchar(255) NOT NULL default 'default',
	year smallint(5) NOT NULL,
	month tinyint(3) NOT NULL,
	day tinyint(3) NOT NULL,
	PRIMARY KEY (id),
	UNIQUE KEY date (year,month,day)
);&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">require_once</span><span style="color: #009900;">&#40;</span>ABSPATH<span style="color: #339933;">.</span><span style="color: #0000ff;">'wp-admin/includes/upgrade.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
dbDelta<span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>If we make some changes to the SQL statement to add an additional column to the unique index, the index would become something like &#8220;<em>UNIQUE KEY date (year,month,day,calendar)</em>&#8220;.</p>
<p>This will work if the the table does not already exist, but if it attempts to update the table then the changes to the unique key are ignored.</p>
<p>The short and simple solution is to simply check the table exists and then drop the index manually before calling the dbDelta function.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$wpdb</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get_var</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SHOW TABLES LIKE <span style="color: #006699; font-weight: bold;">$table</span>&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$table</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$wpdb</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;ALTER TABLE <span style="color: #006699; font-weight: bold;">$table</span> DROP INDEX date&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>I&#8217;m not sure if this is actually a bug with the function or whether it wasn&#8217;t supposed to allow for updates on indexes in the first place, either way the problem can be quite easily resolved.</p>
]]></content:encoded>
			<wfw:commentRss>http://36flavours.com/2010/04/wp-dbdelta-function-cannot-modify-unique-keys/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>WP &#8211; Add class name to first paragraph in blog post</title>
		<link>http://36flavours.com/2009/08/wp-add-class-name-to-first-paragraph-in-blog-post/</link>
		<comments>http://36flavours.com/2009/08/wp-add-class-name-to-first-paragraph-in-blog-post/#comments</comments>
		<pubDate>Thu, 20 Aug 2009 15:04:27 +0000</pubDate>
		<dc:creator>Steve Whiteley</dc:creator>
				<category><![CDATA[Blogging]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[formatting]]></category>
		<category><![CDATA[function]]></category>

		<guid isPermaLink="false">http://36flavours.com/?p=273</guid>
		<description><![CDATA[When re-designing my blog I wanted to display the first paragraph of each blog post in bold text. A CSS3 selector could have been used, but wouldn&#8217;t work in older browsers such as IE6. Instead I decided to write a very simple function to add a class name to the first paragraph by filtering the [...]]]></description>
			<content:encoded><![CDATA[<p class="first">When re-designing my blog I wanted to display the <strong>first paragraph</strong> of each blog post in <strong>bold text</strong>. A CSS3 selector could have been used, but wouldn&#8217;t work in older browsers such as <strong>IE6</strong>.</p>
<p>Instead I decided to write a very simple function to add a class name to the first paragraph by filtering the content, then setting the styles accordingly in my theme CSS file.<span id="more-273"></span></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> first_paragraph<span style="color: #009900;">&#40;</span><span style="color: #000088;">$content</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #990000;">preg_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/&lt;p([^&gt;]+)?&gt;/'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&lt;p$1 class=&quot;first&quot;&gt;'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$content</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
add_filter<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'the_content'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'first_paragraph'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>By default this will append <em>class=&#8221;first&#8221;</em> to the first p tag in your post.</p>
<p>If you want to use this function on your own blog, just add it to your (or create a) <strong> functions.php</strong> file within your themes folder.</p>
]]></content:encoded>
			<wfw:commentRss>http://36flavours.com/2009/08/wp-add-class-name-to-first-paragraph-in-blog-post/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Simple jQuery string padding function</title>
		<link>http://36flavours.com/2009/02/simple-jquery-string-padding-function/</link>
		<comments>http://36flavours.com/2009/02/simple-jquery-string-padding-function/#comments</comments>
		<pubDate>Fri, 27 Feb 2009 12:27:57 +0000</pubDate>
		<dc:creator>Steve Whiteley</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[js]]></category>
		<category><![CDATA[str_pad]]></category>

		<guid isPermaLink="false">http://sha.re.it/?p=142</guid>
		<description><![CDATA[I&#8217;ve written a very simple jQuery function to return a string padded to a specified length, similar to the php equivalent str_pad. 1 2 3 4 5 6 7 8 $.strPad = function&#40;i,l,s&#41; &#123; var o = i.toString&#40;&#41;; if &#40;!s&#41; &#123; s = '0'; &#125; while &#40;o.length &#60; l&#41; &#123; o = s + o; [...]]]></description>
			<content:encoded><![CDATA[<p class="first">I&#8217;ve written a very simple <a href="http://jquery.com/" target="_blank">jQuery</a> function to return a string padded to a specified length, similar to the php equivalent <a href="http://uk2.php.net/manual/en/function.str-pad.php" target="_blank">str_pad</a>.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">$.<span style="color: #660066;">strPad</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>i<span style="color: #339933;">,</span>l<span style="color: #339933;">,</span>s<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #003366; font-weight: bold;">var</span> o <span style="color: #339933;">=</span> i.<span style="color: #660066;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>s<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> s <span style="color: #339933;">=</span> <span style="color: #3366CC;">'0'</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
	<span style="color: #000066; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>o.<span style="color: #660066;">length</span> <span style="color: #339933;">&lt;</span> l<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		o <span style="color: #339933;">=</span> s <span style="color: #339933;">+</span> o<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000066; font-weight: bold;">return</span> o<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Example Usage:</p>
</pre>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$.<span style="color: #660066;">strPad</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">12</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">5</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// returns 00012</span>
$.<span style="color: #660066;">strPad</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'abc'</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">6</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'#'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// returns ###abc</span></pre></div></div>

<p>This version only supports left padding, which is why it is labelled as only a simple version <img src='http://36flavours.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .</p>
]]></content:encoded>
			<wfw:commentRss>http://36flavours.com/2009/02/simple-jquery-string-padding-function/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
