<?php
//enable visitors to see the PHP source code of current file
$me basename($_SERVER['PHP_SELF']);
if(isset(
$_GET['showPHP'])){
    
show_source("$me");    
    die();
}
?>

<?php
if(isset($_POST['publish'])){
    
$html publish();
}else{
    
$html show_form();
    }
    
$html .= show_blog();

function 
show_form(){
    
$out ="<form method=\"post\" action=\"\">
           <fieldset>
           <legend>Blog</legend>
           <label for=\"heading\">Heading</label>
           <input type=\"text\" name=\"heading\" id=\"heading\"/>
           <label for=\"html\"><br/></label> 
           <label for=\"message\">Content</label> 
           <textarea name=\"content\" id=\"content\" rows='4' cols='40'></textarea> 
           <input type=\"submit\" name=\"publish\" value=\"publish\"/>
           </fieldset>
           </form>"
;
    return 
$out;
}
function 
publish(){
    
$file "blogs.xml";

    
//simplexml_load_file — Interprets an XML file into an object 
    
$xml simplexml_load_file($file);
    
    
$new_blog $xml -> addchild("blog");
    
    
$heading $_POST['heading'];
    
$new_blog -> addchild("heading"$heading);

    
$content $_POST['content'];
    
$new_blog ->addchild("content",  $content);
    
    
$date date('j n Y') ;
    
$new_blog ->addchild("date"$date);
   
    
file_put_contents($file$xml->asXML());
    
$url "blogs.php"// name of the document containing this code.
    
$out "<h3>Posted!</h3>";
    
$out .="<p>$heading - $content - $date</p>";
    
$out .= "<p><a href=\"$url\">Go to blog</a></p>"
    return 
$out;
}

function 
show_blog(){
    
$file "blogs.xml";
    
$xml simplexml_load_file($file);

    
//get <heading> from xml and use it as $data
    
$heading $xml -> blog -> heading;
    
$date $xml -> blog -> date;
    
$content $xml -> blog -> content;

    
//build form
    
$html "<p class=\"blog\">".$heading"\n <br />";
    
$html .= "\n <br />";
    
$html .= $content"\n <br />";
    
$html .= $date."</p>";
    return 
$html;
}

//NEW DOCUMENT!!
$file "blogs.xml";
    
$xml simplexml_load_file($file);


foreach(
$xml -> blog as $blog){     
    
$heading $blog -> heading;
    
$content $blog -> content;
        
$date $blog -> date;
        
$html .= "<h3>$heading</h3>
                  <p>
$content</p>
                  <p>
$date</p>";
}

?>


<!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>

<link href="assets/css.css" type="text/css" rel="stylesheet"/>
<title>XML Blogs</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<!--This meta element defines a description of my page:-->
<meta name="description" content="PHP/XML blog and Source Code" /> 

<!--This meta element defines searched keywords:-->
<meta name="keywords" content="blog, blogs" /> 

<!--The page has a NOINDEX and NOFOLLOW for SEO because it would otherwise be 
possible to spam links to on trust worthy sites in the blog and tap the sites Page Rank.
Spam links can damage your sites page rank on Google!
1. NOINDEX Tells the page that this page is not to be indexed.
2. NOFOLLOW Tells that the links(all of them) on this page should 
not be indexed.
-->
<META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">

<!--This meta element identifies the document for search engines as being intended for EN="English" readers.-->
<meta http-equiv="Content-Language" content="en"/>

<?php 
include("includes/header.inc.php");
?>

</head>
<body>
<div id="wrapper">
    <div id="center-box">

        <div class="menu bubplastic horizontal aqua">
            <ul>
                <li><span class="menu_r">
                <a href="/mobile/index.php" target="_self">
                    <span class="menu_ar">Mobile</span>
                </a></span></li>
                
                <li><span class="menu_r">
                <a href="?showPHP" target="_self">
                    <span class="menu_ar">Source code</span>
                </a></span></li>
                
                <li><span class="menu_r">
                <a href="sitemap.html" target="_self">
                    <span class="menu_ar">Site map</span>
                </a></span></li>
                
                <li><span class="menu_r">
                <a href="blogs-dk.php" target="_self">
                    <span class="menu_ar"><img src="image/dk-icon.png" alt="Dansk flag"/> Dansk</span>
                </a></span></li>
        
                <li><span class="menu_r">
                <a href="blogs.php" target="_self">
                    <span class="menu_ar"><img src="image/eng-icon.png" alt="English flag"/> English</span>
                </a></span></li>
            </ul>
          <br class="clearit" />
        </div>
    
<?php 
include("includes/header_img.inc.php");
?>
        <div id="left-right-padding">
<?php 
include("includes/navigation.inc.php");
?>

            <h1>XML &amp; php Blog</h1>
            
                          <br/>
<!--        <h2>Feel free to write in the blog demo.</h2>
            <br/>

<div id="blog">

//<?php
//echo $html; 
//
?>
</div>-->

<br/>
            <br/>
              <div id="text-in-blog">
                 <h2>XML &amp; php Blog tutorial</h2>
              <p>The XML part of the blog looks like this: </p>
                <div id="blog-xml-code">
                    <p>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;<br />
                      &lt;blogs&gt;<br />
                      &lt;blog&gt;<br />
                      &lt;heading&gt;&lt;/heading&gt;<br />
                      &lt;content&gt;&lt;/content&gt;<br />
                      &lt;date&gt;&lt;/date&gt;<br />
                      &lt;/blog&gt;<br />
                      &lt;/blogs&gt;
                    </p>
              </div>
                  
              <p>The XML code should be put in a file called blogs.xml</p>
              <p>&nbsp;</p>
              <p>The php code has to be placed in the top of the document, before the HTML.<br />
              This php document should be called blogs.php</p>
              
                 <div id="blog-php-code">
                     <p>&lt;?php<br />
                     if(isset($_POST['publish'])){<br />
                     $html = publish();<br />
                     }else{<br />
                     $html = show_form();<br />
                     }<br />
                     $html .= show_blog();</p>
                     <p>function show_form(){<br />
                     $out =&quot;&lt;form method=\&quot;post\&quot; action=\&quot;\&quot;&gt;<br />
                     &lt;fieldset&gt;<br />
                     &lt;legend&gt;Blog&lt;/legend&gt;<br />
                     &lt;label for=\&quot;heading\&quot;&gt;Heading&lt;/label&gt;<br />
                     &lt;input type=\&quot;text\&quot; name=\&quot;heading\&quot; 
                     id=\&quot;heading\&quot;/&gt;<br />
                     &lt;label for=\&quot;html\&quot;&gt;&lt;br/&gt;&lt;/label&gt; <br />
                     &lt;label for=\&quot;message\&quot;&gt;Content&lt;/label&gt; <br />
                     &lt;textarea name=\&quot;content\&quot; id=\&quot;content\&quot; 
                     rows='4' cols='40'&gt;&lt;/textarea&gt; <br />
                     &lt;input type=\&quot;submit\&quot; name=\&quot;publish\&quot; 
                     value=\&quot;publish\&quot;/&gt;<br />
                     &lt;/fieldset&gt;<br />
                     &lt;/form&gt;&quot;;<br />
                     return $out;<br />
                     }<br />
                     function publish(){<br />
                     $file = &quot;blogs.xml&quot;;</p>
                     <p> //simplexml_load_file — Interprets an XML file into an object <br />
                     $xml = simplexml_load_file($file);<br />
                     <br />
                     $new_blog = $xml -&gt; addchild(&quot;blog&quot;);<br />
                     <br />
                     $heading = $_POST['heading'];<br />
                     $new_blog -&gt; addchild(&quot;heading&quot;, $heading);</p>
                     <p> $content = $_POST['content'];<br />
                     $new_blog -&gt;addchild(&quot;content&quot;,  $content);<br />
                     <br />
                     $date = date('j n Y') ;<br />
                     $new_blog -&gt;addchild(&quot;date&quot;, $date);<br />
                     <br />
                     file_put_contents($file, $xml-&gt;asXML());<br />
                     $url = &quot;blogs.php&quot;; // name of the document containing this code.<br />
                     $out = &quot;&lt;h3&gt;Posted!&lt;/h3&gt;&quot;;<br />
                     $out .=&quot;&lt;pre&gt;$heading - $content - $date&lt;/pre&gt;&quot;;<br />
                     $out .= &quot;&lt;pre&gt;&lt;a href=\&quot;$url\&quot;&gt;
                     Go to blog&lt;/a&gt;&lt;/pre&gt;&quot;; <br />
                     return $out;<br />
                     }</p>
                     <p>function show_blog(){<br />
                     $file = &quot;blogs.xml&quot;;<br />
                     $xml = simplexml_load_file($file);</p>
                     <p> //get &lt;heading&gt; from xml and use it as $data<br />
                     $heading = $xml -&gt; blog -&gt; heading;<br />
                     $date = $xml -&gt; blog -&gt; date;<br />
                     $content = $xml -&gt; blog -&gt; content;</p>
                     <p> //build form<br />
                     $html = &quot;&lt;p class=\&quot;blog\&quot;&gt;&quot;.$heading. 
                     &quot;\n &lt;br /&gt;&quot;;<br />
                     $html .= &quot;\n &lt;br /&gt;&quot;;<br />
                     $html .= $content. &quot;\n &lt;br /&gt;&quot;;<br />
                     $html .= $date.&quot;&lt;/p&gt;&quot;;<br />
                     return $html;<br />
                     }</p>
                     <p>//NEW DOCUMENT!!<br />
                     $file = &quot;blogs.xml&quot;;<br />
                     $xml = simplexml_load_file($file);<br />
                       </p>
                       <p>foreach($xml -&gt; blog as $blog){ <br />
                     $heading = $blog -&gt; heading;<br />
                     $content = $blog -&gt; content;<br />
                     $date = $blog -&gt; date;<br />
                     $html .= &quot;&lt;h3&gt;$heading&lt;/h3&gt;<br />
                     &lt;p&gt;$content&lt;/p&gt;<br />
                     &lt;p&gt;$date&lt;/p&gt;&quot;;<br />
                     }<br />
                     ?&gt;</p>
                </div>
                <p>&nbsp;</p>
                <p>And in the body of the HTML you only have to write this small piece 
                of php to make the input form visible and get the blogged text from 
                the XML displayed.</p>
                <div id="blog-echo-code">
                    <p>&lt;?php<br />
                    echo $html; <br />
                  ?&gt;</p>
              </div>
             </div>
             <br/>
            <div class="hr"></div>
            <br/>
              <br/>
        </div>
<?php 
include("includes/footer.inc.php");
?>
    </div>
</div>
</body>
</html>