All for creating and promoting websites

Layout, programming, web tools

Get all values TV the resources in MODX XPDO

Get all values TV the resources in MODX XPDO

The script on XPDO for MODX Revo, which gets all values for a specific TV to his id all resources.

The output we get an array with the results of the sample.

<?php
$tv_id = 5; // id TV
$res = array(); // It records the results

$q = $modx->newQuery('modTemplateVarResource', array('tmplvarid' => $tv_id));
$q->select('contentid,value');
if ($q->prepare() && $q->stmt->execute()) {
	while ($row = $q->stmt->fetch(PDO::FETCH_ASSOC)) {
		$res[$row['contentid']] = $row['value'].'
'; } } print_r($res);

If there are same value fields TV,they are displayed. To eliminate duplicates, you need to use sql-method «groupby».Should just add it to the parameters of the sample.

<?php
$tv_id = 5; // id TV
$res = array(); // It records the results

$q = $modx->newQuery('modTemplateVarResource', array('tmplvarid' => $tv_id));
$q->select('contentid,value');
$q->groupby('value');
if ($q->prepare() && $q->stmt->execute()) {
	while ($row = $q->stmt->fetch(PDO::FETCH_ASSOC)) {
		$res[$row['contentid']] = $row['value'].'
'; } } print_r($res);

Now the repeats are excluded. You can add other selection criteria.

Highlight found mistake and press Ctrl + Enter to notify the administrator.