D
D
Dmitry Antonov2016-11-04 17:53:29
MODX
Dmitry Antonov, 2016-11-04 17:53:29

How to paginate MODX with getPage or pdoPage for your snippet?

Good evening, I made my own snippet that receives xml and then parses it into an array.
I tried to connect my snippet with getPage or with pdoPage but it doesn't work. After reading this instruction, I realized that there the selection comes from the database through the MODX API.
Please tell me what I need to do with the array that stores all the posts so that it can be divided into pages.
At the moment I have this code

<?php
$limit=$modx->getOption('limit', $scriptProperties, 5);
$offset=$modx->getOption('offset', $scriptProperties, 0);
$i = 0;
$reviews = array();
function get_id($tvid){
    $res = array(); // Сюда забиваем результаты
    global $modx;
    $q = $modx->newQuery('modTemplateVarResource', array('tmplvarid' => $tvid));
    $q->select('contentid,value');
    if ($q->prepare() && $q->stmt->execute()) {
    	while ($row = $q->stmt->fetch(PDO::FETCH_ASSOC)) {
    		$res[$row['contentid']] = $row['value'];
    	}
      }
    return $res;
}
function get_reviews_XMl($id){
    $url = 'http://shop.digiseller.ru/xml/shop_reviews.asp';
    $xml = "
  <digiseller.request>
    <seller>
      <id>195463</id>
    </seller>
    <product>
    <id>$id</id>
  </product>
    <reviews>
      <type>good</type>
      </reviews>
    <pages>
      <rows>1</rows>
      </pages>
  </digiseller.request>
  ";
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Content-Type: text/xml"));
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
    return curl_exec($ch);

}
function parse_array($array,$limit,$offset){
    global $modx;
    $out = '';
    $l = $offset + $limit;
    for ($i = $offset; $i < $limit; $i++){
        $placeholders = array(
            'my.id' => $array[$i]['game_id'],
            'my.title' => $array[$i]['title'],
            'my.image' => $array[$i]['image'],
            'my.text' => $array[$i]['info'],
            'my.date' => $array[$i]['date']
        );
        $out .= $modx -> parseChunk('review-tpl2',$placeholders);
    }
    return $out;
}
function mysort($a, $b){
    return strtotime($b['date']) - strtotime($a['date']);
}
$res = get_id(18);
foreach($res as $id => $v){
  $xml_obj = simplexml_load_string(get_reviews_XMl($v), 'SimpleXMLElement', LIBXML_NOCDATA);
  foreach($xml_obj->reviews->review as $rv){
    $reviews[$i]['game_id'] = (string)$id;
    $reviews[$i]['title'] = $modx->getObject('modResource',$id)->get('pagetitle');
    $reviews[$i]['image'] = $modx->getObject('modResource',$id)->getTVValue(1);
    $reviews[$i]['date'] = (string)$rv->date;
    $reviews[$i]['info'] = (string)$rv->info;
    $i++;
  }
}
$total = count($reviews);
$totalVar=$modx->getOption('totalVar', $scriptProperties, 'total');
$modx->setPlaceholder($totalVar,$total);
usort($reviews, 'mysort');

$a = parse_array($reviews,$limit,$offset);
return $a;

Unfortunately, it displays the current 1 page .... while others do not work ((
what is displayed can be seen here

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question