使用SimpleXML在PHP中创建动态XML href

时间:2011-06-22 23:41:06

标签: php xml jquery-mobile simplexml

我正在尝试仅使用其各自XML数据行的后五位数创建基于URL变量的链接。

例如,如果XML linkhttp://events.stanford.edu/events/213/21389,我该如何创建此链接a href="e/?i=21389

这是我的pageXML和代码:

    <?php
    // Build the XML file path, using URL variable $c (above)
    $c = $_GET['c'];
    $p ="http://events-prod.stanford.edu/xml/byCategory/";
    $e = "/mobile.xml";
    $file = "$p$c$e";   

    $xml = simplexml_load_file($file);
    ?>

    <h1><?php echo $xml->title; ?></h1>
<a href="http://stanford.edu/group/homepage/cgi-bin/m/" data-icon="home" data-iconpos="notext" data-direction="reverse" class="ui-btn-right">Home</a>
</div><!-- /header -->
<div data-role="content">
<?php // Only display if there are events ?>
<?php if (isset($xml->Event->title)) { ?>
    <ul data-role="listview">
        <?php foreach($xml->Event as $event) { ?>
        <li>
            <a href="<?php echo $event->link; ?>">
            <?php if ($event->Media->url != null) { ?>
                <img src="<?php echo $event->Media->url;?>" alt="<?php echo $event->title;?> thumbnail" />
            <?php } ?>
                <h3><?php echo $event->title; ?></h3>
                <p><strong><?php echo $event->beginDate; ?> at <?php echo $event->beginTime; ?></strong></p>
                <p><?php echo $event->locationText; ?></p>
            </a>
        </li>
        <?php } ?>
    </ul>
<?php } else { ?>
    <?php echo '<p>There is currently nothing scheduled for ', $xml->title, '.</p>';?>
<?php } ?>

1 个答案:

答案 0 :(得分:1)

我正在使用短标签,因为我认为你会同意现在更容易阅读与以前相反的代码。

<?
    $controller ="http://events-prod.stanford.edu/xml/byCategory/";
    $category_id = $_GET['c'];
    $category_id = 0;            
    $xml = "/mobile.xml";

    $url = $controller . $category_id . $xml;       
    $xml_object = simplexml_load_file($url);
?>

<div>
    <h1><?= $xml_object->title ?></h1>
    <a href="http://stanford.edu/group/homepage/cgi-bin/m/" data-icon="home" data-iconpos="notext" data-direction="reverse" class="ui-btn-right">Home</a>
</div>

<div data-role="content">    
    <? if (!empty($xml_object->Event->title)): ?>        
        <ul data-role="listview">
        <? foreach($xml_object->Event as $event): ?>        
            <li>    
            <?
                $pattern = '/[0-9]+$/';
                $matches = array();
                preg_match($pattern, $event->link, $matches);
                $my_link = 'e/?i=' . $matches[0];
            ?>

            <a href="<?= $my_link ?>">                                
                <? if (!empty($event->Media->url)): ?>
                    <img src="<?= $event->Media->url ?>" alt="<?= $event->title ?> thumbnail" />
                <? endif; ?>

                <h3><?= $event->title ?></h3>            
                <p><strong><?= $event->beginDate ?> at <?= $event->beginTime ?></strong></p>                    
                <p><?= $event->locationText ?></p>
            </a>
            </li>
        <? endforeach; ?>        
        </ul>            
    <? else: ?>        
        <? echo '<p>There is currently nothing scheduled for ', $xml_object->title, '.</p>'; ?>            
    <? endif; ?>
</div>