1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

[CONTRIBUTION] Google Sitemap Generator for osCommerce MS-2.2 stores - Categories

Discussion in 'Google Sitemaps' started by Chemo, Jun 16, 2006.

  1. #1
    This is the next generation Google sitemap generator for osCommerce MS2.2 based stores. I have previously released a sitemap generator in the osC contribution area but that is obsolete and difficult to deal with.

    This is a one script solution and eliminates the need for CRON jobs, file permissions, and other problems that plague other scripts. All data is generated on the fly...truly a set and forget solution for osC stores.

    The attached file is for the categories sitemap.

    This version is previously unreleased but I will let it out in the wild here under GPL. You can view the script below and also download it so as to avoid copy-and-paste errors. Have fun!

    Bobby

    
    <?php
    	/**
    	 * Google Sitemap Generator - Categories
    	 * 
    	 * Script to generate a Google sitemap (categories) for osCommerce based stores
    	 *
    	 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
    	 * @version 1.1
    	 * @link http://www.oscommerce-freelancers.com/ osCommerce-Freelancers
    	 * @copyright Copyright 2006, Bobby Easland 
    	 * @author Bobby Easland 
    	 * @filesource
    	 */
    
    	/*
    	 * Include the application_top.php script
    	 */
    	include_once('includes/application_top.php');
    
    	/*
    	 * Send the XML content header
    	 */
    	header('Content-Type: text/xml');
    
    	/*
    	 * Echo the XML out tag
    	 */
    	echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
    ?>
    <urlset xmlns="http://www.google.com/schemas/sitemap/0.84">
    <?php
    
    	/*
    	 * Define the uniform node function 
    	 */
    	function GenerateNode($data){
    		$content = '';
    		$content .= "\t" . '<url>' . "\n";
    		$content .= "\t\t" . '<loc>'.trim($data['loc']).'</loc>' . "\n";
    		$content .= "\t\t" . '<lastmod>'.trim($data['lastmod']).'</lastmod>' . "\n";
    		$content .= "\t\t" . '<changefreq>'.trim($data['changefreq']).'</changefreq>' . "\n";
    		$content .= "\t\t" . '<priority>'.trim($data['priority']).'</priority>' . "\n";
    		$content .= "\t" . '</url>' . "\n";
    		return $content;
    	} # end function
    
    	/*
    	 * Define the SQL for the categories query 
    	 */
    	$sql = "SELECT 
    					c.categories_id as cID, 
    					c.date_added as category_date_added, 
    					c.last_modified as category_last_mod,
    					MAX(p.products_date_added) as products_date_added, 
    					MAX(p.products_last_modified) as products_last_mod   
    			FROM " . TABLE_CATEGORIES . " c, " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c 
    				LEFT JOIN " . TABLE_PRODUCTS . " p 
    					ON (p2c.products_id = p.products_id) 
    			WHERE c.categories_id = p2c.categories_id 
    			GROUP BY cID  
    			ORDER BY 
    				category_date_added ASC, 
    				category_last_mod ASC, 
    				products_date_added ASC, 
    				products_last_mod ASC";
    	
    	/*
    	 * Execute the query
    	 */
    	$query = tep_db_query($sql);
    
    	/*
    	 * If there are returned rows...
    	 * Basic sanity check 
    	 */
    	if ( tep_db_num_rows($query) > 0 ){
    		/*
    		 * Initialize the container
    		 */
    		$container = array();
    
    		/*
    		 * Loop query result and populate container
    		 */
    		while( $result = tep_db_fetch_array($query) ){
    			$container[$result['cID']] = max( $result['category_date_added'], 
    																				$result['category_last_mod'], 
    																				$result['products_last_mod'], 
    																				$result['products_date_added']
    																			 );
    		} # end while
    
    		/*
    		 * Free the resource...could be large
    		 * ...clean as we go
    		 */
    		tep_db_free_result($query);
    
    		/*
    		 * Sort the container based on last mod date
    		 */
    		arsort($container);
    	} # end if
    
    	/*
    	 * Loop the result set
    	 * Basic sanity check
    	 */
    	if ( sizeof($container) > 0 ){
    		$total = sizeof($container);
    		$_total = $total;
    		foreach( $container as $cID => $last_mod ){
    			$location = tep_href_link(FILENAME_DEFAULT, 'cPath=' . $cID, 'NONSSL', false);
    			$changefreq = 'weekly';
    			$priority = max( number_format($_total/$total, 1, '.', ','), .1);
    			$_total--;		
    			$container = array('loc' => htmlspecialchars(utf8_encode($location)),
    												 'lastmod' => date ("Y-m-d", strtotime($last_mod)),
    												 'changefreq' => $changefreq,
    												 'priority' => $priority
    												);
    			/*
    			 * Echo the generated node
    			 */
    			echo generateNode($container);
    		} # end while
    	} # end if
    
    	/*
    	 * Close the urlset
    	 */
    	echo '</urlset>';
    	
    	/*
    	 * Include the application_bottom.php script 
    	 */
    	include_once('includes/application_bottom.php');
    ?>
    
    PHP:

     

    Attached Files:

    Chemo, Jun 16, 2006 IP
    GTech likes this.
  2. GTech

    GTech Rob Jones for President!

    Messages:
    15,836
    Likes Received:
    571
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Thanks Chemo! I've used a number of your contributions for osC over the years and always appreciate the work you do for the community.
     
    GTech, Jun 16, 2006 IP
  3. Chemo

    Chemo Peon

    Messages:
    146
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thank you for your kind words! Obviously, every script I release is GPL so never get anything in return except the kind words from those that use them and find value.

    I have a massive library of code scripts for osC-MS2.2 that I'll hopefully be releasing on these forums over time. So far, I count another 94 or so that need to be released publicly and have not been uploaded anywhere else. I should be busy for a couple more months here :)

    However, I'm also going to be releasing tid bits from the new framework I'm completing. My goal is give you guys "code overload" :)

    Enjoy the scripts...

    Bobby
     
    Chemo, Jun 16, 2006 IP
  4. Reesy

    Reesy Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Hey Bobby,
    I hope your well!
    Thanks for this contrib, its a big improvement on the last one :)

    Quoting the above because Id like to quote your writing from the past (nov 1, 2004 to be precise)
    If you are kindly releasing your stuff can you start from here ;)
    PLEAAAAAAAASE :)
    Take care,
    Thanks, James
     
    Reesy, Jun 16, 2006 IP
  5. Chemo

    Chemo Peon

    Messages:
    146
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Reesy! It's been a while...how's everything going? Welcome to Digital Point...I think you'll find the board more conducive to forward direction and creative process.

    To answer your question as to query count (and underlying problem of server load) I have provided the tools already as contributions and even integrated it into some contributions (like Ultimate SEO URLs). The solution is the cache class which provides a convenient and efficient method of caching result sets at the data level instead of the generated HTML which is default osC code.

    To elaborate let's examine the code that is used to cache the categories menu. It is only active for those browsers that are cookie enabled since the resulting HTML will not have the SID appended to the URLs. However, if the visitor is cookie disabled all the default cache code is basically useless.

    Maybe if time allows I'll post a thorough tutorial to using the advanced cache class to improve performance. However, keep in mind that caching data must be used sensibly and there is an upper boundary to when caching makes sense and when it doesn't. A dynamic site has to remain dynamic or lose functionality.

    Once again, welcome to DP!

    Bobby
     
    Chemo, Jun 16, 2006 IP
  6. Reesy

    Reesy Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    DP is a bit over my head tbh Bobby, but Ive read it a lot as a guest trying to learn ;)
    Now your on here, I might need the email notifications :D
    If you could provide that tutorial that would be awesome.

    I for one look forward to your code and wish you good health sir!
     
    Reesy, Jun 16, 2006 IP
  7. GTech

    GTech Rob Jones for President!

    Messages:
    15,836
    Likes Received:
    571
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I look forward to the tutorial. I have an osC site that I had to move to a VPS because of the sql loads. It gets 1100-1500 unique visitors per day. A very busy site for osC. Caching is something I've contemplated to help reduce the load.

    Hope you'll stick around some this time! I remember you posted here many months ago and think your knowledge and expertise would be a valuable asset to the forum. There are quite a few osC users here.
     
    GTech, Jun 16, 2006 IP