mercredi 29 avril 2015

Proper way to write this if else statement?

How do I combine or write more efficiently this if else statement?

Is there a better way to write it without writing the entire thing twice?

The only difference is the two conditions placed on the first part to check if sitemap exists and then checks if the sitemap file modify time has changed in last 24 hours. If those two conditions are true then move forward, if those two conditions are false then move to the else part of the statement which simply creates the xml document without checking to see if the modified time has changed, since there was no file to check against.

$time  = time();
$sitemap = $_SERVER['DOCUMENT_ROOT'].'/sitemap.xml';
if (file_exists($sitemap)) { // if sitemap exists
    if ($time - filemtime($sitemap) >= 1) { // 1 days

        $xml = new DomDocument('1.0', 'utf-8'); 
        $xml->formatOutput = true; 

        // creating base node
        $urlset = $xml->createElement('urlset'); 
        $urlset -> appendChild(
            new DomAttr('xmlns', 'http://ift.tt/xwbjRF')
        );

            // appending it to document
        $xml -> appendChild($urlset);

        // building the xml document with your website content
        foreach($dirlist as $file) {
        if($file['type'] != 'text/x-php') continue;
            //Creating single url node
            $url = $xml->createElement('url'); 

            //Filling node with entry info
            $url -> appendChild( $xml->createElement('loc', 'http://www.'.$domain.$file['name']) );
            $url -> appendChild( $lastmod = $xml->createElement('lastmod', date('Y-m-d', $file['lastmod'])) );
            $url -> appendChild( $changefreq = $xml->createElement('changefreq', 'monthly') );
            $file['name'] != '/' ? $p = '0.5' : $p = '1.0';
            $url -> appendChild( $priority = $xml->createElement('priority', $p) );

            // append url to urlset node
            $urlset -> appendChild($url);

        }
        $xml->save($sitemap);
    } // if time
} // if sitemap exists

else {

        $xml = new DomDocument('1.0', 'utf-8'); 
        $xml->formatOutput = true; 

        // creating base node
        $urlset = $xml->createElement('urlset'); 
        $urlset -> appendChild(
            new DomAttr('xmlns', 'http://ift.tt/xwbjRF')
        );

            // appending it to document
        $xml -> appendChild($urlset);

        // building the xml document with your website content
        foreach($dirlist as $file) {
        if($file['type'] != 'text/x-php') continue;
            //Creating single url node
            $url = $xml->createElement('url'); 

            //Filling node with entry info
            $url -> appendChild( $xml->createElement('loc', 'http://www.'.$domain.$file['name']) );
            $url -> appendChild( $lastmod = $xml->createElement('lastmod', date('Y-m-d', $file['lastmod'])) );
            $url -> appendChild( $changefreq = $xml->createElement('changefreq', 'monthly') );
            $file['name'] != '/' ? $p = '0.5' : $p = '1.0';
            $url -> appendChild( $priority = $xml->createElement('priority', $p) );

            // append url to urlset node
            $urlset -> appendChild($url);

        }
        $xml->save($sitemap);
}

Aucun commentaire:

Enregistrer un commentaire