dqm4675
2014-08-22 14:50
浏览 34
已采纳

如何使用php将xml中的节点内容发送到另一个页面?

how to send the content of a node in xml to another page using php?

page.php

    <!DOCTYPE html>
<html>
<head>

<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1  target-densitydpi=device-dpi" />
        <link rel="stylesheet" type="text/css" href="styles.css" />
</head>
<body>
<div class="container">
<?php
$html = "";
$url = "http://d-toma.netne.net/Data.xml";
$xml = simplexml_load_file($url);
$title=$xml->page[0]->title;
$image=$xml->page[0]->image;

echo("<div class=\"main\"><img src=\"$image\"/>$title</div>");


  for($i = 1; $i<4;$i++){
$title=$xml->page[$i]->title;
$image=$xml->page[$i]->image;



    $title= $xml->page[$i]->title;
//$title=$xml->page->content->asXML();
$html .="<div class=\"sec\"><img src=\"$image\"/>$title</div>";

 }

echo $html;

?>
</div>

</body>
<html>  

i just want when i click on the div to open another page and put the content of the node i clicked on it please help me thank you i just tryed every thing but i dont know how to solve this problem

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • drti52047 2014-08-22 16:22
    已采纳

    The next solution is same you require, only I use jQ:

    <!DOCTYPE html>
    <html>
       <body>
        <header>
            <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    
            <style>
                .container {
                    position: relative;
                    width: 100%;
                }
    
                .menu {
                    float: left;
                    width: 25%;
                }
    
                .menu a {
                    text-decoration: none;
                }
    
                .page {
                    float: left;
                    width: 75%;
                }
            </style>
        </header>
        <body>
            <div class="container">
                <div class="menu">
                    <ul>
                    </ul>
                </div>
                <div class="page">
                </div>
            </div>
    
            <script>
                var $pageCollection;
    
                function buildMenu($xmlDoc) {
                    var $menu = $('div.menu > ul'),
                        $pageDetail = $('div.page'),
                        items = [];
    
                    // Get page collection
                    $pageCollection = $xmlDoc.find('page');
    
                    $pageCollection.each(function (index, element) {
                        var $page = $(element),
                            item = $('<li><a href="javascript:void(0)" data-index="' + index + '">' 
                                + $page.find('title').html()
                                + '<br />' + $page.find('desc').html() + '</a></li>');
    
                        items.push(item);
                    });
    
                    // Show page 0 for default
                    var $firstPage = $pageCollection.first(),
                        $contentPage = $firstPage.find('content').html();
    
                    $pageDetail.html($contentPage);
    
                    // Add event handlers
                    $menu.append(items);
                    $menu.on('click', 'a', onAnchorClick);
                }
    
                function getXml(url, callback) {
                    $.get(url, function (response) {
                        var $xmlDoc = $(response);
    
                        callback($xmlDoc);
                    });
                }
    
                function onAnchorClick(event) {
                    var $pageDetail = $('div.page'),
                        $anchor = $(event.currentTarget),
                        index = $anchor.attr('data-index');
    
                    // Clear previous content
                    $pageDetail.empty();
    
                    // Show current page
                    var $currentPage = $pageCollection.eq(index),
                        $contentPage = $pageCollection.find('content').html();
    
                    $pageDetail.html($contentPage);
                }
    
                $(function () {
                    var url = 'http://d-toma.netne.net/Data.xml';
    
                    getXml(url, buildMenu);
                });
            </script>
        </body>
    </html>
    

    This solution load using AJAX the XML file, and parse this find for content tag. The anchor in menu div save index position in XML, using it to select current page in XML page collection.

    Regards!

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • duanhan5388 2014-08-22 15:19

    You can use sessions. The important thing to remember is that in the header bellow (header('Location: http://www.yoururl.com/displaypage.php');) must be the same url you acessed the site with along with /displaypage.php.

    So if you accessed with 'yoururl.com' then must be: header('Location: http://yoururl.com/displaypage.php'); but if you accessed with 'www.yoururl.com' then must be: header('Location: http://www.yoururl.com/displaypage.php');. This is nescessary to preserve your access to the session.

    session_start();
    $_SESSION['TitleImageInfo'] = array();    
    
    for($i = 1; $i < 4; $i++) {
    
        $title=$xml->page[$i]->title;
        $image=$xml->page[$i]->image;
    
        //Add title, image html to the $_SESSION['TitleImageInfo'] session array.
        $_SESSION['TitleImageInfo'] = "<div class=\"sec\"><img src=" . $image . "/>" . $title . "</div>";
    }
    
    
    header("Location: http://www.yoururl.com/displaypage.php");
    

    On displaypage.php:

    <?php
        session_start();
    
        for($i = 1; $i < count($_SESSION['TitleImageInfo']); $i++) {
            echo($_SESSION['TitleImageInfo'][i]);
        }
    ?>
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题