<?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>Sean Gates' Blog &#187; Expression Engine</title>
	<atom:link href="http://www.seangates.com/category/expression-engine/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.seangates.com</link>
	<description>By the looks of it {he thinks} he knows what he's doing.</description>
	<lastBuildDate>Sat, 10 Jul 2010 21:22:04 +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>Related Bridge in EE</title>
		<link>http://www.seangates.com/2008/05/01/related-bridge-in-ee/</link>
		<comments>http://www.seangates.com/2008/05/01/related-bridge-in-ee/#comments</comments>
		<pubDate>Thu, 01 May 2008 21:49:27 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[Expression Engine]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[Work]]></category>

		<guid isPermaLink="false">http://www.seangates.com/2008/05/01/related-bridge-in-ee/</guid>
		<description><![CDATA[I&#8217;ve been using Expression Engine for a couple of weeks now, mostly for work projects. It&#8217;s been good to see how a group of people have thought of abstracting data for almost any application. It will be interesting to see if it can solve many of the problems I&#8217;ve run into when developing extremely custom [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been using Expression Engine for a couple of weeks now, mostly for work projects.  It&#8217;s been good to see how a group of people have thought of abstracting data for almost any application.  It will be interesting to see if it can solve many of the problems I&#8217;ve run into when developing extremely custom applications.</p>
<p>So, one problem that we&#8217;ve come across is a way to relate several tables (or weblogs as EE calls them) together.  An example would be like so:  doctors, their specialties, and the locations of their practice.  A likely scenario is a user wants to find out what locations provide a certain specialty.  Thus, you have to use the doctors as the bridge of the data.</p>
<p>In Expression Engine you can use a related field to tie two tables of data together.  For instance, I can tie a Doctor to a certain specialty using a related field.  But, creating a bridge between the doctors&#8217; specialties and all the locations is not possible without a little custom programming.</p>
<p>Also, throw in the mix that doctors can have offices at several locations, and you&#8217;ve got a hairy set of data. (We&#8217;re using the multi-relationship plug-in from <a href="http://expressionengine.com/forums/viewthread/39595/">here</a>).<br />
<span id="more-57"></span><br />
So, let&#8217;s get to the code.</p>
<pre class="brush: php;">&lt;?php
global $DB;
$addresses = $DB-&gt;query(&quot;SELECT * FROM `exp_weblog_data` WHERE weblog_id = 6&quot;);
foreach($addresses-&gt;result as $address){
	$doctors = $DB-&gt;query(&quot;SELECT * FROM `exp_weblog_data` WHERE weblog_id = 3&quot;);
	foreach($doctors-&gt;result as $doc){
		$rel_field_ids = array(2);
		$rel_field_ids_new = '';
		$rel_field_ids[0] = $doc['field_id_34'];
		$rel_field_ids[1] = ereg_replace(&quot;\r&quot;,&quot;,&quot;,$doc['field_id_35']);
		$rel_field_ids_new = &quot;'&quot; . implode(&quot;','&quot;,$rel_field_ids) . &quot;'&quot;;
		$specialties = $DB-&gt;query(&quot;SELECT * FROM exp_relationships WHERE rel_id IN (&quot; . $rel_field_ids_new . &quot;)&quot;);
		foreach($specialties-&gt;result as $specialty){
			if($specialty['rel_child_id'] == $address['entry_id']){
				$specs[] = $specialty['rel_parent_id'];
			}
		}
	}

	if(sizeof($specs) &gt; 0) {
		$specialties_ids = $DB-&gt;query(&quot;SELECT field_id_13 FROM exp_weblog_data WHERE entry_id IN(&quot; . implode($specs,',') . &quot;)&quot;);
		foreach($specialties_ids-&gt;result as $s_id){
			$related_spec = $DB-&gt;query(&quot;SELECT * FROM exp_relationships WHERE rel_id = &quot; . $s_id['field_id_13']);
			$related_spec_title = $DB-&gt;query(&quot;SELECT title FROM exp_weblog_titles WHERE entry_id = &quot; . $related_spec-&gt;row['rel_child_id']);
			if(!in_array($related_spec_title-&gt;row['title'],$titles_array)){ $titles_array[] = $related_spec_title-&gt;row['title']; }
		}
	}
	implode($titles_array,&quot;','&quot;)
}
?&gt;</pre>
<p>So, in a nutshell the code above will compare the locations with the specialties using the exp_relationships table and the doctor as the bridge.</p>
<p>I had to think about it a bit, but you kinda get the idea.  I hope this helps someone out there struggling with the same issue.</p>
<p>Happy coding.</p>
<p><strong>Note:</strong> When copying this code make sure you replace the curly quotes.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.seangates.com/2008/05/01/related-bridge-in-ee/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
