Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
Exclude non-matches comparing multiple variables
I’ve managed to store some article IDs that I need in variables.
<txp:variable name="subject" />
<txp:variable name="type" />
<txp:variable name="creator" />
Those might output:
subject: 108, 192, 193, 194, 197, 1712, 1730, 1828, 1831, 1829, 1833, 1832, 1834, 1835, 1842, 1850, 1853, 1852, 1854, 1855, 1856, 1857, 1859, 1858, 1863, 1864, 1865, 1866, 1867, 1870, 1871, 1872, 1873, 1874, 1875
type: 147, 1874, 2115, 2216, 2249, 2251, 2327, 2347, 2356, 2358, 2361, 2369, 2367, 2366, 2476, 2485, 2570,
creator: 1874, 1892,
A comparison:
1874 is the only ID present in each list.
What might be a good way to put any matches in to a new variable?
<txp:variable name="matched_ids" />
matched_ids: 1874
I’m looking at implode…
Last edited by whaleen (2013-06-03 19:17:08)
txtstrap (Textpattern + Twitter Bootstrap + etc…)
Offline
Re: Exclude non-matches comparing multiple variables
Though ugly, something like this should work:
<txp:php>
variable(
array('name' => 'matched_ids'),
implode(',', array_intersect(do_list(variable(array('name' => 'subject')), do_list(variable(array('name' => 'type')), do_list(variable(array('name' => 'creator')))
);
</txp:php>
Offline
Re: Exclude non-matches comparing multiple variables
I’ll admit this is new too me so instead of interpreting / modyfying what you wrote I just copied and pasted it in. Trying your suggestion as is resulted in:
Parse error: syntax error, unexpected ‘;’ in /Users/joshuavaage/Dropbox/httpdocs/freeathena.com/textpattern/publish/taghandlers.php(3836) : eval()’d code on line 5
I have had no luck yet with what I’ve come up with so far:
<txp:php>
$subject_id_array = '<txp:variable name="subject" />';
$type_id_array = '<txp:variable name="type" />';
$creator_id_array = '<txp:variable name="creator" />';
$a1=explode(",", $subject_id_array);
$a2=explode(",", $type_id_array);
$a3=explode(",", $creator_id_array);
$result=array_intersect($a1,$a2,$a3);
$matched_ids = implode(",", $result);
echo $matched_ids;
</txp:php>
This is new territory for me so I’m a little confused.
txtstrap (Textpattern + Twitter Bootstrap + etc…)
Offline
Re: Exclude non-matches comparing multiple variables
Sorry, I have lost few )
s:
<txp:php>
variable(
array('name' => 'matched_ids'),
implode(',', array_intersect(do_list(variable(array('name' => 'subject'))), do_list(variable(array('name' => 'type'))), do_list(variable(array('name' => 'creator')))))
);
</txp:php>
Your solution is roughly the same, but <txp:variable name="subject" />
will not be parsed inside <txp:php>
, you should replace it with variable(array('name' => 'subject'))
Offline
Re: Exclude non-matches comparing multiple variables
I went with your solution. It works great. Mine, I was unable to get working and had run out of patience with it. Thanks for taking the time to look at this with me.
txtstrap (Textpattern + Twitter Bootstrap + etc…)
Offline
Re: Exclude non-matches comparing multiple variables
whaleen wrote:
Mine, I was unable to get working
That’s strange, it should be fine once txp tags are replaced with functions (or global $variable
s). I’d also use Textpattern do_list
function instead of explode
, just in case your variables contain spaces between ids:
<txp:php>
$subject_id_array = variable(array('name' => 'subject'));
$type_id_array = variable(array('name' => 'type'));
$creator_id_array = variable(array('name' => 'creator'));
$a1=do_list($subject_id_array);
$a2=do_list($type_id_array);
$a3=do_list($creator_id_array);
$result=array_intersect($a1,$a2,$a3);
$matched_ids = implode(",", $result);
echo $matched_ids;
</txp:php>
Offline