<?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>I am Ad Taylor &#187; php</title>
	<atom:link href="http://www.iamadtaylor.com/tag/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.iamadtaylor.com</link>
	<description>The portfolio and blog of Ad Taylor</description>
	<lastBuildDate>Thu, 20 May 2010 22:49:55 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Archive Extractor &#8211; A Perch Hack / Plugin</title>
		<link>http://www.iamadtaylor.com/archive-extractor-a-perch-plugin/</link>
		<comments>http://www.iamadtaylor.com/archive-extractor-a-perch-plugin/#comments</comments>
		<pubDate>Tue, 04 Aug 2009 10:29:57 +0000</pubDate>
		<dc:creator>Ad Taylor</dc:creator>
				<category><![CDATA[features]]></category>
		<category><![CDATA[Perch]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[plugin]]></category>

		<guid isPermaLink="false">http://www.iamadtaylor.com/?p=148</guid>
		<description><![CDATA[A plugin to help with the extraction of content itmes from the Perch database.]]></description>
			<content:encoded><![CDATA[<h1>Archive Extractor &#8211; A Perch Hack / Plugin</h1>
<div id="text-wrap">
<p class="edit note"><strong>Edit :</strong> Changes made as per Drew&#8217;s advice. Now deals with &#8217;shared&#8217; content and custom table prefix.</p>
<p>For sometime now I have been meaning to post this little hack I came up with — however I completely forgot. This is a hack for the <strong>brilliant</strong> &#8216;little&#8217; <abbr  title="Content managment system">CMS </abbr>, <a href="http://grabaperch.com/" title="Perch CMS">Perch</a> .<br/> It was made through necessity as I <strong>wanted</strong> to use Perch but I <strong>needed</strong> to be able to have news items from other pages on the front page. To my surprise it was nice and easy, and although I&#8217;m sure it will be inefficient, it seems pretty fast.</p>
<p>It is an incredibly simple plugin that pulls the JSON out of the Perch database and returns it. It doesn&#8217;t try to do anything else — no formatting, no nothing — just the way I like it.</p>
<p class="note"><strong>Standard disclaimer :</strong> I am not a server-side kinda guy, I can not take responsibility for anything this plugin does to your installation of Perch, server security or general happiness. That said, I can&#8217;t see how these lazy few lines of code could screw up much.</p>
<h2 class="installation">Installation</h2>
<p>Download the file and put it in your Perch &#8220;plugins&#8221; folder. This isn&#8217;t mandatory for it to work but just seems like a logical place to put it. Watch out when you next need to update Perch — Back up!!</p>
<h2 class="usage">Usage</h2>
<p>You must include the file at the top of the page you wish to use Archive Extractor. If your Perch directory is called &#8220;perch&#8221; then the include could be something like this:
</p>
<pre name="code" class="php">
	 include('perch/plugins/archiveExtractor/archiveExtractor.php');
</pre>
<p>To use just call the function with the 2 variables, the Perch Content name (e.g &#8220;News Item&#8221;) and the path to the page it is on (e.g &#8220;/news/index.php&#8221;). With the data returned you can iterate through the items and pull from it the data you want. <br/> Maybe best seen as an example:</p>
<pre name="code" class="php">
	// Call function with variables.
	$newsArchive = archiveExtractor('News Item','/news/index.php');
	// Check to see if there are any results
	if($newsArchive) {
		$limit = count($newsArchive);
		// Loop through all results
		for ($i=0; $i < $limit; $i++) {
			// In the example the word 'title' refers to the id of a content type
			// from the 'News Item' content template
			echo '
<li>
<h5>'.$newsArchive[$i]->title.' Trip</h5>

 on '.$newsArchive[$i]->information.'.</li>

';
		}
	}
</pre>
<p>If you want to extract data from shared regions, simply leave out the content path. It took me a while to understand the point of this (as this was recommended by <a href="http://grabaperch.com" class="vcard fn url">Drew</a>) but obviously it would be handy if you only wanted to extract part of the data (i.e. title and date).</p>
<pre name="code" class="php">
$newsArchive = archiveExtractor('News Item');
</pre>
<p><a href="/resources/archiveExtractor.zip" title="Download Archive Extrator" ><span class="download">Download</span></a><br />
<a href="#" title="Click to see plugin code" class="show-code">Click to view code</a></p>
<div class="clear"></div>
<div class="code-hide">
<pre name="code" class="php">
/*
	Archive Extractor - A Perch Hack
	By Ad Taylor - http://www.iamadtaylor.com
	For usage see :
	Creative Commons Attribution-Share Alike 2.0 UK: England &#038; Wales Licence
*/

function archiveExtractor($contentKey,$contentPage = '*')
{
	$query = queryDB($contentKey,$contentPage,$limit);
	if ($query) {
		foreach ($query as $key => $value) {
			$json = $value;
		}

		return PerchUtil::json_safe_decode($json);
	} else {
		return NULL;
	}

}

function queryDB($contentKey,$contentPage) {

	require_once $_SERVER['DOCUMENT_ROOT'].'/edit/config/config.php';

	$link = @mysql_connect(PERCH_DB_SERVER, PERCH_DB_USERNAME, PERCH_DB_PASSWORD);

	if (!$link) {
		return NULL;
	}
	else {
		mysql_select_db(PERCH_DB_DATABASE,$link);
		$sql = "SELECT `contentJSON` FROM `".PERCH_DB_PREFIX."contentItems` WHERE `contentKey` LIKE '$contentKey' AND `contentPage` LIKE CONVERT(_utf8 '$contentPage' USING latin1) OR '*' COLLATE latin1_swedish_ci";
		$result = mysql_query($sql, $link);
		return mysql_fetch_assoc($result);
		mysql_close($link);

	}
}</pre>
</div>
</div>
<p><!-- #text-wrap --></p>
]]></content:encoded>
			<wfw:commentRss>http://www.iamadtaylor.com/archive-extractor-a-perch-plugin/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>

