Sort VARCHAR as INT in MySQL
Jun1
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
Jan0
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
Jan0
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
http://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
Dec0
- 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.)
Break Categories to Columns in Wordpress
Dec0
Normally the category list is single column. But you can use this trick to split two columns.
Firstly, find
<?php wp_list_categories(); ?>
Then replace this code like here :
<?php
$cats = explode("<br />",wp_list_categories('title_li=&echo=0&depth=1&style=none'));
$cat_n = count($cats) - 1;
for ($i=0;$i<$cat_n;$i++):
if ($i<$cat_n/2):
$cat_left = $cat_left.'<li>'.$cats[$i].'</li>';
elseif ($i>=$cat_n/2):
$cat_right = $cat_right.'<li>'.$cats[$i].'</li>';
endif;
endfor;
?>
<ul class="left">
<?php echo $cat_left;?>
</ul>
<ul class="right">
<?php echo $cat_right;?>
</ul>
After changing this you can stylize your columns with css file (style.css)
.right {float:left; width:140px;}
.left {float:left; width:140px;}
Resizing All Images in Directory
May0
I need a script for resize all images in one directory. I have create thumbnails for a site. I have not so much time write these code. I found two different code and mixed them. I love PHP, it is very easy to solve problem with little scripts. If you resize in same directory it will be resize resized images. If you need in same directory, just resized for temp directory and move it to your real one.
<?
/*
* Class: SimpleImage, Author: Simon Jarvis, Copyright: 2006 Simon Jarvis, Date: 08/11/06
* Link: http://www.white-hat-web-design.co.uk/articles/php-image-resizing.php
* See the GNU General Public License for more details: http://www.gnu.org/licenses/gpl.html
*/
class SimpleImage {
var $image;
var $image_type;
function load($filename) {
$image_info = getimagesize($filename);
$this->image_type = $image_info[2];
if( $this->image_type == IMAGETYPE_JPEG ) {
$this->image = imagecreatefromjpeg($filename);
} elseif( $this->image_type == IMAGETYPE_GIF ) {
$this->image = imagecreatefromgif($filename);
} elseif( $this->image_type == IMAGETYPE_PNG ) {
$this->image = imagecreatefrompng($filename);
}
}
function save($filename, $image_type=IMAGETYPE_JPEG, $compression=75, $permissions=null) {
if( $image_type == IMAGETYPE_JPEG ) {
imagejpeg($this->image,$filename,$compression);
} elseif( $image_type == IMAGETYPE_GIF ) {
imagegif($this->image,$filename);
} elseif( $image_type == IMAGETYPE_PNG ) {
imagepng($this->image,$filename);
}
if( $permissions != null) {
chmod($filename,$permissions);
}
}
function output($image_type=IMAGETYPE_JPEG) {
if( $image_type == IMAGETYPE_JPEG ) {
imagejpeg($this->image);
} elseif( $image_type == IMAGETYPE_GIF ) {
imagegif($this->image);
} elseif( $image_type == IMAGETYPE_PNG ) {
imagepng($this->image);
}
}
function getWidth() {
return imagesx($this->image);
}
function getHeight() {
return imagesy($this->image);
}
function resizeToHeight($height) {
$ratio = $height / $this->getHeight();
$width = $this->getWidth() * $ratio;
$this->resize($width,$height);
}
function resizeToWidth($width) {
$ratio = $width / $this->getWidth();
$height = $this->getheight() * $ratio;
$this->resize($width,$height);
}
function scale($scale) {
$width = $this->getWidth() * $scale/100;
$height = $this->getheight() * $scale/100;
$this->resize($width,$height);
}
function resize($width,$height) {
$new_image = imagecreatetruecolor($width, $height);
imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
$this->image = $new_image;
}
}
// End of SimpleImage class
/* Directory Listing
Source : http://www.spoono.com/php/tutorials/tutorial.php?id=10
*/
// Define the path as relative
$path = "/home/site/public_html/images/";
// Using the opendir function
$dir_handle = @opendir($path) or die("ERROR: Cannot open <b>$path</b>");
echo("Directory Listing of $path<br/>");
//running the while loop
while ($file = readdir($dir_handle))
{
if($file != "." && $file != "..")
{
$image = new SimpleImage();
$image->load("images/".$file);
$image->resize(80,60);
$image->save("images2/mini_".$file);
echo("• $file <br>");
}
}
//closing the directory
closedir($dir_handle);
?>
Google Create New Programming Language : GO
Nov0
Google Engineers create new programming language “Go”. In first look it is look like C++ or Java. We will see, how it will be.
More details :
http://www.golang.me/google-launches-its-own-programming-language-go/
Turn Off Autocomplete Form in Internet Explorer
Nov1
A webmastersucks user ask me “The only problem is that IE will store the MD5 ] password when it “saves” the password. Have you been able to overcome that?”. Solution is really simple, just we can use autocomplete attribute in form tag. It will be close autocomplete in form. It will better in password protected sites.
<form name="form" method="post" autocomplete="off">....</form>


