【问题标题】:MySQL PHP generate XMLMySQL PHP 生成 XML
【发布时间】:2017-05-18 18:30:14
【问题描述】:

团队我在通过 PHP 从 MySQL 生成 XML 时遇到问题

我需要的 XML 需要有这种结构

<markers>
<line width="4" html="CAR1" colour="#0000FF" label="CAR1" >
<point label="04:57PM" lat="32.75" lng="-117.27" html="CAR1" />
<point label="04:58PM" lat="32.72" lng="-117.33" html="CAR1" />
</line>
<line width="4" html="CAR2" colour="#0000FF" label="CAR2" >
<point label="04:53PM" lat="32.75" lng="-117.22" html="CAR2" />
<point label="04:54PM" lat="32.75" lng="-117.23" html="CAR2" />
<point label="04:55PM" lat="32.78" lng="-117.27" html="CAR2" />
</line>

<line width="4" html="IMSI3" colour="#0000FF" label="CAR3" >
<point label="04:53PM" lat="32.73" lng="-117.22" html="CAR3" />
<point label="04:54PM" lat="32.75" lng="-117.27" html="CAR3" />
<point label="04:55PM" lat="32.72" lng="-117.33" html="CAR3" />
<point label="04:56PM" lat="32.67" lng="-117.27" html="CAR3" />
<point label="04:57PM" lat="32.65" lng="-117.15" html="CAR3" />
<point label="04:58PM" lat="32.62" lng="-117.03" html="CAR3" />
</line>
</markers>

我真的把自己变成了一个网格

我用过类似的东西

while ($row = @mysqli_fetch_assoc($result)){
  // Add to XML document node
  $node = $dom->createElement("markers");
  $newnode = $parnode->appendChild($node);
      $newnode->setAttribute("car",$row['car']);
      $newnode->setAttribute("timestamp", $row['timestamp']);
      $newnode->setAttribute("lat", $row['lat']);
      $newnode->setAttribute("lng", $row['lng']);
      $newnode->setAttribute("label", $row['label']);
}

虽然正在通过所有数据库运行,但每次 LABEL 更改时,我如何进行内部操作?请帮忙!!

我设法得到了类似的东西

//while($row =  @mysqli_fetch_assoc($resultactivos)) {
//    $activos[$index] = $row;
//  $index++;
}
//foreach($activos as $nombre){
//  $node=$dom->createElement("makers");
//  $newnode = $parnode->appendChild($node);
//  $newnode->setAttribute("activo",$nombre);
//  $query = "SELECT * from recorrido WHERE activo = '$nombre'";
//  $result = mysqli_query($connection,$query);
//  while ($row = @mysqli_fetch_assoc($result)){
//          // Add to XML document node
//          $newnode->setAttribute("tiempo", $row['timestamp']);
//          $newnode->setAttribute("lat", $row['lat']);
//          $newnode->setAttribute("lng", $row['lng']);
//          $newnode->setAttribute("car", $row['car']);
//}
//}

但还是很惨……

【问题讨论】:

    标签: php mysql xml


    【解决方案1】:

    在开始一个新元素之前,针对之前的值添加一个测试。

    # Initialize Empty Var for Label Comparison
    $prev_label = "";
    while ($row = @mysqli_fetch_assoc($result)){
      # New Node if Label Changes
      if ($prev_label != $row['label']) {
        // Add to XML document node
        $node = $dom->createElement("markers");
      }
      $prev_label = $row['label'];
    
      # Add child to Node
      $newnode = $parnode->appendChild($node);
      $newnode->setAttribute("car",$row['car']);
      $newnode->setAttribute("timestamp", $row['timestamp']);
      $newnode->setAttribute("lat", $row['lat']);
      $newnode->setAttribute("lng", $row['lng']);
      $newnode->setAttribute("label", $row['label']);
    }
    

    【讨论】:

    • 我会尝试添加更多关于嵌套 php 嵌套的信息,但还错了
    猜你喜欢
    • 2011-01-15
    • 1970-01-01
    • 1970-01-01
    • 2016-11-05
    • 2021-12-29
    • 1970-01-01
    • 2012-06-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多