Archive for March, 2011
YUI Compressor with Multiple files
Mar 24th
using YUI Compressor with multiples file into single file or one-to-one
simple batch file. helps with dependecy order as well. at least my css and javascript files do have dependecy.
the following batch is setup for CSS files. for JavaScript replace the –type argument value to js
@echo off cls echo. echo. echo Compressing CSS echo. echo. echo. :: Comment line using :: :: First command has single > to clear the file and dump the content java -jar %TWS_DIR%\TDevTool\YUICompress\build\yuicompressor-2.4.2.jar --type css tws-base.css > tws-combined.css :: dump the rest into output file using >> :: > is different than >> :: ::java -jar %TWS_DIR%\TDevTool\YUICompress\build\yuicompressor-2.4.2.jar --type css tws-carousel.css >> tws-combined.css java -jar %TWS_DIR%\TDevTool\YUICompress\build\yuicompressor-2.4.2.jar --type css tws-columnset.css >> tws-combined.css java -jar %TWS_DIR%\TDevTool\YUICompress\build\yuicompressor-2.4.2.jar --type css tws-common.css >> tws-combined.css java -jar %TWS_DIR%\TDevTool\YUICompress\build\yuicompressor-2.4.2.jar --type css tws-contact.css >> tws-combined.css java -jar %TWS_DIR%\TDevTool\YUICompress\build\yuicompressor-2.4.2.jar --type css tws-content.css >> tws-combined.css java -jar %TWS_DIR%\TDevTool\YUICompress\build\yuicompressor-2.4.2.jar --type css tws-form.css >> tws-combined.css java -jar %TWS_DIR%\TDevTool\YUICompress\build\yuicompressor-2.4.2.jar --type css tws-language.css >> tws-combined.css java -jar %TWS_DIR%\TDevTool\YUICompress\build\yuicompressor-2.4.2.jar --type css tws-linkedlist.css >> tws-combined.css java -jar %TWS_DIR%\TDevTool\YUICompress\build\yuicompressor-2.4.2.jar --type css tws-list.css >> tws-combined.css java -jar %TWS_DIR%\TDevTool\YUICompress\build\yuicompressor-2.4.2.jar --type css tws-logo.css >> tws-combined.css java -jar %TWS_DIR%\TDevTool\YUICompress\build\yuicompressor-2.4.2.jar --type css tws-navigation.css >> tws-combined.css :: java -jar %TWS_DIR%\TDevTool\YUICompress\build\yuicompressor-2.4.2.jar --type css tws-slide.css >> tws-combined.css java -jar %TWS_DIR%\TDevTool\YUICompress\build\yuicompressor-2.4.2.jar --type css tws-social.css >> tws-combined.css ::java -jar %TWS_DIR%\TDevTool\YUICompress\build\yuicompressor-2.4.2.jar --type css tws-subscribe.css >> tws-combined.css :: java -jar %TWS_DIR%\TDevTool\YUICompress\build\yuicompressor-2.4.2.jar --type css tws-tab.css >> tws-combined.css java -jar %TWS_DIR%\TDevTool\YUICompress\build\yuicompressor-2.4.2.jar --type css tws-table.css >> tws-combined.css java -jar %TWS_DIR%\TDevTool\YUICompress\build\yuicompressor-2.4.2.jar --type css tws-ovr.css >> tws-combined.css
HTML remove cookies from static files, images etc
Mar 24th
If you have not configured your application and server, it might send them to browser with cookie information.
that’s costing you as extra traffic and slow load times.
<FilesMatch "\.(css|js|jpg|jpeg|png|gif)$">
<IfModule mod_headers.c>
Header set cookie ""
</IfModule>
</FilesMatch>
HTML Reduce time on page load using performance images
Mar 24th
used this method while ago when i was working for one of those websites in top 10 the purpose was to reduce the loading time of the page and improve the parallel download
$(document).ready(function() {
/* apply performance image */
$.each($('img'), function(k, v) {
var src = $(v).attr('src');
if (src.indexOf('#') >= 0) {
src = src.split('#');
$(v).attr('src', src[1]);
}
});
});
the trick is simple. host a 1×1.gif image or don’t. up to you and separate the actual image path with ‘#’ and when the dom is ready start replacing them with original path.
make sure your images cachable.
PHP PDO Generate Insert Statement
Mar 6th
MySQL Insert Manual
http://dev.mysql.com/doc/refman/5.0/en/insert.html
PHP PDO Update Statement
http://blog.teknober.com/2011/03/06/php-pdo-generate-update-statement/
PHP PDO Replace Statement
http://blog.teknober.com/2011/04/13/php-pdo-generate-replace-statement/
Sample
/* throws error */
$result = generateInsertSet("subscription");
/* insert: single column */
$result = generateInsertSet("subscription", new TModelObject('email', "test@domain.com", PDO::PARAM_STR, 200));
/* insert: multiple column */
$result = generateInsertSet("subscription", array(
new TModelColumn('md5', md5("test@domain.com" . TDate::getDATE_MYSQL(time())), false, PDO::PARAM_STR, 100),
new TModelColumn('email', "test@domain.com", PDO::PARAM_STR, 200),
new TModelColumn('active', 1, PDO::PARAM_INT),
new TModelColumn('valid', 0, PDO::PARAM_INT)
));
$result->execute();
PDO Statement: Insert
function generateInsertSet($table, $columns = null, $execute = false, $return = false) {
if (isset($columns) == true) {
/* prepare result */
$result = null;
/* prepare sql */
$sql = "
INSERT INTO
`%s` (%s)
VALUES
(%s);";
/* prepare columns_string */
$columns_string = null;
$values_string = null;
if (is_array($columns) == true) {
/* iterate columns to generate columns_string */
foreach ($columns as $column) {
$columns_string .= sprintf(" `%s`,", $column->getKey());
$values_string .= "?,";
}
/* clean up */
unset($column);
/* remove trailing comma */
$columns_string = substr($columns_string, 0, strlen($columns_string) - 1);
$values_string = substr($values_string, 0, strlen($values_string) - 1);
} elseif ($columns instanceof TModelColumn) {
$columns_string = sprintf("`%s`", $columns->getKey());
$values_string = "?";
} else {
trigger_error("No column is defined in set", E_USER_ERROR);
}
/* prepare sql */
$sql = sprintf($sql, $table, $columns_string, $values_string);
/* prepare statement */
$result = $model->prepare($sql);
/* prepare statement parameter index */
$param_index = 0;
/* bind statement parameters */
if (is_array($columns) == true) {
foreach ($columns as $column) {
$result->bindParam(++$param_index, $column->getValue(), $column->getType(), $column->getLength());
}
/* clean up */
unset($column);
} elseif ($columns instanceof TModelColumn) {
$result->bindParam(++$param_index, $columns->getValue(), $columns->getType(), $columns->getLength());
} else {
trigger_error("No column is defined in set", E_USER_ERROR);
}
/* clean up */
unset($table, $columns, $sql, $columns_string, $values_string, $column, $param_index);
/* return PDO Statement */
if ($execute) {
$result->execute();
if ($return) {
return self::$instance->lastInsertId();
}
} else {
return $result;
}
} else {
trigger_error("No column is defined in arguments");
}
}
TModelColumn
class TModelColumn {
public $key = null;
public $value = null;
public $type = null;
public $length = null;
public function __construct($key, $value = null, $type = null, $length = null) {
$this->key = $key;
$this->value = $value;
$this->type = $type;
$this->length = $length;
}
public function getKey() {
return $this->key;
}
public function getValue() {
return $this->value;
}
public function getType() {
return $this->type;
}
public function getLength() {
return $this->length;
}
}
PHP PDO Generate Update Statement
Mar 6th
MySQL Update Manual
http://dev.mysql.com/doc/refman/5.0/en/update.html
PHP PDO Insert Statement
http://blog.teknober.com/2011/03/06/php-pdo-generate-insert-statement/
PHP PDO Replace Statement
http://blog.teknober.com/2011/04/13/php-pdo-generate-replace-statement/
Sample:
$model = new PDO(/* enter your parameters */);
$statement = generateUpdateSet("mytable", new TModelColumn("name", "Teknober", PDO::PARAM_STR, 100), new TModelWhere("id", 4, PDO::PARAM_INT, null, "="), true);
Sample 2:
$model = new PDO(/* enter your parameters */);
$statement = generateUpdateSet("mytable", new TModelColumn("name", "Teknober", PDO::PARAM_STR, 100), new TModelWhere("name", "A%", PDO::PARAM_STR, 100, "LIKE"), true);
PDO Statement: Update
function generateUpdateSet($table, $columns = null, $wheres = null, $execute = false) {
if (isset($columns) == true) {
$sql = null;
$result = null;
/* prepare columns_string */
$columns_string = null;
if (is_array($columns) == true) {
/* iterate columns to generate columns_string */
foreach ($columns as $value) {
if ($value instanceof TModelColumn) {
$columns_string .= sprintf("`%s` = ?,", $value->getKey());
} else {
trigger_error("column parameter must be instance of TModelColumn");
}
}
/* clean up */
unset($value);
/* remove trailing comma */
$columns_string = substr($columns_string, 0, strlen($columns_string) - 1);
} elseif ($columns instanceof TModelColumn) {
$columns_string = sprintf("`%s` = ?", $columns->getKey());
} else {
trigger_error("No column is defined in set", E_USER_ERROR);
}
/* prepare where_string */
$wheres_string = null;
if (is_array($wheres) === true) {
foreach ($wheres as $where) {
if ($where instanceof TModelWhere) {
$wheres_string .= sprintf(" `%s` %s ? %s", $where->getKey(), $where->getOperator(), $where->getCondition());
} else {
trigger_error("where parameter must be instance of TModelWhere");
}
}
/* clean up */
unset($value);
} elseif ($wheres instanceof TModelWhere) {
$wheres_string = sprintf(" `%s` %s ?", $wheres->getKey(), $wheres->getOperator());
}
/* prepare sql */
if (strlen($columns_string) > 0) {
$sql = sprintf("UPDATE `%s` SET %s WHERE%s;", $table, $columns_string, $wheres_string);
} else {
$sql = sprintf("UPDATe `%s` SET %s", $table, $columns_string);
}
/* prepare statement */
$result = $model->prepare($sql);
/* prepare statement parameter index */
$param_index = 0;
/* bind statement column parameters */
if (is_array($columns) == true) {
foreach ($columns as $column) {
$result->bindParam(++$param_index, $column->getValue(), $column->getType(), $column->getLength());
}
} elseif ($columns instanceof TModelColumn) {
$result->bindParam(++$param_index, $columns->getValue(), $columns->getType(), $columns->getLength());
}
/* bind statement where parameters */
if (is_array($wheres) == true) {
foreach ($wheres as $where) {
$result->bindParam(++$param_index, $where->getValue(), $where->getType(), $where->getLength());
}
} elseif ($wheres instanceof TModelWhere) {
$result->bindParam(++$param_index, $wheres->getValue(), $wheres->getType(), $wheres->getLength());
}
/* clean up */
unset($table, $columns, $wheres, $where, $sql, $columns_string, $wheres_string, $value, $param_index);
/* return PDO Statement */
if ($execute) {
return $result->execute();
} else {
return $result;
}
} else {
trigger_error("No column is defined in arguments", E_USER_ERROR);
}
}
TModelColumn Class
class TModelColumn {
public $key = null;
public $value = null;
public $type = null;
public $length = null;
public function __construct($key, $value = null, $type = null, $length = null) {
$this->key = $key;
$this->value = $value;
$this->type = $type;
$this->length = $length;
}
public function getKey() {
return $this->key;
}
public function getValue() {
return $this->value;
}
public function getType() {
return $this->type;
}
public function getLength() {
return $this->length;
}
}
TModelWhere Class
class TModelWhere extends TModelColumn {
public $condition = null;
public $operator = null;
public function __construct($key, $value = null, $type = null, $length = null, $operator = null, $condition = null) {
$this->key = $key;
$this->value = $value;
$this->type = $type;
$this->length = $length;
$this->operator = $operator;
$this->condition = $condition;
}
public function getOperator() {
return $this->operator;
}
public function getCondition() {
return $this->condition;
}
}
$options,


LinkedIn
Twitter
Skype