惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
Tenable Blog
博客园_首页
Vercel News
Vercel News
WordPress大学
WordPress大学
美团技术团队
G
Google Developers Blog
大猫的无限游戏
大猫的无限游戏
小众软件
小众软件
Y
Y Combinator Blog
博客园 - 【当耐特】
量子位
酷 壳 – CoolShell
酷 壳 – CoolShell
The Cloudflare Blog
T
The Blog of Author Tim Ferriss
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google DeepMind News
Google DeepMind News
云风的 BLOG
云风的 BLOG
腾讯CDC
M
MIT News - Artificial intelligence
爱范儿
爱范儿
Recent Announcements
Recent Announcements
雷峰网
雷峰网
Last Week in AI
Last Week in AI
宝玉的分享
宝玉的分享
The Register - Security
The Register - Security
Jina AI
Jina AI
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Hugging Face - Blog
Hugging Face - Blog
P
Privacy & Cybersecurity Law Blog
Recorded Future
Recorded Future
Help Net Security
Help Net Security
N
News and Events Feed by Topic
博客园 - Franky
P
Proofpoint News Feed
L
LINUX DO - 热门话题
S
SegmentFault 最新的问题
The GitHub Blog
The GitHub Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
月光博客
月光博客
D
Docker
Google DeepMind News
Google DeepMind News
有赞技术团队
有赞技术团队
IT之家
IT之家
Security Latest
Security Latest
L
LangChain Blog
V
V2EX
阮一峰的网络日志
阮一峰的网络日志
J
Java Code Geeks

MarcySutton.com RSS Feed

On Joining Khan Academy Developing anti-SLAPP policies for A11y Slack with Harvard Cyberlaw Clinic Focus on What Matters Celebrating One Year of Independence as Modern Sole Design, LLC Evinced is Pushing the Limits of Automated Accessibility Testing Content-visibility and Accessible Semantics Finding accessibility jobs in specialized companies and the mainstream Outsider Leverage and Accessibility Encouraging Open Source Contributions with Docs: a Self-Fulfilling Prophecy Remote Work and Van Life Salary and Career Growth Prototype Testing for Accessible Client-Side Routing On Great Leadership, Gatsby & Girl Develop It The Deal with Developer Advocacy Live Coding Accessibility Chapter Two at Deque 2017, in Music Writing winning abstracts Accessibility is a Civil Right 2016, a Year of Milestones Best of 2016 Music Links vs. Buttons in Modern Web Applications Accessibility and Performance I won an O Web Accessibility Resources This is what a developer looks like. What Wally On writing better captions for images What I’ve Learned Working on a Large Open-Source Framework Speak at your local elementary school. Button Focus Hell Page Scrolling in Mobile Safari & VoiceOver Accessibility Wins Notes from CSUN 2015 Protractor Accessibility Plugin Riding a bicycle to an accessibility conference 2014: One to Remember AngularJS Material Design & ngAria Summing Up JSConf EU 2014 How I Audit a Website for Accessibility Accessibility and the Shadow DOM: JSConf Australia 2014 CSUN 2014 Conference Recap Accessibility and the Shadow DOM Favorite Music 2013 Girl Develop It Web Accessibility Mobile Web Accessibility with VoiceOver Webstock & NZ 2013 Favorite Music 2012 Target Corporate Site Redesign: Accessible & Responsive Web Development Decibel Festival Recap 2012 Favorite Music 2011 Spiceboard: Wordpress Recipes for iPad POP Clock Favorite Music 2010 CSS + JS + Accessibility Christmas JS1k Zend Framework. NACCC Urban Type Sutton RV AS3 Load Workflow AS3 Mouse Events Holiday 2009 Why Outlook Sucks
Simplexml in php 5
2010-04-25 · via MarcySutton.com RSS Feed

April 25, 2010

I found a new love in PHP — SimpleXML. I recently discovered I can traverse an XML tree just like I do in Actionscript, right in the middle of an HTML page. This makes it easy to create a website with all content in an XML document, a technique I like to use on projects that do not have the budget for a CMS. XML is useful for organizing site navigation and image/copy content in a central place, thus making it a breeze to update. You can then use SimpleXML to inject content into an HTML page with just a few lines of code.

According to the W3Schools:
“The SimpleXML function lets you convert XML to an object… This object can be processed, like any other object, with normal property selectors and array iterators.”

Using SimpleXML is as easy as loading the XML file and adding it to a SimpleXML object (this requires PHP 5):

<?php
$file = file_get_contents('navigation.xml');
$xml = new SimpleXMLElement($file);
?>

Say you have your navigation organized in a basic XML file. It can be as simple or complex as you like; that’s the beauty of XML. My example below has attributes for page titles and URLs, which could easily be combined into one attribute and transformed accordingly with a PHP loop — for this example I kept them separate. Query strings are totally optional — it just depends on how you want to generate your subpage URLs (using $_GET or separate physical pages?).

// navigation.xml
<?xml version="1.0" encoding="UTF-8"?> 
<navigation>
	<page title="Home" url="index.php" />
	<page title="Projects" url="projects.php">
		<subpage title="Personal" query="personal" />
		<subpage title="Technical + Geeky" query="technical" />
		<subpage title="Creative" query="creative" />
	</page>
	<page title="About Me" url="about.php" />
	<page title="Contact" url="contact.php" />
</navigation>

You can structure your XML file however you like by adding/removing child nodes and attributes to fit your website’s particular requirements. To generate HTML output from the XML file above, I like to use a foreach loop:

 
<div id="nav">
 
<?php
$file = file_get_contents('navigation.xml');
$xml = new SimpleXMLElement($file);
 
$checkit = $_SERVER['SCRIPT_FILENAME'];
// for checking the current page 
$path = 'http://.'$_SERVER['HTTP_HOST'].'/';
// useful for generating full subpage URLs and preserving the domain in links 
 
$counter = 0;
 
echo "<ul>\n";
foreach($xml->children() as $page) {
   $numSubChildren = count($page->children());
    <li";
   if($numSubChildren > 0) {  // add a class to <li>s with children 
   echo" class='hasChild'";
   }
// form href from path and url attribute 
 echo"><a href='". $path . $page->attributes()->url. "' class='navLink";
 
// replace pesky ampersands in attributes 
$pageTitle = str_replace(' & ', ' &amp; ', $page->attributes()->title); 
 
// add class to current link 
 if(strstr($checkit, $page->getName()))  {
	echo " current'>".$pageTitle;
}
 else {
	echo "'>".$pageTitle;
}
echo "</a>\n";
 
if($numSubChildren > 0) {
echo " <li id='sub".$counter.">
           <ul>";
 
   foreach($page->children() as $subpage) {
// loop through subpages 
$subtitle = str_replace('&','&amp;',$subpage->attributes()->title);
 
echo " <li class=\"subLi\"><a href='".$path.$page->attributes()->url."/".$subpage->attributes()->query;
 
// this method uses URL rewriting, but can also be used with plain query strings
// ?subpage=$subpage->attributes()->query; 
 
echo "' class='navLink'>".$subtitle."</a><li>\n";
		}
echo "	</ul>\n";
$counter++;
	}
echo " 	<li>\n";
echo " </ul>\n";
?>
</div>

I love this method because it is so simple to use — but it has potential to grow into complex code, as you can see. Holler at me if you have any questions or if you find this useful!