0

I'm trying to create a link based on a URL variable using only the last five digits of its respective line of XML data.

For example, if the XML link is http://events.stanford.edu/events/213/21389 how can I create this link a href="e/?i=21389?

Here's my page, XML and code:

    <?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 } ?>
8
  • @Ryan can you provide an example of $c ? Jun 22, 2011 at 23:48
  • @Ryan I can answer your question easily if you just gave me an example of $c. $c is a part of your URL to get the XML data. I can put in random $c values, but it returns random XML data, some that don't have the needed id's for me to properly get what I want to complete this task. Jun 23, 2011 at 0:02
  • @FinalForm - $c is determined by static HTML links from the previous page (bit.ly/k5q4Hc). In the example page above $c is "0" for Arts.
    – Ryan
    Jun 23, 2011 at 16:04
  • @Ryan k, I'll let you know if I have any other questions. I will start work now on it. Jun 23, 2011 at 16:07
  • @Ryan If you do <?= "hello world" ?> Are you able to see the output? I want to see if your php.ini file is configured a certain way. Jun 23, 2011 at 16:24

1 Answer 1

1

I'm using short tags, because I think you'll agree it's easier now to read the code as oppose to before.

<?
    $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>
1
  • It may be worth noting that you can't use just <? with IIS. Besides, isn't it considered bad to not specify a full <?php?
    – doubleJ
    Jul 16, 2012 at 14:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.