CSV Export Class for PHP

Since I’ve been working with a lot of different data sources lately, I’ve found myself needing an easy to use CSV Export class. So, here it is, CSVMaker.php

This class makes it very easy to create CSV files on the fly, from any type of data. Here’s a usage example:

$CSV = new CSVMaker();
 
// Optionally Change the Format
$CSV->csvDelimeter    = ","; // (What seperates each value in a set Foo,Bar,Etc)
$CSV->csvLine         = "\n"; // (What ends one set of data - usually a new line)
$CSV->csvCapsule      = '"'; // (What comes before and after each piece of data "Foo","Bar","Etc")
 
$CSVHeader = array();
$CSVHeader['first_name'] = "First Name";
$CSVHeader['last_name'] = "Last Name";
 
$CSV->createTemplate($CSVHeader);
 
$CSVLine = array();
$CSVLine['first_name'] = "Michael";
$CSVLine['last_name'] = "Hartmayer";
 
$CSV->addEntry($CSVLine);
 
file_put_contents("MyCSVFile.csv",$CSV->buildDoc);
Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • LinkedIn
  • Live
  • MySpace
  • Reddit
  • StumbleUpon
Categories: PHP Tags: , ,

Fetch Google Pages Indexed Using PHP and AJAX

April 1st, 2010 No comments

Here’s a quick and dirty solution I threw together to fetch how many pages of a site google has index. I needed this for one of my control panels, so my work group could track this and other metrics on the fly.

gpindex.php

<?php
	// Some arbitrary access code so not everyone can access it. Not "real" security :)
	if($_GET['accessCode']!="some_password") { echo "Access Denied"; exit; }
 
	$site = $_GET['site'];
	$gurl = "http://www.google.com/search?q=site:";
 
	$curl_handle=curl_init();
	curl_setopt($curl_handle,CURLOPT_URL,$gurl.$site);
	curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,2);
	curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1);
	$buffer = curl_exec($curl_handle);
	curl_close($curl_handle);
 
	preg_match_all("#Results(.*)of about <b>(.*)</b> from <b>$site</b>#",$buffer,$matches,0);
	echo $matches[2][0];
?>

And here’s the Ajax via jQuery that is used to fetch the information:

<script type="text/javascript" src="js/patch/jquery.min.js"></script>
<script type="text/javascript">
   $(document).ready(function(){
      $.ajax({
         url:"http://www.example.com/path/to/gpindex.php?accessCode=some_password&site=example.com",
         success:function(d){$("#googleIndexed").html(d+" Pages");}
      });
   });
</script>
<p style="font-size:11px;">Indexed on Google</p>
<p style="font-size:11px; font-weight:bold;" id="googleIndexed">( Fetching Data )</p>
Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • LinkedIn
  • Live
  • MySpace
  • Reddit
  • StumbleUpon
Categories: PHP Tags: , ,

Working on New Portfolio Theme!

December 4th, 2009 1 comment

Working on a new theme for my Portfolio! The theme I’m currently using is just something I grabbed off some site, to get my portfolio up and running. Now that I have time to rework it, I’m putting a little extra time and effort into it. So far my process has been to create a wire frame of the elements, and markup some basic functionality (I created an asset list document to work from, before creating the wires). This will make designing a whole lot easier and permit me to only have to worry about the “pretty factor”, since I’ve already mapped out my functionality and flow.

Check out my progress:

Wireframes (Layout):

Wireframe: Layout

Wireframe (Layout + Navigation Example):

Wireframe: New Layout with Navigation

Designing the Background:

Design: Background

( I’ve already got grid lines in place to make sure everything will work with 960px. width )

More Background Work + Some Layout:

Design and Layout: New Portfolio

Trying to find a good way to present the navigation:

UI - Designing Navigation

Started Filling in the Content UI:

New Portfolio Theme: Content and UI

Continued:

New Portfolio Theme: UI and Design Continued

Continued:

New Portfolio Theme - UI and Design Continued

Continued:

New Portfolio Theme: UI and Design Continued

Continued:

New Portfolio Theme: Created Custom Icons

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • LinkedIn
  • Live
  • MySpace
  • Reddit
  • StumbleUpon

IE6 / IE7 jQuery Fix: Anchor Image Clickable Area

September 25th, 2009 3 comments

IE6 and IE7 both experience a problem in which images inside of block elements inside of anchors lose their click ability. Here’s an example:

<a href="rss-icon.png">
	<span style="display:block; width:100px; height:100px;">
		<img src="someImage.png" />
	</span>
</a>

Every area of the link remains click-able except for the surface consumed by someImage.png. (Note, this problem will not show in IE8, or FF)

Here’s a very small jQuery plugin I wrote to fix this particular issue.

(function($){
	$.fn.fixClick = function() {
		return this.each(function(){
			$(this)
				.css({cursor:'pointer'})
				.click(function(){
					window.location.href = $(this).attr('href'); 
				});
		});
	}
})(jQuery);

Simply select your target element(s) and use this plugin to make the entire anchor click-able again. Here’s an example:

$(document).ready(function(){
   $('a').fixClick();
});
Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • LinkedIn
  • Live
  • MySpace
  • Reddit
  • StumbleUpon
Categories: JavaScript, Web Design Tags: , , , ,

Scripting for Smarties : Application Portability 101

September 23rd, 2009 No comments



Here’s the first ‘tutorial’-esque video that I’ve published in my new Channel Scripting for Smarties.

Enjoy~

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • LinkedIn
  • Live
  • MySpace
  • Reddit
  • StumbleUpon
Categories: PHP Tags: , ,

PHP Database Class

September 10th, 2009 No comments

I just recently wrote a brand new Database class to add to my PHP library. The old one was starting to get stale. This class is capable of taking advantage of the Singleton design pattern.

<?php
 
/**
 * @author Michael Hartmayer (michaelhartmayer[at]gmail.com)
 * @copyright 2009
 */
 
	class Db {
 
		static $ref;				// Database Resource
 
		private $user 	= 'root';		// Username
		private $pass 	= '';			// Password
		private $host 	= 'localhost';		// Host
		private $db 	= 'test';		// Database
 
		private $err 	= array();		// Error Log
		private $errHalt = true;		// Toggle: Halt on DB Error?
 
		static $q;				// Last Query
		static $r;				// Last Results
 
		static $singleton;
 
		public function Db() {
			return true;
		}//endMethod
 
		public function getInstance() {
			if(!self::$singleton)
				self::$singleton = new self;
 
			return self::$singleton;
		}//endMethod
 
		public function setUser($i) {
			$this->user = $i;
			return true;
		}//endMethod 
 
		public function setPass($i) {
			$this->pass = $i;
			return true;
		}//endMethod
 
		public function setHost($i) {
			$this->host = $i;
			return true;
		}//endMethod
 
		public function setDb($i) {
			$this->db = $i;
			return true;
		}//endMethod
 
		public function setAll($h,$u,$p,$d) {
			$this->host = $h;
			$this->user = $u;
			$this->pass = $p;
			$this->db = $d;
			return;
		}//endMethod
 
		public function doConnect(){
			if(!empty($this->ref))
				$this->doDisconnect();
 
			if(!$this->ref = @mysql_connect($this->host,$this->user,$this->pass))
				return $this->dbErr(mysql_error());
 
			if(!@mysql_select_db($this->db,$this->ref))			
				return $this->dbErr(mysql_error($this->ref));
 
			return true;
		}//endMethod
 
		public function doDisconnect() {
			if(empty($this->ref)) return false;
 
			mysql_close($this->ref);
			$this->ref = null;
 
			return true;
		}//endMethod
 
		public function getRef() { 
			return($this->ref);
		}//endMethod
 
		public function dbSet($q) {
			$this->q = $q;
			if($this->r=@mysql_query($this->q,$this->ref)) 
				return true;
 
			return $this->dbErr(mysql_error($this->ref));
		}//endMethod
 
		public function dbGet($q,$type='') {
 
			switch($type) {
				case 'STR':
					$resultType = MYSQL_ASSOC;
					break;
				case 'INT':
					$resultType = MYSQL_NUM;
					break;
				default:
					$resultType = MYSQL_BOTH;
					break;
			}
 
			$this->q = $q;
			if(!$this->r = mysql_query($this->q,$this->ref)) 
				return $this->dbErr(mysql_error($this->ref));
 
			$resultArr = array();
			while($makeDbArr = mysql_fetch_array($this->r,$resultType)) {
				$resultArr[] = $makeDbArr;
			}//endWhile
 
			return $resultArr;
		}//endMethod
 
		public function dbCount($table, $matchPartialQuery='') {
			$this->q = "SELECT COUNT(*) AS 'COUNT' FROM `$table` $matchPartialQuery;";
			if($this->r = $this->dbGet($this->q))
				return $this->r[0]['COUNT'];
 
			return $this->dbErr(mysql_error($this->err));
		}//endMethod
 
		private function dbErr($err) {
			if(empty($err)) return false;
 
			$this->err[] = $err;
 
			if($this->errHalt==true) {
				echo ">> System has halted on Db Error. <br />";
				echo ">> Error Log:<br /><br />";
				print_r($err);
				exit;	
			}
 
			return err;
		}//endMethod
 
		public function strClean($str) {
			return mysql_real_escape_string($str,$this->ref);
		}//endMethod
 
	}//endClass
 
?>
Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • LinkedIn
  • Live
  • MySpace
  • Reddit
  • StumbleUpon
Categories: PHP Tags: , , , ,

960gs Margin Mod

July 23rd, 2009 No comments

Lately I’ve been working with the marvelously easy to use and understand CSS framework, 960 Grid System. After getting started with it, I very quickly noticed that, while easy and efficient, it has a flaw. You absolutely can NOT extend your elements to end after a margin, touching the next grid element. And so, one becomes effectively shackled to having margins between any horizontally placed grid elements.

Here’s a quick Mod that I wrote that will allow you to negate that problem, without modifying the 960gs CSS itself. Also, it is important to note, that while this does allow you to push your grid class over the margin, you still can’t pull the next element back. Still, another mod could be written using the same flavor to accomplish that as well.

So, without further adieu, here it is:

960.overExtend.css

/*
 
Modifies the 960gs to allow elements to carry over
into the margins, so that they may snugly touch
the next grid over. Simply use the appropriate grid,
as you normally would, and then attach the additional
overExtend class to it, to make it over extend into
the margin. 
 
Example:
 
<div id="myContainer" class="grid_5 grid_5_overExtend">
...
</div>
 
*/
 
/* Margin Mod 12 Col */
.container_12 .grid_1_overExtend { width:80px !important; margin-right:-10px !important; }
.container_12 .grid_2_overExtend { width:160px !important; margin-right:-10px !important; }
.container_12 .grid_3_overExtend { width:240px !important; margin-right:-10px !important; }
.container_12 .grid_4_overExtend { width:320px !important; margin-right:-10px !important; }
.container_12 .grid_5_overExtend { width:400px !important; margin-right:-10px !important; }
.container_12 .grid_6_overExtend { width:480px !important; margin-right:-10px !important; }
.container_12 .grid_7_overExtend { width:560px !important; margin-right:-10px !important; }
.container_12 .grid_8_overExtend { width:640px !important; margin-right:-10px !important; }
.container_12 .grid_9_overExtend { width:720px !important; margin-right:-10px !important; }
.container_12 .grid_10_overExtend { width:800px !important; margin-right:-10px !important; }
.container_12 .grid_11_overExtend { width:880px !important; margin-right:-10px !important; }
 
/* Margin Mod 16 Col */
.container_16 .grid_1_overExtend { width:60px !important; margin-right:-10px !important; }
.container_16 .grid_2_overExtend { width:120px !important; margin-right:-10px !important; }
.container_16 .grid_3_overExtend { width:180px !important; margin-right:-10px !important; }
.container_16 .grid_4_overExtend { width:240px !important; margin-right:-10px !important; }
.container_16 .grid_5_overExtend { width:300px !important; margin-right:-10px !important; }
.container_16 .grid_6_overExtend { width:360px !important; margin-right:-10px !important; }
.container_16 .grid_7_overExtend { width:420px !important; margin-right:-10px !important; }
.container_16 .grid_8_overExtend { width:480px !important; margin-right:-10px !important; }
.container_16 .grid_9_overExtend { width:540px !important; margin-right:-10px !important; }
.container_16 .grid_10_overExtend { width:600px !important; margin-right:-10px !important; }
.container_16 .grid_11_overExtend { width:660px !important; margin-right:-10px !important; }
.container_16 .grid_12_overExtend { width:720px !important; margin-right:-10px !important; }
.container_16 .grid_13_overExtend { width:780px !important; margin-right:-10px !important; }
.container_16 .grid_14_overExtend { width:840px !important; margin-right:-10px !important; }
.container_16 .grid_15_overExtend { width:900px !important; margin-right:-10px !important; }
Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • LinkedIn
  • Live
  • MySpace
  • Reddit
  • StumbleUpon
Categories: CSS Tags: , ,

Alternating Styles in jQuery (Plug-In)

July 16th, 2009 No comments

This is a little script that I wrote, that lets you easily alternate the style of repeating class elements, or w/e elements you want to select. This is particularly useful for lists, and the like.

function($){
$.fn.styleAlternation = function(aClass, bClass) {
    var i = 0;
    return this.each(function() {
        if (++i % 2 != 0 && aClass!='') {
            $(this).addClass(aClass);
        } else if(bClass!='') {
            $(this).addClass(bClass);
        }
    });
}(jQuery);

The usage is very simple. Here’s the JavaScript:

$("#awesomeTable tr").styleAlternation("cssClass1","cssClass2");

From there, all you need to do is create your CSS. Example:

.cssClass1 { background: #000; color: #444; }
.cssClass2 { background: #999; color: #333; }

Here’s a screenshot:
Alternation Demo

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • LinkedIn
  • Live
  • MySpace
  • Reddit
  • StumbleUpon
Categories: JavaScript Tags: ,

AJAX Calendar Loader Using jQuery

July 15th, 2009 No comments

My good pal Austin needed help turning his static calendar into one that could change the month, using AJAX. The calendar itself was written in PHP. Here is the quick and easy solution I came up with for him, using jQuery:

$(document).ready(function() {
 
   // Create Date Object
   var jsDateObject = new Date();
 
   // Create Calendar Object ( Holds all your variables, etc )
   var jsCalendar = {};
       jsCalendar.currentMonth = jsDateObject.getMonth();
 
   // Controls the NEXT button
   $("#jsCalendarNext").click(function() {
      // Update Active Month
      jsCalendar.currentMonth++;
         if(jsCalendar.currentMonth>12) { jsCalendar.currentMonth=1; }
 
      // Get New Calendar
      ajaxUpdateCalendar();
   });
 
   // Controls the PREV button
   $("#jsCalendarPrev").click(function() {
      // Update Active Month
      jsCalendar.currentMonth--;
         if(jsCalendar.currentMonth<1) { jsCalendar.currentMonth=12; }
 
      // Get New Calendar
      ajaxUpdateCalendar();
   });
 
   // Use AJAX to update the Calendar
   function ajaxUpdateCalendar() {
      $.post(
         "phpCalendarLoader.php",
         { calendarMonth: jsCalendar.currentMonth },
         function(data) {
            $("#jsCalendarContainer").html(data);
         },
         "html"
      );
   }
 
});

From here, all Austin had to do was set his $month variable to $_POST['calendarMonth'];.

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • LinkedIn
  • Live
  • MySpace
  • Reddit
  • StumbleUpon
Categories: JavaScript Tags: ,

My Dad :)

June 28th, 2009 No comments

Just got my Dad’s CD put on YouTube. Nothing fancy~

He’s been singing and composing all his life and put out two CD’s. This is a small preview of his ‘Golden Classics’ Album. For more information about the CD, please get in touch with wolfhartmayer@gmail.com

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • LinkedIn
  • Live
  • MySpace
  • Reddit
  • StumbleUpon
Categories: Fun Tags: ,