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

推荐订阅源

Forbes - Security
Forbes - Security
大猫的无限游戏
大猫的无限游戏
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Jina AI
Jina AI
美团技术团队
博客园 - 聂微东
博客园 - 叶小钗
Security Latest
Security Latest
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
博客园_首页
Spread Privacy
Spread Privacy
J
Java Code Geeks
雷峰网
雷峰网
宝玉的分享
宝玉的分享
C
Cyber Attacks, Cyber Crime and Cyber Security
P
Privacy International News Feed
C
CXSECURITY Database RSS Feed - CXSecurity.com
T
Threat Research - Cisco Blogs
The Hacker News
The Hacker News
量子位
L
LINUX DO - 热门话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
G
GRAHAM CLULEY
D
Darknet – Hacking Tools, Hacker News & Cyber Security
月光博客
月光博客
腾讯CDC
Last Week in AI
Last Week in AI
人人都是产品经理
人人都是产品经理
酷 壳 – CoolShell
酷 壳 – CoolShell
T
Tor Project blog
罗磊的独立博客
V
Vulnerabilities – Threatpost
Apple Machine Learning Research
Apple Machine Learning Research
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
有赞技术团队
有赞技术团队
Project Zero
Project Zero
Hugging Face - Blog
Hugging Face - Blog
爱范儿
爱范儿
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
T
Tenable Blog
MyScale Blog
MyScale Blog
T
The Exploit Database - CXSecurity.com
GbyAI
GbyAI
博客园 - 【当耐特】
O
OpenAI News
Schneier on Security
Schneier on Security
S
Secure Thoughts
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
S
Securelist
博客园 - 司徒正美

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!