如何处理XML模板上的多维数组? [英] How to handle Multidimensional Array on XML Template?

查看:44
本文介绍了如何处理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);
  }
);

所以我将此数组作为xml模板的输入(顺便说一句...这是json_encoded ...我只是用它来表示数组...谢谢...)

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"}]

如何在模板上处理它们?chk_db.xml

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

谢谢...

推荐答案

哦,天哪...我找到了答案...我的模板没错.我发送的数组.这是正确的代码.

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

);

我忘了给发送的数组命名...所以在这里,我将 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 ^^,

这篇关于如何处理XML模板上的多维数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆