Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-13-2012, 12:02 PM   PM User | #1
alexc_6311
New to the CF scene

 
Join Date: Nov 2012
Posts: 4
Thanks: 2
Thanked 0 Times in 0 Posts
alexc_6311 is an unknown quantity at this point
Creating Link from XML document

Im trying to learn to build a new part of my website retriving information from an external xml document.

I can do all the listing bits and name codes etc etc but Im wondering how I create the retrived slug as a link. Eg. the xml document has

<slug>tm_44_compliance</slug>

My php code looks like this:

<?php
//create new document object
$dom_object = new DOMDocument();
//load xml file
$dom_object->load("http://quidos.eventhq.co.uk/.xml");

$event = $dom_object->getElementsByTagName("event");

foreach( $event as $activity )
{
$names = $activity->getElementsByTagName("name");
$name = $names->item(0)->nodeValue;


$summarys = $activity->getElementsByTagName("summary");
$summary = $summarys->item(0)->nodeValue;

$slugs = $activity->getElementsByTagName("slug");
$slug = $slugs->item(0)->nodeValue;

echo "$name - $summary - $slug<br>";
}
?>

How do I enter in that I want the slug to appear as a link eg.

http://quidos.eventhq.co.uk/{slug}

Thanks
alexc_6311 is offline   Reply With Quote
Old 11-13-2012, 04:49 PM   PM User | #2
Fumigator
UE Antagonizer


 
Fumigator's Avatar
 
Join Date: Dec 2005
Location: Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
Posts: 7,686
Thanks: 42
Thanked 637 Times in 625 Posts
Fumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of light
The trick is in the echo, which you may recall is dumping output to your browser, which can read HTML. So instead of

PHP Code:
echo "$name - $summary - $slug<br>"
You want to HTML-ize that up like so:

PHP Code:
echo "<p>$name - $summary - <a href='http://quidos.eventhq.co.uk/$slug'>$slug</a></p>"
As a side note, it's interesting you're using the DOMDocument Object to parse XML, when there are other tools in PHP better suited for parsing XML, such as the SimpleXML object.
__________________
Fumigator is offline   Reply With Quote
Users who have thanked Fumigator for this post:
alexc_6311 (11-14-2012)
Old 11-14-2012, 02:46 PM   PM User | #3
alexc_6311
New to the CF scene

 
Join Date: Nov 2012
Posts: 4
Thanks: 2
Thanked 0 Times in 0 Posts
alexc_6311 is an unknown quantity at this point
Thanks I've not got my links working!

I have also transfered away from DOMDoc to SimpleXML

DOM was first method I came across via google.

Thanks
alexc_6311 is offline   Reply With Quote
Old 11-14-2012, 03:45 PM   PM User | #4
alexc_6311
New to the CF scene

 
Join Date: Nov 2012
Posts: 4
Thanks: 2
Thanked 0 Times in 0 Posts
alexc_6311 is an unknown quantity at this point
Still appears to not be working using SimpleXML

Can someone spot the mistake?

PHP Code:
<?php
// load SimpleXML
$events = new SimpleXMLElement('http://quidos.eventhq.co.uk/.xml'nulltrue);

echo <<<EOF
<table border="1">
        <tr>
                <th>Name</th>
                <th>Summary</th>
                <th>Location</th>
                <th>City</th>
                <th>Start Date</th>
                <th>Finish Date</th>
                <th>Book</th>
        </tr>

EOF;
foreach(
$events as $event// loop through our events
{
        echo <<<EOF
        <tr>
                <td>
{$event->name}</td>
                <td width="250">
{$event->summary}</td>
                <td>
{$event->location}</td>
                <td>
{$event->city}</td>
                <td>
{$event->event_start_date}</td>
                <td>
{$event->event_end_date}</td>
                <td>
{$event->quidos.eventhq.co.uk/slug}</td>
        </tr>

EOF;
}
echo 
'</table>';
?>
alexc_6311 is offline   Reply With Quote
Old 11-14-2012, 11:04 PM   PM User | #5
Fumigator
UE Antagonizer


 
Fumigator's Avatar
 
Join Date: Dec 2005
Location: Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
Posts: 7,686
Thanks: 42
Thanked 637 Times in 625 Posts
Fumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of light
How is it not working? (Your anchor tags didn't make the last code snippet you posted, btw.)
__________________
Fumigator is offline   Reply With Quote
Old 11-15-2012, 12:06 AM   PM User | #6
minder
Banned

 
Join Date: Oct 2012
Posts: 81
Thanks: 0
Thanked 4 Times in 4 Posts
minder can only hope to improve
I copied your xml file and the below writes all the values in the <slug> elements out as links

PHP Code:
<?php
$xmlData 
simplexml_load_file('XML_data.xml');
if(!
$xmlData){
    die(
'xml load failed');
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
        <title></title>
    </head>
    <body>
        <table>
            <?php
            
foreach($xmlData->events->event as $event){
                echo 
'<tr>';
                echo 
'<td><a href="#">'.$event->slug.'</a></td>';
                echo 
'</tr>';
            }
            
?>
        </table>
    </body>
</html>
Code:
<?xml version="1.0" encoding="UTF-8"?>
<root>
    <events>
        <event>
            <name>TM44 Compliance Training Course</name>
            <slug>tm44_compliance_training_course_birmingham_nov</slug>
            <summary>Training day detailing how to complete Air-Conditioning Inspections in line with new TM44 requirements.</summary>
            <location>Villa Park</location>
            <city>Birmingham</city>
            <country>GB</country>
            <open_registration_date>2012-10-09 12:12:00</open_registration_date>
            <close_registration_date>2012-11-15 12:12:00</close_registration_date>
            <event_start_date>2012-11-15 10:00:00</event_start_date>
            <event_end_date>2012-11-15 16:30:00</event_end_date>
        </event>
        <event>
            <name>Quidos Domestic Green Deal Advisor Training</name>
            <slug>quidos_domestic_green_deal_advisor_training_bath_november</slug>
            <summary>Training to become a domestic Green Deal Advisor. </summary>
            <location>Quidos Bath</location>
            <city>Bath</city>
            <country>GB</country>
            <open_registration_date>2012-09-06 12:53:00</open_registration_date>
            <close_registration_date>2012-11-22 12:53:00</close_registration_date>
            <event_start_date>2012-11-20 10:00:00</event_start_date>
            <event_end_date>2012-11-22 16:30:00</event_end_date>
        </event>
        <event>
            <name>Quidos Floor Plan Training</name>
            <slug>quidos_floor_plan_training_november_2012_bath</slug>
            <summary>A thorough introduction into sketching floor plans. Valid for 5 hours worth of CPD. </summary>
            <location>Quidos Bath</location>
            <city>Bath</city>
            <country>GB</country>
            <open_registration_date>2012-04-21 00:00:00</open_registration_date>
            <close_registration_date>2012-11-20 17:00:00</close_registration_date>
            <event_start_date>2012-11-20 10:00:00</event_start_date>
            <event_end_date>2012-11-20 17:00:00</event_end_date>
        </event>
        <event>
            <name>DEA Refresher day</name>
            <slug>dea_refresher_day_october_2012_bath</slug>
            <summary>Aimed specifically at DEAs, offering expert insight into the industry. Valid for 5 hours of CPD. </summary>
            <location>Quidos Offices</location>
            <city>Bath</city>
            <country>GB</country>
            <open_registration_date>2012-04-21 00:00:00</open_registration_date>
            <close_registration_date>2012-11-21 17:00:00</close_registration_date>
            <event_start_date>2012-11-21 10:00:00</event_start_date>
            <event_end_date>2012-11-21 17:00:00</event_end_date>
        </event>
        <event>
            <name>NDEA Refresher day</name>
            <slug>ndea_refresher_day_october_2012_bath</slug>
            <summary>Aimed specifically at NDEAs, offering expert insight into the industry. Valid for 5 hours of CPD. </summary>
            <location>Quidos Offices</location>
            <city>Bath</city>
            <country>GB</country>
            <open_registration_date>2012-04-21 00:00:00</open_registration_date>
            <close_registration_date>2012-11-22 17:00:00</close_registration_date>
            <event_start_date>2012-11-22 10:00:00</event_start_date>
            <event_end_date>2012-11-22 17:00:00</event_end_date>
        </event>
        <event>
            <name>L3 ABBE Certificate in Domestic Energy Assessment</name>
            <slug>l3_abbe_certificate_in_domestic_energy_assessment_mdecember_2012</slug>
            <summary>Registration for qualification in L3 Domestic Energy Assessment.</summary>
            <location>Quidos Bath</location>
            <city>Bath</city>
            <country>GB</country>
            <open_registration_date>2012-04-21 00:00:00</open_registration_date>
            <close_registration_date>2012-12-04 17:00:00</close_registration_date>
            <event_start_date>2012-12-04 10:00:00</event_start_date>
            <event_end_date>2012-12-06 17:00:00</event_end_date>
        </event>
        <event>
            <name>Green Deal Advisor Assessment Day</name>
            <slug>green_deal_advisor_assessment_day</slug>
            <summary>Green Deal Advisor Assessment Day.</summary>
            <location>East of England</location>
            <city>TBA</city>
            <country>GB</country>
            <open_registration_date>2012-11-08 11:26:00</open_registration_date>
            <close_registration_date>2012-12-14 11:26:00</close_registration_date>
            <event_start_date>2012-12-10 10:00:00</event_start_date>
            <event_end_date>2012-12-14 17:00:00</event_end_date>
        </event>
        <event>
            <name>Quidos Domestic Green Deal Advisor Training</name>
            <slug>quidos_domestic_green_deal_advisor_training_bath_december</slug>
            <summary>Training to become a Domestic Green Deal Advisor.</summary>
            <location>Quidos Bath</location>
            <city>Bath</city>
            <country>GB</country>
            <open_registration_date>2012-09-06 12:30:00</open_registration_date>
            <close_registration_date>2012-12-18 12:30:00</close_registration_date>
            <event_start_date>2012-12-18 10:00:00</event_start_date>
            <event_end_date>2012-12-20 16:30:00</event_end_date>
        </event>
        <event>
            <name>Quidos Floor Plan Training</name>
            <slug>quidos_floor_plan_training_december_2012_birmingham</slug>
            <summary>A thorough introduction into sketching floor plans. Valid for 5 hours worth of CPD. </summary>
            <location>Quidos Birmingham</location>
            <city>Birmingham</city>
            <country>GB</country>
            <open_registration_date>2012-04-21 00:00:01</open_registration_date>
            <close_registration_date>2012-12-18 17:00:00</close_registration_date>
            <event_start_date>2012-12-18 10:00:00</event_start_date>
            <event_end_date>2012-12-18 17:00:00</event_end_date>
        </event>
        <event>
            <name>DEA Refresher day</name>
            <slug>dea_refresher_day_december_2012_birmingham</slug>
            <summary>Aimed specifically at DEAs, offering expert insight into the industry. Valid for 5 hours of CPD. </summary>
            <location>Quidos Birmingham</location>
            <city>Birmingham</city>
            <country>GB</country>
            <open_registration_date>2012-04-21 00:00:01</open_registration_date>
            <close_registration_date>2012-12-19 17:00:00</close_registration_date>
            <event_start_date>2012-12-19 10:00:00</event_start_date>
            <event_end_date>2012-12-19 17:00:00</event_end_date>
        </event>
        <event>
            <name>NDEA Refresher day</name>
            <slug>ndea_refresher_day_december_2012_birmingham</slug>
            <summary>Aimed specifically at NDEAs, offering expert insight into the industry. Valid for 5 hours of CPD. </summary>
            <location>Quidos Birmingham</location>
            <city>Birmingham</city>
            <country>GB</country>
            <open_registration_date>2012-04-21 00:00:01</open_registration_date>
            <close_registration_date>2012-12-20 17:00:00</close_registration_date>
            <event_start_date>2012-12-20 10:00:00</event_start_date>
            <event_end_date>2012-12-20 17:00:00</event_end_date>
        </event>
    </events>
</root>
minder is offline   Reply With Quote
Users who have thanked minder for this post:
alexc_6311 (11-15-2012)
Old 11-15-2012, 09:05 AM   PM User | #7
alexc_6311
New to the CF scene

 
Join Date: Nov 2012
Posts: 4
Thanks: 2
Thanked 0 Times in 0 Posts
alexc_6311 is an unknown quantity at this point
Sorted now!! Thanks all
alexc_6311 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:26 PM.


Advertisement
Log in to turn off these ads.