Sort VARCHAR as INT in MySQL

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 order
SELECT vNumber FROM table ORDER BY CAST(`vNumber` AS SIGNED) DESC
Other possible conversion types you may need are:
  • BINARY[(N)]
  • CHAR[(N)]
  • DATE
  • DATETIME
  • DECIMAL[(M[,D])]
  • SIGNED [INTEGER]
  • TIME
  • UNSIGNED [INTEGER]
You can find more about CAST -> MySQL docs here.
Share and Enjoy:
  • StumbleUpon
  • Digg
  • TwitThis
  • FriendFeed
  • del.icio.us
  • MySpace
  • Technorati
  • Facebook
  • Google Bookmarks
  • Live

Recursive Category Listing

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
  • Drinks
    • Soft
      • Fruit Juice
        • Apple Juice
        • Strawberry Juice
      • Milk
Share and Enjoy:
  • StumbleUpon
  • Digg
  • TwitThis
  • FriendFeed
  • del.icio.us
  • MySpace
  • Technorati
  • Facebook
  • Google Bookmarks
  • Live

Tracking Advertisement Campaigns

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..

Share and Enjoy:
  • StumbleUpon
  • Digg
  • TwitThis
  • FriendFeed
  • del.icio.us
  • MySpace
  • Technorati
  • Facebook
  • Google Bookmarks
  • Live

Optimize PHP Codes Tricks

  1. Use echo, not print,
  2. Unset your variable when your job is over that variable, especially for large arrays,
  3. Don’t use require_once,
  4. Use full paths for includes,
  5. str_replace is faster than ereg_replace but strtr is fastest.. If you can, use strtr,
  6. Close your database connections when you are finish your process,
  7. Using $table['id'] is faster than $table[id],
  8. Use caching systems (memcache etc.)

For more about php optimizing >

Share and Enjoy:
  • StumbleUpon
  • Digg
  • TwitThis
  • FriendFeed
  • del.icio.us
  • MySpace
  • Technorati
  • Facebook
  • Google Bookmarks
  • Live

Break Categories to Columns in Wordpress

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;}
Share and Enjoy:
  • StumbleUpon
  • Digg
  • TwitThis
  • FriendFeed
  • del.icio.us
  • MySpace
  • Technorati
  • Facebook
  • Google Bookmarks
  • Live

Twitter Advertisements

There is some web tools for twitter advertisements.

Here is the list of Twitter Ads :

  • Twittertise - Advertise on Twitter
  • Twitterad - Monetize your twitter profile
  • TwitterCard - Place a 125×125 TwitterCard on your Blog or as an Advertisement
  • Twitterribbons - Twitter Ribbon Ad generator
  • TwitterKnowHow - Twitter ribbon ad generator
  • BeaMagpie - Be a Magpie is an ad network for Twitter
  • TweetTornado - Fully automated advertising software
  • FeaturedUsers - FeaturedUsers is a Twitter application ad network
  • Share and Enjoy:
    • StumbleUpon
    • Digg
    • TwitThis
    • FriendFeed
    • del.icio.us
    • MySpace
    • Technorati
    • Facebook
    • Google Bookmarks
    • Live

    Resizing All Images in Directory

    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.

    1. Resizing image with Php by Simon Jarvis
    2. Directory Listing by Spoono
    <?
    /*
    * 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("&bull; $file <br>");
       }
    }
    
    //closing the directory
    closedir($dir_handle);
    ?>
    
    Share and Enjoy:
    • StumbleUpon
    • Digg
    • TwitThis
    • FriendFeed
    • del.icio.us
    • MySpace
    • Technorati
    • Facebook
    • Google Bookmarks
    • Live

    Google Announce Chromium OS Project

    Today Google released Chromium OS, the open source project behind Google Chrome OS. Google Chrome OS is an operating system that is intended for people who spend most of their time on the web. It aims to provide a computing experience that is fast, simple and secure. The Chromium OS project as you’ll see it today is comprised of the code that has been developed thus far, our early experiments with the user interface, and detailed design docs for many parts that are under active development.

    Fore more information and announcement:

    http://chrome.blogspot.com/2009/11/announcing-chromium-os-open-source.html
    http://googletesting.blogspot.com/2009/11/testing-chrome-os.html

    Share and Enjoy:
    • StumbleUpon
    • Digg
    • TwitThis
    • FriendFeed
    • del.icio.us
    • MySpace
    • Technorati
    • Facebook
    • Google Bookmarks
    • Live

    Google Create New Programming Language : GO

    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/

    Share and Enjoy:
    • StumbleUpon
    • Digg
    • TwitThis
    • FriendFeed
    • del.icio.us
    • MySpace
    • Technorati
    • Facebook
    • Google Bookmarks
    • Live

    Turn Off Autocomplete Form in Internet Explorer

    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>
    Share and Enjoy:
    • StumbleUpon
    • Digg
    • TwitThis
    • FriendFeed
    • del.icio.us
    • MySpace
    • Technorati
    • Facebook
    • Google Bookmarks
    • Live
    Oyun