PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
<?php // Load correct xml for corresponding page. function page_xml($page) { $xml_list = array( 'home' => 'http://cm.cyloop.com/feeds/drupal/cyloop_mobile.xml', 'Artist' => 'http://cm.cyloop.com/feeds/drupal/cyloop_mobile.xml', 'People' => 'http://cm.cyloop.com/feeds/drupal/cyloop_mobile.xml', 'News' => 'http://cm.cyloop.com/feeds/drupal/cyloop_mobile.xml' ); $xml = new SimpleXMLElement($xml_list[$page]); return $xml; } ?>
HTML
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
<?php require "assets/functions.php"; page_xml(home); $content = $xml->xpath('item/title'); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html> <head> <meta http-equiv="Content-type" content="text/html; charset=utf-8"> <title>Cyloop Mobile</title> </head> <body> <div id="container"> <div id="header">Cyloop Mobile</div> <div id="nav"> <ul> <li>Home</li> <li>Artist</li> <li>People</li> <li>News</li> </ul> </div> <div id="wrapper"> <ul> <?php foreach ($content as $artist) { echo "<li>$artist</li>"; }?> </ul> </div> </div> </body> </html>
Refactorings
No refactoring yet !
sofia
June 10, 2008, June 10, 2008 01:56, permalink
You really should look into separating your php code from the html - that way all your relevant code is in one place and not divided into several files like you have here, and it looks much nicer. Look into template engines like Savant, Smarty or phpTal.
Anyway you returned the xml from the page_xml function but you forgot to assign it to a variable.
1 2
//replace this page_xml(home); with code below
$xml = page_xml('home');
hubfactor
June 25, 2008, June 25, 2008 11:26, permalink
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
<?php function page_xml($page){ $xml = array( 'home' => 'cyloop_mobile.xml', 'artist' => 'cyloop_mobile.xml', 'people' => 'cyloop_mobile.xml', 'news' => 'cyloop_mobile.xml' ); return simplexml_load_file('http://cm.cyloop.com/feeds/drupal/' . $xml[$page]); } $xml = page_xml('home'); foreach ($xml->xpath('item/title') as $artist) printf('<li>%s</li>', htmlspecialchars($artist));
twar59
July 5, 2008, July 05, 2008 17:42, permalink
Assuming you want to use MVC, as you should. Don't put your xpath in your view, instead, pass a simple variable to your view. Fixed the bug, missing params for SimpleXMLElement(), your version was trying to parse the string rather than pulling the document from the url.
Controller
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
<?php // Load correct xml for corresponding page. function page_xml($page) { $xml_list = array( 'home' => 'http://cm.cyloop.com/feeds/drupal/cyloop_mobile.xml', 'Artist' => 'http://cm.cyloop.com/feeds/drupal/cyloop_mobile.xml', 'People' => 'http://cm.cyloop.com/feeds/drupal/cyloop_mobile.xml', 'News' => 'http://cm.cyloop.com/feeds/drupal/cyloop_mobile.xml' ); $xml = new SimpleXMLElement($xml_list[$page], null, true); return $xml; } $xml = page_xml('home'); $contents = $xml->xpath('item/title'); ?> %% View [php] <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html> <head> <meta http-equiv="Content-type" content="text/html; charset=utf-8"> <title>Cyloop Mobile</title> </head> <body> <div id="container"> <div id="header">Cyloop Mobile</div> <div id="nav"> <ul> <li>Home</li> <li>Artist</li> <li>People</li> <li>News</li> </ul> </div> <div id="wrapper"> <ul> <?php foreach ($contents as $artist): ?> <li><?= $artist?></li> <?php endforeach ?> </ul> </div> </div> </body> </html>
I have a list of pages, with corresponding xmls that need to be parsed. Im trying to create a function where im trying to just past the page name and have it return the correct xml. I'm new to programming so I'm just trying to figure this out as i go.