Pages

Jun 29, 2011

MySQL export to file

Export a query result to a file directly from MySQL.

First open mysql with "-s -t" arguments - this will force mysql to output without boxing characters:
mysql -s -t -u <user> <database_name>

Now:
SELECT * FROM table INTO OUTFILE '/path/to/file.txt'

It's all good, peace!

Typical MySQL encoding problem

Don't you just HATE encodings?

Ever wanted to insert something in MySQL and were 100% sure the text/database/table/fields were in UTF-8 and by some reason the text got all scrambled up?

This usually works for me:

mysql_query("SET NAMES 'utf8'");

Hope it helps!

Jun 17, 2011

jQuery preload images

Cool jquery image preload method:

(function($) {
 var imgList = [];
 $.extend({
  preload: function(imgArr, option) {
   var setting = $.extend({
    init: function(loaded, total) {},
    loaded: function(img, loaded, total) {},
    loaded_all: function(loaded, total) {}
   }, option);
   var total = imgArr.length;
   var loaded = 0;
   
   setting.init(0, total);
   for(var i in imgArr) {
    imgList.push($("")
     .attr("src", imgArr[i])
     .load(function() {
      loaded++;
      setting.loaded(this, loaded, total);
      if(loaded == total) {
       setting.loaded_all(loaded, total);
      }
     })
    );
   }
   
  }
 });
})(jQuery);
 
 
$(function() {
 
 $.preload([
  "http://farm3.static.flickr.com/2661/3792282714_90584b41d5_b.jpg",
  "http://farm2.static.flickr.com/1266/1402810863_d41f360b2e_o.jpg"
 ], {
  init: function(loaded, total) {
   $("#indicator").html("Loaded: "+loaded+"/"+total);
  },
  loaded: function(img, loaded, total) {
   $("#indicator").html("Loaded: "+loaded+"/"+total);
   $("#full-screen").append(img);
  },
  loaded_all: function(loaded, total) {
   $("#indicator").html("Loaded: "+loaded+"/"+total+". Done!");
  }
 });
 
});

From: http://ditio.net/2010/02/14/jquery-preload-images-tutorial-and-example/
Live example: http://ditio.net/demo/preloader/

jQuery "sleep" function plugin - doTimeout

jQuery doTimeout: Like setTimeout, but better!

It's really not a "sleep" function but it acts just like we want when we go "oh! I could really use a sleep function right now!"

$.doTimeout(3000,function(){
    alert('yeeey!');
});

Homepage: http://benalman.com/projects/jquery-dotimeout-plugin/
GitHub: http://github.com/cowboy/jquery-dotimeout

Jun 16, 2011

PHP5 Dynamically calling Classes and Methods

Easy peasy:

$class_name = 'NiceClass';
$method_name = 'niceMethod';
$arguments = array('a' => true, 'b' => 5);

if (class_exists($class_name)) {
  $class = new $class_name();
  if (method_exists($class, $method_name)) {  
    $class->$method_name($arguments);
  }
  
}