普通网友 2015-06-03 23:57
浏览 80
已采纳

如何处理XML模板中的多维数组?

So here is my code... Which I used in my Project.

$app->post(
  '/chk_db',
    function () use ($app){
      require_once 'lib/mysql.php';
      $dx = connect_db('MyPhotos');

      //XML RESPONSE
      $app->response->setStatus(0);
      $res = $app->response();
      $res['Content-Type'] = 'application/xml';
      $view = $app->view();
      $view->setTemplatesDirectory('./');
      $oArray = array("Status"=> $dx.status, "code" => $dx.code);
      return $app->render('chkdb.xml', $oArray);
  }
);

so i have this Array as an input to the xml template (by the way... this is json_encoded... I just use it to represent the array... thanks...)

[{"ObjID":"1","ParenetID":"10001","Path":"http:\/\/localhost\/img\/1.jpg","Title":"1st Image","ChildCount":"0","Owner":"jhim","Comment":"hehe","inode":"0"},
 {"ObjID":"2","ParenetID":"10002","Path":"http:\/\/localhost\/img\/2.jpg","Title":"2nd Image","ChildCount":"0","Owner":"nemy","Comment":"test lang","inode":"0"},
 {"ObjID":"3","ParenetID":"10003","Path":"http:\/\/localhost\/img\/3.jpg","Title":"3rd Image","ChildCount":"0","Owner":"jayjay","Comment":"para amy output","inode":"0"},
 {"ObjID":"4","ParenetID":"10004","Path":"http:\/\/localhost\/img\/4.jpg","Title":"4th Image","ChildCount":"0","Owner":"jhim","Comment":"yeah boy","inode":"0"}]

How can I handle them on the template? chk_db.xml

{% for x in ????%}
<MyPhotos>
<ObjID>{{x.ObjID}}</ObjID>
...
<inode>{{x.inode}}</inode>
</MyPhotos>
{% else %}
<data>No data Found</data>
{% endfor %}

Thanks...

  • 写回答

2条回答 默认 最新

  • dpa84373 2015-06-04 01:40
    关注

    Oh man... my bad.. I found the answer to this... There is nothing wrong on my Template.. it's my bad not naming the Array I send.. Here's the corrected code..

    $app->post(
    '/get_gallery_allphotos',
        function () use ($app) {
            require_once 'lib/mysql.php';
            $re = getAll_photos('MyPhotos');
            $app->response->setStatus(200);
            $app->response()->headers->set('Content-Type', 'text/xml');
            return $app->render('myphotos_allphotos_admin.xml', array("ArrayName" => $re));
        }
    
    );
    

    I forgot to name the Array i send... So here i put ArrayName as the name of the $re...

    So in the template i use the..

    {% for x in ArrayName %}
    <MyPhotos>
        <ObjID>{{x.ObjID}}</ObjID>
        <ParenetID>{{x.ParenetID}}</ParenetID>
        <Path>{{x.Path}}</Path>
        <Title>{{x.Title}}</Title>
        <ChildCount>{{x.ChildCount}}</ChildCount>
        <Owner>{{x.Owner}}</Owner>
        <Comment>{{x.Comment}}</Comment>
        <inode>{{x.inode}}</inode>
    </MyPhotos>
    {% else %}
        Not Found
    {% endfor %}
    

    Hope it helps somebody else ^^,

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 请求分析基于spring boot+vue的前后端分离的项目
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥200 关于#c++#的问题,请各位专家解答!网站的邀请码
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?