2011-06-22 30 views
0

我试图仅使用其各自的XML数据行的最后五位数字来创建基于URL变量的链接。使用SimpleXML在PHP中创建一个动态XML href

例如,如果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 } ?> 
+0

你的问题是? – hakre

+0

@Ryan你能提供一个$ c的例子吗? – FinalForm

+0

@Ryan我可以很容易地回答你的问题,如果你只给了我一个$ c的例子。 $ c是获取XML数据的URL的一部分。我可以放入随机的$ c值,但它会返回随机的XML数据,有些没有所需的ID,我无法正确地获得我想要完成此任务的内容。 – FinalForm

回答

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> 
+0

非常感谢你 - 这完美的作品!你是一个天才:) – Ryan

+0

@Ryan谢谢你的投票和点数。 :-D :-D – FinalForm

+0

值得注意的是,你不能仅仅使用'<?'和IIS。另外,如果不指定完整的'<?php',会不会不好? – doubleJ