0

this my code. It create xml file from mysql..

my problem:

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]);  

}

for not show the all data... it only show me latest one data.. i cant find where is there problem..

P.S. Sorry for my english

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>";
}
Sirko
  • 72,589
  • 19
  • 149
  • 183
brio
  • 175
  • 2
  • 5
  • 16
  • 1
    Without seeing the relevant XML file, it's not easy to tell what's going wrong, but as a guess are you intentionally mis-spelling "coordinates" in the $str_exp1 = explode(";", $mar['coordinats']); line? – John Parker Feb 05 '13 at 12:52
  • XML working good. i have not problem with xml.. http://nn-gis.com/map/line.php this my xml output.. my problem. For must give me 4 latlong coordinates but it gives me only one – brio Feb 05 '13 at 12:59

2 Answers2

1

Your problem is that you set multiple values to the same node. So you are always overwriting the attribute values with the latest lat/long value.

Instead you need to add a new element per each lat/long pair because XML elements do not have duplicate attributes.

Some example code based on your question, as you can see I introduce some functions to keep things more modular:

$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;
}

I hope this example is helpful and shows you some ways how you can put a problem apart so that your code becomes more easy and more stable.


To make use of $db->query(...) first define a class that has the query method:

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

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

        return $result;
    }
}

Then instantiate it:

$db = new DB();

You can then use the code above for that part.

For the problem with the PHP 5.4 array notation for example in this line:

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

First of all extract the array out of it:

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

Then define the array with the array( and ) notation instead of [ and ]:

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

Do so as well in other places, e.g.

['', '']

becomes

array('', '')

and similar.

hakre
  • 193,403
  • 52
  • 435
  • 836
  • thanks for answer.. Thanks for help... it gives me error.. cant solve it.. :( Parse error: syntax error, unexpected '[', expecting ')' in /home/content/78/9527178/html/map/line.php on line 16 – brio Feb 05 '13 at 19:51
  • The example code is written in PHP version 5.4. You are not using that PHP 5.4 version. You can learn more about the `[` character in PHP 5.4 here: http://php.net/array - to get such errors away, change those parts into *your* PHP version - whichever PHP version you are using. Should be trivial, probably `array(`. – hakre Feb 05 '13 at 23:29
  • thanks for answer. i did it.. but dont understand this error: Fatal error: Call to a member function query() on a non-object in /// i didnt see there mysql query.. (( – brio Feb 06 '13 at 11:35
  • @brio, the `$db` object is exemplary (e.g. PDO), you use the `mysql_*` functions (which I do not because they are outdated). It's just some preference. Also if you run into errors, we have a reference online here on site for common errors (like the last one): [Reference - What does this error mean in PHP?](http://stackoverflow.com/q/12769982/367456). – hakre Feb 06 '13 at 11:43
  • soory.. its so hard for me((.. i dont understand it((.. please can your help with php version < 5.4.. I could not adjust php 5.3:( – brio Feb 06 '13 at 14:09
  • @brio: Just put your querying in there and instead of the foreach take your while loop. I just don't have that so I could mock the code. Also you find an edit with some hints for your PHP version. – hakre Feb 06 '13 at 14:20
  • broooooo thanks sooo much!!!! thnk you... array_map_array() here must be array_map )).. all is good! thanks! – brio Feb 06 '13 at 16:51
0

Replace your code with this:

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

$newnode->setAttribute("lat", $str_exp1[0]);
$newnode->setAttribute("lng", $str_exp1[1]);
Bram Verstraten
  • 1,414
  • 11
  • 24
  • not working(( give s error: Line Number 2, Column 1:Warning: explode() expects parameter 2 to be string, array given in /home/content/78/9527178/html/map/line.php on line 28
    ^
    – brio Feb 05 '13 at 13:23
  • It's just a warning. It happens when your string is empty. You can ignore it or disable error reporting. – Bram Verstraten Feb 05 '13 at 13:31
  • i know it. )) bu my string is not empty..it must show me data. if i disable error reporting xml working but lat and long data now showing.. – brio Feb 05 '13 at 13:38