Return to Snippet

Revision: 573
at December 12, 2007 14:13 by methnen


Updated Code
global $DB;

$this_entry_id = "{entry_id}";
$related_field = "member_related_org";

// Query for field id of related field
$rel_field_id_query = $DB->query("SELECT field_id FROM exp_weblog_fields WHERE field_name = '$related_field'");
$field_id = 'field_id_'.$rel_field_id_query->row['field_id'];

// Query for rel_id values in the exp_relationships table
$rel_id_query = $DB->query("SELECT rel_id FROM exp_relationships WHERE rel_child_id = '$this_entry_id'");

if($rel_id_query->num_rows > 0)
{  
	// Build query to grab all related entries based off of the rel_id values
	$rel_query_chunk = '(';
	
	foreach($rel_id_query->result as $row)
	{
		$rel_id = $row['rel_id'];

		$rel_query_chunk .= "OR exp_weblog_data.${field_id} = '$rel_id' ";
	}
	
	$rel_query_chunk .= ')';
	
	$find = array(
		'(OR ',
		' )'
	);
	
	$replace = array(
		'(',
		')'
	);
	
	$rel_query_chunk = str_replace($find, $replace, $rel_query_chunk);
	
	$sql = "
		SELECT * FROM exp_weblog_titles
		LEFT JOIN exp_weblog_data ON exp_weblog_data.entry_id = exp_weblog_titles.entry_id
		WHERE $rel_query_chunk
	";
	
	// Query for entry data
	$rel_entry_query = $DB->query($sql);

	if($rel_entry_query->num_rows > 0)
	{
		foreach($rel_entry_query->result as $row)
		{
			$entry_id = $row['entry_id'];
			$title = $row['title'];
		}
	}
}

Revision: 572
at May 31, 2007 23:34 by methnen


Updated Code
global $DB;

$this_entry_id = "{entry_id}";
$related_field = "member_related_org";

// Query for field id of related field
$rel_field_id_query = $DB->query("SELECT field_id FROM exp_weblog_fields WHERE field_name = '$related_field'");
$field_id = $rel_field_id_query->row['field_id'];

// Query for rel_id in the exp_relationships table
$rel_id_query = $DB->query("SELECT rel_id FROM exp_relationships WHERE rel_child_id = '$this_entry_id'");

if($rel_id_query->num_rows > 0)
{  
	$rel_query_chunk = '(';
	
	foreach($rel_id_query->result as $row)
	{
		$rel_id = $row['rel_id'];

		$rel_query_chunk .= "OR exp_weblog_data.${field_id} = '$rel_id' ";
	}
	
	$rel_query_chunk .= ')';
	
	$find = array(
		'(OR ',
		' )'
	);
	
	$replace = array(
		'(',
		')'
	);
	
	$rel_query_chunk = str_replace($find, $replace, $rel_query_chunk);
	
	$sql = "
		SELECT * FROM exp_weblog_titles
		LEFT JOIN exp_weblog_data ON exp_weblog_data.entry_id = exp_weblog_titles.entry_id
		WHERE $rel_query_chunk
	";
	
	// Query for entry id's of entries who's related_field field matches
	$rel_entry_query = $DB->query("SELECT entry_id FROM exp_weblog_data WHERE $field_id = '$rel_id'");

	if($rel_entry_query->num_rows > 0)
	{
		foreach($rel_entry_query->result as $row)
		{
			$entry_id = $row['entry_id'];
			$title = $row['title'];
		}
	}
}

Revision: 571
at May 31, 2007 21:00 by methnen


Updated Code
<?php
global $DB;

$this_entry_id = "{entry_id}";
$related_field = "member_related_org";

// Query for field id of related field
$rel_field_id_query = $DB->query("SELECT field_id FROM exp_weblog_fields WHERE field_name = '$related_field'");
$field_id = $rel_field_id_query->row['field_id'];

// Query for rel_id in the exp_relationships table
$rel_id_query = $DB->query("SELECT rel_id FROM exp_relationships WHERE rel_child_id = '$this_entry_id'");

if($rel_id_query->num_rows > 0)
{  
	$rel_query_chunk = '(';
	
	foreach($rel_id_query->result as $row)
	{
		$rel_id = $row['rel_id'];

		$rel_query_chunk .= "OR exp_weblog_data.${field_id} = '$rel_id' ";
	}
	
	$rel_query_chunk .= ')';
	
	$find = array(
		'(OR ',
		' )'
	);
	
	$replace = array(
		'(',
		')'
	);
	
	$rel_query_chunk = str_replace($find, $replace, $rel_query_chunk);
	
	$sql = "
		SELECT * FROM exp_weblog_titles
		LEFT JOIN exp_weblog_data ON exp_weblog_data.entry_id = exp_weblog_titles.entry_id
		WHERE $rel_query_chunk
	";
	
	// Query for entry id's of entries who's related_field field matches
	$rel_entry_query = $DB->query("SELECT entry_id FROM exp_weblog_data WHERE $field_id = '$rel_id'");

	if($rel_entry_query->num_rows > 0)
	{
		foreach($rel_entry_query->result as $row)
		{
			$entry_id = $row['entry_id'];
			$title = $row['title'];
		}
	}
}
?>

Revision: 570
at May 31, 2007 20:49 by methnen


Updated Code
<?php
global $DB;

$this_entry_id = "{entry_id}";
$related_field = "member_related_org";

// Query for field id of related field
$rel_field_id_query = $DB->query("SELECT field_id FROM exp_weblog_fields WHERE field_name = '$related_field'");
$field_id = $rel_field_id_query->row['field_id'];
$field_id = 'field_id_54';

// Query for rel_id in the exp_relationships table
$rel_id_query = $DB->query("SELECT rel_id FROM exp_relationships WHERE rel_child_id = '$this_entry_id'");

if($rel_id_query->num_rows > 0)
{  
	$rel_query_chunk = '(';
	
	foreach($rel_id_query->result as $row)
	{
		$rel_id = $row['rel_id'];

		$rel_query_chunk .= "OR exp_weblog_data.$field_id = '$rel_id' ";
	}
	
	$rel_query_chunk .= ')';
	
	$find = array(
		'(OR ',
		' )'
	);
	
	$replace = array(
		'(',
		')'
	);
	
	$rel_query_chunk = str_replace($find, $replace, $rel_query_chunk);
	
	$sql = "
		SELECT * FROM exp_weblog_titles
		LEFT JOIN exp_weblog_data ON exp_weblog_data.entry_id = exp_weblog_titles.entry_id
		WHERE $rel_query_chunk
	";
	
	// Query for entry id's of entries who's related_field field matches
	$rel_entry_query = $DB->query("SELECT entry_id FROM exp_weblog_data WHERE $field_id = '$rel_id'");

	if($rel_entry_query->num_rows > 0)
	{
		foreach($rel_entry_query->result as $row)
		{
			$entry_id = $row['entry_id'];
			$title = $row['title'];
		}
	}
}
?>

Revision: 569
at May 31, 2007 15:17 by methnen


Updated Code
global $DB;

$this_entry_id = "{entry_id}";
$related_field = "member_related_org";

// Query for field id of related field
$rel_field_id_query = $DB->query("SELECT field_id FROM exp_weblog_fields WHERE field_name = '$related_field'");
$field_id = $rel_field_id_query->row['field_id'];
$field_id = "field_id_".$field_id;

// Query for rel_id in the exp_relationships table
$rel_id_query = $DB->query("SELECT rel_id FROM exp_relationships WHERE rel_child_id = '$this_entry_id'");

if($rel_id_query->num_rows > 0)
{  
	$rel_query_chunk = '(';
	
	foreach($rel_id_query->result as $row)
	{
		$rel_id = $row['rel_id'];

		$rel_query_chunk .= "OR exp_weblog_data.field_id_92 = '$rel_id' ";
	}
	
	$rel_query_chunk .= ')';
	
	$find = array(
		'(OR',
		' )'
	);
	
	$replace = array(
		'(',
		')'
	);
	
	$rel_query_chunk = str_replace($find, $replace, $rel_query_chunk);
	
	$sql = "
		SELECT * FROM exp_weblog_titles
		LEFT JOIN exp_weblog_data ON exp_weblog_data.entry_id = exp_weblog_titles.entry_id
		WHERE $rel_query_chunk
	";
	
	// Query for entry id's of entries who's related_field field matches
	$rel_entry_query = $DB->query("SELECT entry_id FROM exp_weblog_data WHERE $field_id = '$rel_id'");

	if($rel_entry_query->num_rows > 0)
	{
		foreach($rel_entry_query->result as $row)
		{
			$entry_id = $row['entry_id'];
			$title = $row['title'];
		}
	}
}

Revision: 568
at July 21, 2006 08:24 by methnen


Updated Code
global $DB;

$this_entry_id = "{entry_id}";
$related_field = "member_related_org";

// Query for field id of related field
$rel_field_id_query = $DB->query("SELECT field_id FROM exp_weblog_fields WHERE field_name = '$related_field'");
$field_id = $rel_field_id_query->row['field_id'];
$field_id = "field_id_".$field_id;

// Query for rel_id in the exp_relationships table
$rel_id_query = $DB->query("SELECT rel_id FROM exp_relationships WHERE rel_child_id = '$this_entry_id'");

if($rel_id_query->num_rows > 0)
{  
	foreach($rel_id_query->result as $row)
	{
		$rel_id = $row['rel_id'];

		// Query for entry id's of entries who's related_field field matches
		$entry_id_query = $DB->query("SELECT entry_id FROM exp_weblog_data WHERE $field_id = '$rel_id'");

		if($entry_id_query->num_rows > 0)
		{
			foreach($entry_id_query->result as $row)
			{
				$entry_id = $row['entry_id'];

				// Query for title of entries who's entry_id matches
				$title_query = $DB->query("SELECT title FROM exp_weblog_titles WHERE entry_id = '$entry_id'");
				$title = $title_query->row['title'];

				// Query for custom fields of entries who's entry_id matches
				$field_data_query = $DB->query("SELECT * FROM exp_weblog_data WHERE entry_id = '$entry_id'");
			}
		}
	}	
}

Revision: 567
at July 19, 2006 11:13 by methnen


Updated Code
/*
=============================================================================
 Finds related entries in the direction that EE does not support by default.
=============================================================================
*/

global $DB;

$this_entry_id = "{entry_id}";
$related_field = "member_related_org";

// Query for field id of related field
$rel_field_id_query = $DB->query("SELECT field_id FROM exp_weblog_fields WHERE field_name = '$related_field'");
$field_id = $rel_field_id_query->row['field_id'];
$field_id = "field_id_".$field_id;

// Query for rel_id in the exp_relationships table
$rel_id_query = $DB->query("SELECT rel_id FROM exp_relationships WHERE rel_child_id = '$this_entry_id'");

if($rel_id_query->num_rows > 0)
{  
	foreach($rel_id_query->result as $row)
	{
		$rel_id = $row['rel_id'];

		// Query for entry id's of entries who's related_field field matches
		$entry_id_query = $DB->query("SELECT entry_id FROM exp_weblog_data WHERE $field_id = '$rel_id'");

		if($entry_id_query->num_rows > 0)
		{
			foreach($entry_id_query->result as $row)
			{
				$entry_id = $row['entry_id'];

				// Query for title of entries who's entry_id matches
				$title_query = $DB->query("SELECT title FROM exp_weblog_titles WHERE entry_id = '$entry_id'");
				$title = $title_query->row['title'];

				// Query for custom fields of entries who's entry_id matches
				$field_data_query = $DB->query("SELECT * FROM exp_weblog_data WHERE entry_id = '$entry_id'");
			}
		}
	}	
}

Revision: 566
at July 19, 2006 10:15 by methnen


Initial Code
/*
========================================================
 Usage: Finds related entries in the direction that EE
 does not support by default.
========================================================
*/

global $DB;

$this_entry_id = "{entry_id}";
$related_field = "member_related_org";

// Query for field id of related field
$rel_field_id_query = $DB->query("SELECT field_id FROM exp_weblog_fields WHERE field_name = '$related_field'");
$field_id = $rel_field_id_query->row['field_id'];
$field_id = "field_id_".$field_id;

// Query for rel_id in the exp_relationships table
$rel_id_query = $DB->query("SELECT rel_id FROM exp_relationships WHERE rel_child_id = '$this_entry_id'");

if($rel_id_query->num_rows > 0)
{  
	foreach($rel_id_query->result as $row)
	{
		$rel_id = $row['rel_id'];

		// Query for entry id's of entries who's related_field field matches
		$entry_id_query = $DB->query("SELECT entry_id FROM exp_weblog_data WHERE $field_id = '$rel_id'");

		if($entry_id_query->num_rows > 0)
		{
			foreach($entry_id_query->result as $row)
			{
				$entry_id = $row['entry_id'];

				// Query for title of entries who's entry_id matches
				$title_query = $DB->query("SELECT title FROM exp_weblog_titles WHERE entry_id = '$entry_id'");
				$title = $title_query->row['title'];

				// Query for custom fields of entries who's entry_id matches
				$field_data_query = $DB->query("SELECT * FROM exp_weblog_data WHERE entry_id = '$entry_id'");
			}
		}
	}	
}

Initial URL

                                

Initial Description

                                

Initial Title
Related Entries Backwards

Initial Tags
textmate

Initial Language
PHP