doupeng2253 2016-05-03 11:45
浏览 35
已采纳

显示xml结果范围

I want to make a pagination like div. Right now div is populated with xml, which displays 6 results. I want on a click of a button to display next/previous 6 results. I have no idea how to force xml return results like from matches[0] to matches[5] and on next click matches[6] to matches[11].

XML in widget.php:

foreach ($xml->team->last_matches->match as $match) {
   //some php and html stuff 
}

if javascript variable that I pass to file widget.php is limit, want something like:

for ($x = 0; $x < $_GET['limit']; x++)
   foreach ($xml->team->last_matches->match[x] as $match) {
       //some php and html stuff 
    }
}

Not sure where to specify to display last six entries up to limit. Thanks in advance.

  • 写回答

1条回答 默认 最新

  • dpd66100 2016-05-04 00:37
    关注

    Consider using xpath's node index denoted by square brackets []. Below demonstrates with an example XML of Google Doodles:

    XML

    <root>
        <GoogleDoodles>
            <ID>1758</ID>
            <DoodleDate>2015-07-01</DoodleDate>
            <Doodle>Canada Day 2015</Doodle>
        </GoogleDoodles>
        <GoogleDoodles>
            <ID>1759</ID>
            <DoodleDate>2015-07-04</DoodleDate>
            <Doodle>Fourth of July 2015</Doodle>
        </GoogleDoodles>
        <GoogleDoodles>
            <ID>1760</ID>
            <DoodleDate>2015-02-15</DoodleDate>
            <Doodle>50th Anniversary of the Canadian Flag</Doodle>
        </GoogleDoodles>
        <GoogleDoodles>
            <ID>1761</ID>
            <DoodleDate>2015-02-15</DoodleDate>
            <Doodle>Cricket World Cup 2015 - India vs. Pakistan</Doodle>
        </GoogleDoodles>
        <GoogleDoodles>
            <ID>1762</ID>
            <DoodleDate>2015-02-17</DoodleDate>
            <Doodle>Carnival 2015</Doodle>
        </GoogleDoodles>
        <GoogleDoodles>
            <ID>1763</ID>
            <DoodleDate>2015-02-16</DoodleDate>
            <Doodle>Rosenmontag 2015</Doodle>
        </GoogleDoodles>
        <GoogleDoodles>
            <ID>1764</ID>
            <DoodleDate>2015-02-18</DoodleDate>
            <Doodle>Alessandro Volta's 270th Birthday</Doodle>
        </GoogleDoodles>
        <GoogleDoodles>
            <ID>1765</ID>
            <DoodleDate>2015-02-24</DoodleDate>
            <Doodle>Rosalia de Castro's 178th Birthday</Doodle>
        </GoogleDoodles>
        <GoogleDoodles>
            <ID>1766</ID>
            <DoodleDate>2015-02-19</DoodleDate>
            <Doodle>Lunar New Year 2015 (Vietnam)</Doodle>
        </GoogleDoodles>
        <GoogleDoodles>
            <ID>1767</ID>
            <DoodleDate>2015-02-26</DoodleDate>
            <Doodle>Jose Mauro de Vasconcelos' 95th Birthday</Doodle>
        </GoogleDoodles>
        <GoogleDoodles>
            <ID>1768</ID>
            <DoodleDate>2015-02-19</DoodleDate>
            <Doodle>Lunar New Year 2015</Doodle>
        </GoogleDoodles>
    </root> 
    

    PHP Script

    $xml = simplexml_load_file('path/to/xml/file.xml');
    # $xml = simplexml_load_string($xmlstring);
    
    $limit = 5;
    
    for($j=0; $j <= $limit; $j++) {
    
        foreach ($xml->xpath("//GoogleDoodles[".$j."]/Doodle") as $d) {
            echo $d->saveXML()."
    ";
        }
    
    }    
    
    // <Doodle>Canada Day 2015</Doodle>
    // <Doodle>Fourth of July 2015</Doodle>
    // <Doodle>50th Anniversary of the Canadian Flag</Doodle>
    // <Doodle>Cricket World Cup 2015 - India vs. Pakistan</Doodle>
    // <Doodle>Carnival 2015</Doodle>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)