如何使用php将XML格式的节点内容发送到另一个页面


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

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

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>  

我只是想当我点击div打开另一个页面,并把我点击它的节点的内容请帮我一下,谢谢我已经尝试了所有方法,但还是不知道如何解决这个问题

下一个解决方案与您需要的相同,只是我使用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>

此解决方案使用AJAX加载XML文件,并解析此查找结果以查找content标记。菜单div中的锚在XML中保存索引位置,使用它在XML页面集合中选择当前页面。

问候!

您可以使用会话。重要的是要记住,在标题下面(header('Location: http://www.yoururl.com/displaypage.php');)必须是您访问该网站的url与/displaypage.php。

因此,如果您访问'yoururl.com',那么必须是:header('Location: http://yoururl.com/displaypage.php');,但如果您访问'www.yoururl.com',那么必须是:header('Location: http://www.yoururl.com/displaypage.php');。这对于保持对会话的访问是必要的。

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");
在displaypage.php

:

<?php
    session_start();
    for($i = 1; $i < count($_SESSION['TitleImageInfo']); $i++) {
        echo($_SESSION['TitleImageInfo'][i]);
    }
?>