RIP - IE

Code Igniter Library for Layouts

October 28th 2009

Here is a simple library that takes advantages of layouts in code igniter. You can call it in your controller like:

$this->layout->view('dashboard');

You can pass data to it just like load->view:

$data = array('foo'=>'bar'); $this->layout->view('dashboard',$data); 

Either configure your autoloader to load the layout or load the library in your controller:

$this->load->library('layout');

If you wish to change the layout from default you do so in your controller:

$this->layout->setLayout('secondary');

<?php  
if (!defined('BASEPATH')) exit('No direct script access allowed');

class Layout {

	var $obj;
	var $layout;

	function Layout($layout = "default"){
		$this->obj =& get_instance();
		$this->layout = $layout;
	}

	function setLayout($layout){
		$this->layout = $layout;
	}

	function view($view, $data=null, $return=false){
		$loadedData['content_for_layout'] = $this->obj->load->view($view,$data,true);
		if($return):
			$output = $this->obj->load->view('layouts/'.$this->layout, $loadedData, true);
			return $output;
		else:
			$this->obj->load->view('layouts/'.$this->layout, $loadedData, false);
		endif;
	}
	
}
?>
352 downloads

2 Comments

Code Igniter Model for Fetching Yahoo Weather Temp and Conditions.

October 27th 2009

Comes with basic SQL for building the database. This script is designed to update weather once every 5 minutes. No need for cron, this will just pull data if the current information in the database is older than 5 minutes. It will also archive previous weather information for long term usage.

Visit Gist on Github

<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');
/*
CREATE TABLE IF NOT EXISTS `weather` (
  `id` int(11) NOT NULL auto_increment,
  `timestamp` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
  `recorded_at` datetime NOT NULL,
  `temp` float NOT NULL,
  `conditions` varchar(150) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;
*/
class Weather extends Model {

	var $zip_code = '87525';

	var $recorded_at;
	var $timestamp;
	var $temp;
	var $conditions;

	function __construct(){
		parent::Model();
		$this->getLatest();
	}
	
	function getLatest(){
		
		# load from db first..
		$this->loadFromDatabase();
		
		# this will fetch and store weather..
		if($this->timeToGetNewWeather() == true):
			$this->fetchAndStoreWeather();
		endif;
		
	}
	
	private function timeToGetNewWeather(){
		if(strtotime($this->timestamp." +5 minutes") < time()):
			return true;
		endif;
		return false;
	}
	
	private function loadFromDatabase(){
		$this->db->order_by('timestamp','desc');
		$stats = $this->db->get('weather',1)->row();
		$this->temp = $stats->temp;
		$this->conditions = $stats->conditions;
		$this->recorded_at = $stats->recorded_at;
		$this->timestamp = $stats->timestamp;
	}
	
	private function fetchAndStoreWeather(){

		$url = 'http://weather.yahooapis.com/forecastrss?p='.$this->zip_code;
		$xml = $this->stream_remote_file($url);
		$weatherData = $this->simplexml->xml_parse($xml);
		
		$fields = array();
		$fields['temp'] = $weatherData['channel']['item']['yweather:condition']['@attributes']['temp'];
		$fields['conditions'] = $weatherData['channel']['item']['yweather:condition']['@attributes']['text'];
		$fields['recorded_at'] = date('Y-m-d H:i:s',strtotime($weatherData['channel']['item']['yweather:condition']['@attributes']['date']));

		$this->db->insert('weather', $fields);
		
		$this->loadFromDatabase();
		
	}
	
	private function stream_remote_file($url){
		$handle = fopen($url, "rb");
		$contents = stream_get_contents($handle);
		fclose($handle);
		return $contents;
 	}

}
?>
365 downloads

No comments, post your comment here.

Google Graphing Class

August 8th 2009

Sharing my super super simple google graphing class to draw line charts. This is used in various projects. I really would like to see what kind of additions people can make to this class so feel free to fork this project on github and see what can be added! Visit github project page here.

Or just clone the git project: git clone git://github.com/wes/googleGraph.git 

 

<?php
class graph {
	
	var $data = array();
	var $opts = array();
	var $colors;
	var $areaColors;
	var $labels;

	function render(){
	
		foreach($this->data as $d){
			$dd = explode(",",$d);
		}
		
		$maxAxis = max($dd);
		
		if(empty($this->colors)){
			$this->colors = '000000';
		}
		if(empty($this->areaColors)){
			$this->areaColors = 'D,4D89F9,0,0,5';
		}
	
		if(empty($this->opts['w'])){ $this->opts['w'] = 100; }
		if(empty($this->opts['h'])){ $this->opts['h'] = 100; }
		if(empty($this->opts['class'])){ $this->opts['class'] = 'graph'; }
		if(empty($this->opts['bg'])){ $this->opts['bg'] = 'ffffff'; }
		if(empty($this->labels)){ $this->labels = array(); }
		
		$url = array();
		$url[] = "http://chart.apis.google.com/chart?";
		$url[] = "cht=ls";
		$url[] = "&chs=".$this->opts['w']."x".$this->opts['h'];
		$url[] = "&chd=t:".implode(",",$this->data);
		$url[] = "&chco=".$this->colors;
		$url[] = "&chl=".implode('|',$this->labels);
		$url[] = "&chm=".$this->areaColors;
		$url[] = "&chg=20,50,1,5";
		$url[] = "&chxt=x,y";
		$url[] = "&chbh=7,2";
		$url[] = "&chds=-2,".$maxAxis;
		$url[] = "&chf=bg,s,".$this->opts['bg'];
		
		return "<img src='".implode("",$url)."' width='".$this->opts['w']."' height='".$this->opts['h']."' border='0' />";
		
	}
	
}
?>
269 downloads

1 Comment

Web Browsers in a Treehouse Illustration

June 28th 2009

 

1 Comment

Taos Inn disguised as El Gallo Cantina on Tonight Show

June 20th 2009

1 Comment

1 2 3 4 5 6 7 8 9