doushenyu2537 2017-05-18 18:30
浏览 43
已采纳

MySQL PHP生成XML

Team I have a problem generating XML from MySQL thru PHP

The XML I need to needs to have this structure

<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>

I´m really making myself a mesh

I´have used something like that

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

while is running thru aall the database, how can I do an internal while every time LABEL changes ??? Please help !!

I´ve managed to get something like that

//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']);
//}
//}

But still faling miserably....

  • 写回答

1条回答 默认 最新

  • doumu6941 2017-05-18 18:47
    关注

    Add a test against the previous value before starting a new element.

    # 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']);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?