We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 12168
    • 19 Posts
    I had this idea of being able to change the background based on the weather. Then I started googling and came across this article. http://css-tricks.com/using-weather-data-to-change-your-websites-apperance-through-php-and-css/.. Then I thought I might try to convert it for MODx, problem is i’m not an expert in php or in making snippets. I’m going to try to play around with it. But if anyone has a solution before me please keep me updated.

    Thanks in Advance

      • 28042 ☆ A M B ☆
      • 24,524 Posts
      Just remember to use a return statement to actually output which CSS file you want to use:
      // determine the weather, such as rain or sunny or snow, and store it in a variable
      
      return $weather;
      

      Then use it in your template head
      <link rel="stylesheet" type="text/css" href="assets/templates/mytemplate/[!weather!].css" />

      This way you’ll get sunny.css, or snow.css; whatever the weather is determined to be. Use uncached snippet, otherwise the pages taken from cache will never get their css file changed.
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 12168
        • 19 Posts
        Thanks for the tips.

        - I made a snippet called [!Weather!]. Not sure if it’s the 100% correct way of doing it, but all I did was add return $iWeather; and it worked.

        - I Navigated to weather.yahoo.com. Searched my city and then navigated to the RSS to get my Local weather URL.

        - Couldn’t get it to work in Revolutions. But very excited to use this in Evolutions.

        Snippet call returns cloudy, rain, light-rain and...

        <?php
        /* get xml, find match */
        	
        	/* get the weather from Yahoo */
        	$data = get_data("http://weather.yahooapis.com/forecastrss?p=CAXX0345&u=f");
        	
        	$iWeather = format_result(get_match('/<yweather:condition  text="(.*)"/isU',$data));
        		
        	/* debug to see what we got back */
        	//echo '<pre style="background:#fff;font-size:12px;">['; print_r($iWeather); echo ']</pre>';
        	
        	/* format the result */
        	function format_result($input)
        	{
        		return strtolower(str_replace(array(' ', '(', ')'), array('-', '-', ''), $input));
        	}
        	
        	/* helper:  does regex */  
        	function get_match($regex,$content)  
        	{  
        		preg_match($regex,$content,$matches);  
        		return $matches[1];  
        	}
        	
        	/* gets the xml data from Alexa */
        	function get_data($url)
        	{
        		$ch = curl_init();
        		$timeout = 5;
        		curl_setopt($ch,CURLOPT_URL,$url);
        		curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
        		curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
        		$xml = curl_exec($ch);
        		curl_close($ch);
        		return $xml;
        	}
        return $iWeather;
        ?>


          • 18913
          • 654 Posts
          Nicely done. I amended your code to deal with the full array of codes presented at the site to which you linked. So the start of my version of your snippet looks like this :
          $weatherra =array(0=>'tornado',
          1=>'tropical storm',
          2=>'hurricane',
          3=>'severe thunderstorms',
          4=>'thunderstorms',
          5=>'mixed rain and snow',
          6=>'mixed rain and sleet',
          7=>'mixed snow and sleet',
          8=>'freezing drizzle',
          9=>'drizzle',
          10=>'freezing rain',
          11=>'showers',
          12=>'showers',
          13=>'snow flurries',
          14=>'light snow showers',
          15=>'blowing snow',
          16=>'snow',
          17=>'hail',
          18=>'sleet',
          19=>'dust',
          20=>'foggy',
          21=>'haze',
          22=>'smoky',
          23=>'blustery',
          24=>'windy',
          25=>'cold',
          26=>'cloudy',
          27=>'mostly cloudy (night)',
          28=>'mostly cloudy (day)',
          29=>'partly cloudy (night)',
          30=>'partly cloudy (day)',
          31=>'clear (night)',
          32=>'sunny',
          33=>'fair (night)',
          34=>'fair (day)',
          35=>'mixed rain and hail',
          36=>'hot',
          37=>'isolated thunderstorms',
          38=>'scattered thunderstorms',
          39=>'scattered thunderstorms',
          40=>'scattered showers',
          41=>'heavy snow',
          42=>'scattered snow showers',
          43=>'heavy snow',
          44=>'partly cloudy',
          45=>'thundershowers',
          46=>'snow showers',
          47=>'isolated thundershowers');
          
          	/* get the weather from Yahoo */
          	$data = get_data("http://weather.yahooapis.com/forecastrss?p=USVT0196&u=f");
          	
          	//$iWeather = format_result(get_match('/<yweather:condition  text="(.*)"/isU',$data));
          	$iWeathercode  = format_result(get_match('/  code="(.*)"/isU',$data));
                  $iWeather = $weatherra[$iWeathercode];


          That ought to give you plenty of background options to play with wink

          Thanks for sharing that...
          MattC
            • 18913
            • 654 Posts
            Now that I think about it, I guess you could offer up sound effects and animations depending on the weather. And add in a seasonal page in the event that the weather is "clear."

            A web guy at a former company I worked with had snow going across the screen of anyone logging into the company intranet on the day before a holiday. It was a nice diversion for once ...
            MattC
              • 3749
              • 24,544 Posts
              It looks to me like it should work in Revolution if you call it with [[!Weather]] (assuming that curl is enabled at the site).
                Did I help you? Buy me a beer
                Get my Book: MODX:The Official Guide
                MODX info for everyone: http://bobsguides.com/modx.html
                My MODX Extras
                Bob's Guides is now hosted at A2 MODX Hosting
                • 12168
                • 19 Posts
                I am just learning, is this the correct way to get a chunk? The code below works. I just wanted to make sure I’m doing it correctly.

                $iTPL = isset($tpl) ? $tpl : ' ';   
                
                 if ($iTPL==' ')
                        $iWoutput = $iWcity.', '.$iWcondition.', '.$iWtemp; 
                else
                        $iWoutput = $modx->getChunk($iTPL);
                
                return $iWoutput;


                  • 3749
                  • 24,544 Posts
                  Quote from: hillnick at Nov 30, 2009, 11:00 AM

                  I am just learning, is this the correct way to get a chunk? The code below works. I just wanted to make sure I’m doing it correctly.

                  $iTPL = isset($tpl) ? $tpl : ' ';   
                  
                   if ($iTPL==' ') {
                          $iWoutput = $iWcity.', '.$iWcondition.', '.$iWtemp; 
                   } else {
                          $iWoutput = $modx->getChunk($iTPL);
                   }
                  
                  return $iWoutput;




                  Looks right to me, although for style and maintainability, I’d do it with curly braces as above.
                    Did I help you? Buy me a beer
                    Get my Book: MODX:The Official Guide
                    MODX info for everyone: http://bobsguides.com/modx.html
                    My MODX Extras
                    Bob's Guides is now hosted at A2 MODX Hosting