0

これは私のコードです。mysql から xml ファイルを作成します。

私の問題:

for($i=0; $i<count($str_exp1); $i++) // HERE 
{
  $str_exp2 = explode(",", $str_exp1[$i]);

  $newnode->setAttribute("lat", $str_exp2[0]);
  $newnode->setAttribute("lng", $str_exp2[1]);  

}

すべてのデータが表示されないため...最新のデータのみが表示されます..どこに問題があるのか​​ わかりません..

PS私の英語でごめんなさい

0

$doc =  new DOMDocument("1.0");
$node = $doc->createElement("marker");
$parnode = $doc->appendchild($node);


$result = mysql_query("SELECT * FROM usersline");
if(mysql_num_rows($result)>0)
{ 
  header("Content-type: text/xml");
  while ($mar = mysql_fetch_array($result))
  {

    $node = $doc->createElement("line");
    $newnode = $parnode->appendChild($node);
    $newnode->setAttribute("id_line", $mar['id_line']);
    $newnode->setAttribute("color", $mar['colour']);
    $newnode->setAttribute("width", $mar['width']);

    $node = $doc->createElement("point");
    $newnode = $parnode->appendChild($node);

    $str_exp1 = explode(";", $mar['coordinats']);

    for($i=0; $i<count($str_exp1); $i++) // HERE 
    {
      $str_exp2 = explode(",", $str_exp1[$i]);

      $newnode->setAttribute("lat", $str_exp2[0]);
      $newnode->setAttribute("lng", $str_exp2[1]);  

    }


  }
  $xmlfile = $doc->saveXML();
  echo $xmlfile;
}
else
{
  echo "<p>Ëèíèé íå îáíàðóæåíî!</p>";
}
4

2 に答える 2

1

問題は、同じノードに複数の値を設定することです。そのため、常に属性値を最新の緯度/経度値で上書きしています。

XML 要素には重複する属性がないため、緯度と経度のペアごとに新しい要素を追加する必要があります。

あなたの質問に基づいたいくつかのサンプルコードです。ご覧のとおり、モジュールをよりモジュール化するためにいくつかの関数を導入しています。

$result = $db->query("SELECT * FROM usersline");

if (!$result || !count($result)) {
    echo "<p>Ëèíèé íå îáíàðóæåíî!</p>";
    return;
}

$doc = new DOMDocument("1.0");
$doc->loadXML('<marker/>');
$marker = $doc->documentElement;

foreach ($result as $mar) {

    $line = $doc->createElement('line');
    $attributes = array_map_array(['id_line', 'colour' => 'color', 'width'], $mar);
    element_add_attributes($line, $attributes);
    foreach (coordinates_to_array($mar['coordinats']) as $latlong) {
        $point = $doc->createElement('point');            
        element_add_attributes($point, $latlong);
        $line->appendChild($point);
    }

    $marker->appendChild($line);

}

header("Content-type: text/xml");
echo $doc->saveXML();


function element_add_attributes(DOMElement $element, array $attributes)
{
    foreach ($attributes as $name => $value) {
        if (!is_string($name)) continue;
        $element->setAttribute($name, $value);
    }
}

function array_map_array(array $map, array $array)
{
    $result = array();
    foreach ($map as $alias => $name) {
        $source = is_string($alias) ? $alias : $name;
        $result[$name] = $array[$source];
    }
    return $result;
}

function coordinates_to_array($coordinates)
{
    $result = array();
    $coordinatePairs = explode(";", $coordinates);

    foreach ($coordinatePairs as $coordinatePair) {
        list($pair['lat'], $pair['lng']) = explode(',', $coordinatePair, 2) + ['', ''];
        $result[] = $pair;
    }

    return $result;
}

この例がお役に立てば幸いです。また、コードをより簡単に、より安定させるために、問題を分離する方法をいくつか示していただければ幸いです。


メソッド$db->query(...)を持つクラスを最初に定義するには、次のようにします。query

class DB {
    public function query($sql) {
        $dbhandle = mysql_query($sql);
        $result  = array();

        while ($mar = mysql_fetch_array($dbhandle)) 
            $result[] = $mar
        ;

        return $result;
    }
}

次にインスタンス化します。

$db = new DB();

その後、その部分に上記のコードを使用できます。

たとえば、次の行の PHP 5.4 配列表記の問題については、次のようになります。

$attributes = array_map_array(['id_line', 'colour' => 'color', 'width'], $mar);

まず、そこから配列を抽出します。

$mapping    = ['id_line', 'colour' => 'color', 'width'];
$attributes = array_map_array($mapping, $mar);

次に、 andの代わりにarray(and表記を使用して配列を定義します。)[]

$mapping    = array('id_line', 'colour' => 'color', 'width');
$attributes = array_map_array($mapping, $mar);

他の場所でも同様に行います。

['', '']

になる

array('', '')

と同様です。

于 2013-02-05T19:28:39.393 に答える
0

コードを次のように置き換えます。

$str_exp1 = explode(";", $mar['coordinats']);

$newnode->setAttribute("lat", $str_exp1[0]);
$newnode->setAttribute("lng", $str_exp1[1]);
于 2013-02-05T12:56:35.063 に答える