Bryan Seeds

21Apr/110

PHP : csv string to associative array

Here is a handy function :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
function CsvStr2Array($str = ''){
		$data = array();
		$a = str_getcsv($str, "\n");
	    foreach($a as $k => $v){
	    	if($k == 0){
	    		$columns = str_getcsv($v);
	    	} else {
	    		if($k > 0){
	    			$v = preg_replace("/[^[:print:]]+/", "", $v);
	    			$row = str_getcsv($v);
	    			foreach($columns as $i => $col){
	    				if(is_null($row[$i])){
	    					continue;
	    				}
	    				$data[$k-1][$col] = $row[$i];
	    			}
	    		}
	    	}
	    }
	    return $data;
	}

might be the most efficient regarding big O notation.. but.. it works =)

23Mar/100

Custom Magento Module for CMS Page

Today just wanted to discuss how to make a custom Magento module that will allow you to make your own blocks to load into a CMS page using the layout updates.  Really not going to get in depth in the the Models and controllers part of the module creation, going to save that for another time..  anyhow here it is.

First let's create the file to enable the module in magento :

app/etc/modules/{Namespace}_{ModuleName}.xml

<?xml version="1.0"?>
<config>
    <modules>
       <[Namespace_ModuleName]>
           <active>true</active>
           <codePool>local</codePool>
       </[Namespace_ModuleName]>
     </modules>
</config>

Next, we are just going to create a configuration file to define the Block configuration only at this point, for our module :

app/code/local/{Namespace}/{ModuleName}/etc/config.xml

<?xml version="1.0"?>
<config>
<global>
<blocks>
<[namespace]_[modulename]>
<class>[Namespace]_[ModuleName]_Block</class>
</[namespace]_[modulename]>
</blocks>
</global>
</config>

Now, let's go ahead and create our own Block class code to define the object used for our CMS/template page.

app/code/local/{Namespace}/{ModuleName}/Block/MyBlock.php

<?php
 
class <Namespace>_<ModuleName>_Block_MyBlock extends Mage_Core_Block_Abstract
{
 
public function __construct()
{
parent::__construct();
// any instantiation code here
}
 
protected function _toHtml()
{
 
// by overriding this method, you can define here everything you will render to browser, and use the inherited methods and accessors i.e. - $this->get[ParamName]()
 
return $html;
}
 
}

Last thing is to just throw this line into the CMS page content text area to load this custom Magento Block module.

 
{{block type="[namespace]_[modulename]/myblock" param1="value" param2="value"}}

So, that's it in a nutshell.. I will expand on this later for those who need more =)

29Aug/090

CodeIgniter a PHP Framework

After searching around for a good PHP framework to produce rapid web applications for clients, I had come across CodeIgniter's solution.  From my intial exposure last year to now, this framework has proven time and again to be an asset in assisting me to get jobs done.  Out of the box it has database abstraction, MVC architecture,  optimized PHP 5 object-orientation, superb documentation, and a great community.  This framework has won me over, even with my anti-framework attitude.  After trying to find flaws and pulling the code base apart, finally I gave in.  This is a tool added to my belt for sure.