Answer the question
In order to leave comments, you need to log in
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);
Answer the question
In order to leave comments, you need to log in
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);
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);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question