Answer the question
In order to leave comments, you need to log in
Script error. Where is the extra data coming from?
Good afternoon! There are three tables containing subscriber addresses. Of these, I'm interested in STREET, HOUSE, APARTMENT. I need to create an xml file containing only unique street names, each with unique house numbers, each with unique apartment numbers.
All this is complicated by the fact that the xml file is generated for the entire city, and the database for the city can have several tables, so you need to make several selections (one per table) and combine the resulting data.
The script code to do this is below.
XML is being formed, but for some reason, for some houses (mainly in the last streets), tags are added with apartments that do not exist (from 1 to 179), although in this house there is essentially only one apartment (a private house).
Where do these apartments come from???Where to look for the error?
Thanks in advance!
Thanks to the user Denis Denis Symfony2, for the proposed solution. The corrected code is written below. Everything works perfectly.
header("Content-Type: text/html; charset=utf-8");
error_reporting(E_ALL);
ini_set('display_errors', 1);
ini_set('xdebug.var_display_max_depth', -1);
include '../connect.php';
$tables = array(
"table" => "1",
"table" => "2",
"table" => "3",
);
$array = array();
foreach ($tables as $table) {
$table_query = pg_query($dbconn, "SELECT street, house_number, app_number from $table");
$data = pg_fetch_all ($table_query);
$array = array_merge($array, $data);
}
$output = array();
foreach ($array as $value) {
$output[$value['street']][$value['house_number']][] = $value['app_number'];
}
$dom = new DOMDocument('1.0', 'utf-8');
$dom->formatOutput = true;
// создаем корневой элемент
$root = $dom->createElement('root');
foreach ($output as $key => $street) {
$street_tag = $dom->createElement("street");
$street_tag->formatOutput = true;
$street_tag = $root->appendChild($street_tag);
$street_tag->setAttribute("street", "$key");
$street_tag->appendChild( $dom->createTextNode(''));
foreach ($street as $key => $house) {
$house_tag = $dom->createElement("house");
$house_tag->formatOutput = true;
$house_tag = $street_tag->appendChild($house_tag);
$house_tag->setAttribute("house", "$key");
$house_tag->appendChild( $dom->createTextNode(''));
foreach ($house as $key => $appartment) {
$app_tag = $dom->createElement("appartment");
$app_tag->formatOutput = true;
$app_tag = $house_tag->appendChild($app_tag);
$app_tag->setAttribute("appartment", "$appartment");
$app_tag->appendChild( $dom->createTextNode(''));
}
}
}
$dom->appendChild($root);
// сохраняем результат в строку
$save_xml = $dom->saveXML();
// выводим полученную строку
echo $save_xml;
Answer the question
In order to leave comments, you need to log in
First, refactor your code.
Then get rid of the huge number of requests to the database.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
ini_set('xdebug.var_display_max_depth', -1);
$array = [];
foreach ($tables as $table) {
/* pg_query... 'SELECT street, house, appartment FROM '.$table */
$data = [];
$array = array_merge($array, $data);
}
$output = [];
foreach ($array as $value) {
$output[$value['street']][$value['house']][] = $value['appartment'];
}
// array {
// 'ул. Уличная' => [
// 'дом 5' => [1, 2, 3],
// 'дом 6' => [1, 2, 3, 4, 5],
// ],
// 'ул. Домашняя' => [
// 'дом 1' => [1],
// 'дом 2' => [1, 2, 3, 4, 5],
// ]
// }
$dom = new DOMDocument('1.0', 'utf-8');
foreach ($array as $street) {
// $dom add $street
foreach ($street as $house) {
// $dom add $street
foreach ($house as $appartment) {
// $dom add $appartment
}
}
}
Man, hardly anyone will understand such noodles. Make it at least in the form of separate functions. Separate getting data, transforming it and creating xml. At each stage, control what your code will produce. You'll see, the error will be found by itself after putting the code in order. And read about formatting. https://github.com/php-fig/fig-standards/blob/mast...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question