C
C
Cheizer2020-12-31 14:07:17
MODX
Cheizer, 2020-12-31 14:07:17

MODX REVO not working select by conditions, how to set WHERE parameter?

The task is simple but it is not clear how to implement it, there is a resource with id 198, it has a TV field_news of the MIGX type with a date field, I am trying to filter records by the where condition, where date (plain text type) = 2018. All this is needed for the AJAX filter.
Here is the logic of my snippet. Simplified everything as much as possible for clarity. In $_GET['date'] from the form, the idea is to pass a parameter. But in an example directly rigidly registered. The problem is that the where condition syntax for getImageList is complicated, and I can't figure out how to write it. If you bypass the Filtr array and directly write the condition in WHERE $where = '{"date:=":"2018"}'; then everything works. But you need it through an array, since there can be a lot of filtering parameters, and AJAX needs the JSON format? in the snippet this place is $where = $modx->toJSON(array($filter));

$filter = array();
if($_GET['date']) {
  //$filter[] = 'date='.$_GET['date'];
  //$filter[] = "{'date:=':'2018'}";
}
if($filter) {
 //$where = $modx->toJSON(array($filter));
 $where = '{"date:=":"2018"}';
}
$params = array(
  'docid' => 198,
  'tvname' => 'field-news',
       'where' => $where
    //'where' => '{"date:=":"2018"}',
  );	
return $modx->runSnippet('getImageList', $params);

How to implement a selection so that the WHERE condition works with JSON and the FILTER array?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
C
Cheizer, 2020-01-03
@Cheizer

Decision

<?php
$filter = array();

if($_GET['date']) {
 $filter[] = 'date='.$_GET['date'];
}
if($_GET['mytv']) {
 $filter[] = 'mytv='.$_GET['mytv'];
}
$newArray = [];
if($filter) {
foreach($filter as $key => $value)
  {
 $tmp = explode('=', $value);
  $newArray[$tmp[0] . ':='] = $tmp[1];
    $res = explode("=", $value);
  }
} else {
  $where = '';
}
 $where = json_encode($newArray);

$params = array(
  'docid' => $parents,
  'tvname' => $tvname,
  'tpl' => $tpl,
  'totalVar' => 'totalnews',
  'where' => $where
  );	
  
return $modx->runSnippet('getImageList', $params);

S
srjk94, 2020-12-31
@srjk94

Try like this

$filter = array();
if($_GET['date']) {
  $filter[] = ['date' => $_GET['date']]; //Или так $filter['date'] = $_GET['date'];
}
if($filter) {
 $where = $modx->toJSON($filter);
}
$params = array(
  'docid' => 198,
  'tvname' => 'field-news',
       'where' => $where
  );	
return $modx->runSnippet('getImageList', $params);

Documentation can be found here https://docs.modx.com/current/en/extending-modx/xp...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question