Archive for April, 2011
PHP PDO Generate Replace Statement
Apr 13th
MySQL Replace Manual
http://dev.mysql.com/doc/refman/5.0/en/replace.html
PHP PDO Insert Statement
http://blog.teknober.com/2011/03/06/php-pdo-generate-insert-statement/
PHP PDO Update Statement
http://blog.teknober.com/2011/03/06/php-pdo-generate-update-statement/
Sample:
$model = new PDO(/* enter your parameters */);
$statement = generateReplaceSet("mytable", new TModelColumn("name", "Teknober", PDO::PARAM_STR, 100), null, true);
PDO Statement: Replace
function generateReplaceSet($table, $columns = null, $options = null, $execute = false) {
if (isset($columns) == true) {
/* prepare result */
$result = null;
/* prepare sql */
$sql = "
REPLACE `%s` 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 $value) {
$columns_string .= "`" . $value->getKey() . "`,";
$values_string .= "?,";
}
/* 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 = "`" . $columns->getKey() . "`";
$values_string = "?";
} else {
trigger_error("No column is defined in set", E_ERROR);
}
/* prepare sql */
$sql = sprintf($sql, $options, $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());
}
} elseif ($columns instanceof TModelColumn) {
$result->bindParam(++$param_index, $columns->getValue(), $columns->getType(), $columns->getLength());
} else {
trigger_error("No column is defined in set", E_ERROR);
}
/* clean up */
unset($table, $columns, $options, $sql, $columns_string, $values_string, $param_index);
/* return PDO Statement */
if ($execute) {
return $result->execute();
} else {
return $result;
}
} else {
trigger_error("No column is defined in arguments");
}
}
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;
}
}
$options,
PHP Create HTML Tag-Node
Apr 13th
/**
* Generates an HTML Node
* @param string $name
* @param boolean $short
* @param array $attributes
* @param string $content
* @return Ambigous <NULL, string>
*/
function createTag($name, $short = false, $attributes = null, $content = null) {
$tag = null;
if (!$short) {
$tag = sprintf("<%s%s>%s</%s>", $name, array_to_html_attributes("=", $attributes), $content, $name);
} else {
$tag = sprintf("<%s%s/>", $name, array_to_html_attributes("=", $attributes));
}
unset($name, $short, $attributes, $content);
return $tag;
}
see for array_to_html_attributes: http://blog.teknober.com/2011/04/13/php-array-to-html-attributes/
PHP array to html attributes
Apr 13th
/**
* Generates HTML Node Attribures
* @param string $glue
* @param array $pieces
* @return string
*/
public static function array_to_html_attributes($glue, $pieces) {
$str = "";
if (is_array($pieces)) {
$str = " ";
foreach($pieces as $key => $value) {
if (strlen($value) > 0) {
$str .= $key . $glue . '"' . $value . '" ';
}
}
}
return rtrim($str);
}
PHP UTF to ASCII for URL Path
Apr 13th
Converting UTF8 Content Titles to ASCII Charset to get proper URLs.
iconv did part of the job well but not all of it… so the code is:
example:
input: ÉçÖóáˇ21õ & test
output: ecooa21o-test [you wont have duplicated -'s]
public static function toAsciiUrl($url) {
$result = array();
$url = iconv("UTF-8", "ASCII//IGNORE//TRANSLIT", $url);
for($i = 0; $i < strlen($url); $i++) {
switch (ord($url[$i])) {
case 32: /* */
case 38: /* & */
case 45: /* - */
$result[] = '-';
break;
case 48: /* 0 */
$result[] = '0';
break;
case 49: /* 1 */
$result[] = '1';
break;
case 50: /* 2 */
$result[] = '2';
break;
case 51: /* 3 */
$result[] = '3';
break;
case 52: /* 4 */
$result[] = '4';
break;
case 53: /* 5 */
$result[] = '5';
break;
case 54: /* 6 */
$result[] = '6';
break;
case 55: /* 7 */
$result[] = '7';
break;
case 56: /* 8 */
$result[] = '8';
break;
case 57: /* 9 */
$result[] = '9';
break;
case 65: /* A */
case 97: /* a */
case 131: /* â */
case 132: /* ä */
case 133: /* à */
case 134: /* å */
case 142: /* Ä */
case 143: /* Å */
case 160: /* á */
case 181: /* Á */
case 182: /* Â */
case 183: /* À */
case 198: /* ã */
case 199: /* Ã */
$result[] = 'a';
break;
case 66: /* B */
case 98: /* b */
$result[] = 'b';
break;
case 67: /* C */
case 99: /* c */
case 128: /* Ç */
case 135: /* ç */
$result[] = 'c';
break;
case 68: /* D */
case 100: /* d */
$result[] = 'd';
break;
case 69: /* E */
case 101: /* e */
case 130: /* é */
case 136: /* ê */
case 137: /* ë */
case 138: /* è */
case 144: /* É */
case 210: /* Ê */
case 211: /* Ë */
case 212: /* È */
$result[] = 'e';
break;
case 70: /* F */
case 102: /* f */
$result[] = 'f';
break;
case 71: /* G */
case 103: /* g */
case 166: /* G */
case 167: /* g */
$result[] = 'g';
break;
case 72: /* H */
case 104: /* h */
$result[] = 'h';
break;
case 73: /* I */
case 105: /* i */
case 139: /* ï */
case 140: /* î */
case 141: /* i */
case 152: /* I */
case 161: /* í */
case 214: /* Í */
case 215: /* Î */
case 216: /* Ï */
case 222: /* Ì */
case 236: /* ì */
$result[] = 'i';
break;
case 74: /* J */
case 106: /* j */
$result[] = 'j';
break;
case 75: /* K */
case 107: /* k */
$result[] = 'k';
break;
case 76: /* L */
case 108: /* l */
$result[] = 'l';
break;
case 77: /* M */
case 109: /* m */
$result[] = 'm';
break;
case 78: /* N */
case 110: /* n */
case 164: /* ñ */
case 165: /* Ñ */
$result[] = 'n';
break;
case 79: /* O */
case 111: /* o */
case 147: /* ô */
case 148: /* ö */
case 149: /* ò */
case 153: /* Ö */
case 155: /* ø */
case 157: /* Ø */
case 162: /* ó */
case 224: /* Ó */
case 226: /* Ô */
case 227: /* Ò */
case 228: /* õ */
case 229: /* Õ */
$result[] = 'o';
break;
case 80: /* P */
case 112: /* p */
$result[] = 'p';
break;
case 81: /* Q */
case 113: /* q */
$result[] = 'q';
break;
case 82: /* R */
case 114: /* r */
$result[] = 'r';
break;
case 83: /* S */
case 115: /* s */
case 158: /* S */
case 159: /* s */
$result[] = 's';
break;
case 84: /* T */
case 116: /* t */
$result[] = 't';
break;
case 85: /* U */
case 117: /* u */
case 129: /* ü */
case 150: /* û */
case 151: /* ù */
case 154: /* ü */
case 163: /* ú */
case 233: /* Ú */
case 234: /* Û */
case 235: /* Ù */
$result[] = 'u';
break;
case 86: /* V */
case 118: /* v */
$result[] = 'v';
break;
case 87: /* W */
case 119: /* w */
$result[] = 'w';
break;
case 88: /* X */
case 120: /* x */
$result[] = 'x';
break;
case 89: /* Y */
case 121: /* y */
case 237: /* ÿ */
$result[] = 'y';
break;
case 90: /* Z */
case 122: /* z */
$result[] = 'z';
break;
case 95: /* _ */
$result[] = '_';
break;
case 145: /* æ */
case 146: /* Æ */
$result[] = 'ae';
break;
case 225: /* ß */
$result[] = 'ss';
break;
}
}
unset($url, $i);
/* remove duplicated - and return */
return preg_replace('{(-)\1+}','$1', implode("", $result));
}


LinkedIn
Twitter
Skype