Sort VARCHAR as INT in MySQL
Jun2933 Comments " class="www.webmastersucks.com/sort-varchar-as-int-in-mysql/#respond"> 3
Sometimes we can user VARCHAR for integer values. But when we need sort them, we have got big problem. Because VARCHAR’s are string and it is sorting like a string.
Example:
1a 2a 3a 10b 20b 30b 15c 25c 35c (VARCHAR)
1a 10b 15c 2a 20b 25c 3a 30b 35c (Sorted)
We need
1a 2a 3a 10b 15c 20b 25c 30b 35c (Sorted integer)
We can do it with mysql CAST
SELECT vNumber FROM table ORDER BY CAST(`vNumber` AS SIGNED)
Reverse orderSELECT vNumber FROM table ORDER BY CAST(`vNumber` AS SIGNED) DESCOther possible conversion types you may need are:
- BINARY[(N)]
- CHAR[(N)]
- DATE
- DATETIME
- DECIMAL[(M[,D])]
- SIGNED [INTEGER]
- TIME
- UNSIGNED [INTEGER]
Recursive Category Listing
Jan2901 Comment " class="www.webmastersucks.com/recursive-category-listing/#respond"> 1
Webmasters allways use category trees, i wrote a function about creating category trees with using recursive function.
<?php header('Content-Type: text/html; charset=UTF-8'); include($_SERVER['DOCUMENT_ROOT']."/mysql.inc.php"); function categories($firstCategory,$node) { $kQ = mysql_query("SELECT * FROM categories WHERE category= ".$firstCategory); if(mysql_num_rows($kQ) > 0) { echo(str_repeat("\t",$node+1)."<ul>\n"); $node++; while($k = mysql_fetch_array($kQ)) { //str_repeat is for "view source" format, we set TAB character for readable HTML echo(str_repeat("\t",$node)."<li>".($node+1)." ".$k["name"]."</li>\n"); // Recursive, call function it self categories($k["id"], $node); } echo(str_repeat("\t",$node)."</ul>\n"); } } categories(0,-1); ?>
Output :
- Food
- Fruits
- Apple
- Banana
- Vegetables
- Tomatoes
- Aborgin
- Fruits
- Drinks
- Soft
- Fruit Juice
- Apple Juice
- Strawberry Juice
- Milk
- Fruit Juice
- Soft
Tracking Advertisement Campaigns
Jan285No Comments " class="www.webmastersucks.com/tracking-advertisement-campaigns/#respond"> 0
We can track easily with using google analytics for all advertising campaings. We use utm_source, utm_medium, utm_campaing, utm_content and utm_term for tracking..
utm_medium is using for particular marketing; email, affiliate, paid search etc.
utm_source is our basic source which is we used to advertising; google adwords, facebook ads etc.
utm_campaing is your advertising campaing name, you will see this name in google analytics; %20 discount, Free shipping etc.
And URL example for following
- utm_medium=banner-adv
- utm_source=adwords
- utm_campaign=free-membership
- utm_content=300-250-fire
www.webmastersucks.com/?utm_medium=banner-adv&utm_source=adwords&utm_campaign=free-membership&utm_content=300-250-fire
From now on we can track our campaing in Google Analytics easily..
Optimize PHP Codes Tricks
Dec281No Comments " class="www.webmastersucks.com/optimize-php-codes-tricks/#respond"> 0
- Use echo, not print,
- Unset your variable when your job is over that variable, especially for large arrays,
- Don’t use require_once,
- Use full paths for includes,
- str_replace is faster than ereg_replace but strtr is fastest.. If you can, use strtr,
- Close your database connections when you are finish your process,
- Using $table['id'] is faster than $table[id],
- Use caching systems (memcache etc.)
For more about php optimizing >