P
P
Pavel Sidorov2020-10-14 11:57:06
PHP
Pavel Sidorov, 2020-10-14 11:57:06

Where is the error in the code when writing to a variable?

Hello,

where is the mistake?

i'm trying to do this

if($_POST['generate_xml']) {

$arSelect = Array("ID", "IBLOCK_ID", "NAME", "DATE_ACTIVE_FROM",);
$arFilter = Array("IBLOCK_ID"=>11, "ACTIVE_DATE"=>"Y", "ACTIVE"=>"Y");
$res = CIBlockElement::GetList(Array(), $arFilter, false, Array("nPageSize"=>50), $arSelect);

  $site = 'http://';
  $data = date('Y-m-d');
  $dir = $_SERVER['DOCUMENT_ROOT'].'/scheme/geoObjects2.geojson';

  $data_for_write ='
  {
    "type": "FeatureCollection",
    "metadata": {
      "name": "Карта торгового центра",
      "creator": "SWP",
      "description": ""
    },
  '.
    while($ob = $res->GetNextElement()){
      $arFields = $ob->GetFields();
      $arProps = $ob->GetProperties();
  .'
      "features": [{
        "type": "Feature",
        "id": "'.echo $arFields['ID'].'",
        "geometry": {
          "type": "Polygon",
          "coordinates": "'.echo $arProps['PROPERTIES']['COORDINATES']['VALUE'].'"
        },
        "properties": {
          "description": "'.echo $arFields['NAME'].'",
          "fill": "#ff931e",
          "fill-opacity": 0.3,
          "stroke": "#e6761b",
          "stroke-width": "2",
          "stroke-opacity": 0.9
        }
      }]
  } 
    '.}.'
  ';

  $write = file_put_contents( $dir , $data_for_write );
  if($write) { $info = "Запись прошла";} else {$info = "Запись не прошла";}
  echo $info;
}


the error is in a piece of code

$data_for_write ='
  {
    "type": "FeatureCollection",
    "metadata": {
      "name": "Карта торгового центра",
      "creator": "Паблито SWP",
      "description": "Сквозь бесонные ночи была сделана эта карта. Пожалуйста не сломайте её!"
    },
  '.
    while($ob = $res->GetNextElement()){
      $arFields = $ob->GetFields();
      $arProps = $ob->GetProperties();
  .'
      "features": [{
        "type": "Feature",
        "id": "'.echo $arFields['ID'].'",
        "geometry": {
          "type": "Polygon",
          "coordinates": "'.echo $arProps['PROPERTIES']['COORDINATES']['VALUE'].'"
        },
        "properties": {
          "description": "'.echo $arFields['NAME'].'",
          "fill": "#ff931e",
          "fill-opacity": 0.3,
          "stroke": "#e6761b",
          "stroke-width": "2",
          "stroke-opacity": 0.9
        }
      }]
  } 
    '.}.'
  ';

Answer the question

In order to leave comments, you need to log in

2 answer(s)
F
FanatPHP, 2020-10-14
@FanatPHP

The error, as usual, is in the DNA. Joke.
Remember for the rest of your life:
JSON is NEVER, under any circumstances, written by hand
. Type it in big letters and hang it on your monitor.
To get JSON, in PHP you make an array , which you then output using json_encode

A
Anton, 2020-10-14
@anton99zel

You receive data from the infoblock, you need to send it in the form of json somewhere to someone, but you have syntax errors when generating json.
You need to use a validator to check.
It helps a lot to detect extra or missing characters, compare the result with examples from the documentation.
This is quite ugly:

spoiler
$data_for_write ='
  {
    "type": "FeatureCollection",
    "metadata": {
      "name": "Карта торгового центра",
      "creator": "SWP",
      "description": ""
    },
  '.
    while($ob = $res->GetNextElement()){
      $arFields = $ob->GetFields();
      $arProps = $ob->GetProperties();
  .'
      "features": [{

First collect everything in an array:
Example
spoiler
$data = array('items' =>array(
array(
'description' => $description, 
'category_id' => $category_id, 
'name' => 'Карта торгового центра',
'offer_id' => $offer_id,
'price' => $price,
'old_price' => $old_price,

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question