2013年8月29日木曜日

Install ruby on rails for Aptana Studio 3 on Windows


I spent almost half of my day to install ruby on rails on my Windows PC. After a long fighting on installing, I found an EASY way for windows. Could be said as DAMN EASY.

What you need to do is download the latest version from Rails installer site and run it. Don’t need to download Ruby Installer, as it is included inside Rails installer. What you need to do is waiting until it’s done.

After installed, you can test it by open command prompt and go to whatever testing directory and type
rails new TestApps
cd TestApps
rails server

Then, open browser and go to http://localhost:3000 .If you are able to see the page. Congratulation, you are done with ruby on rails install.

Last things is to download Aptana Studio 3.

Caution here, make sure you install rails first instead of Aptana studio. Otherwise, you have to configure the path for rails on Aptana studio.

If you received error "Unable to find 'rdebug-ide' binary script. May need to install 'ruby-debug-ide' gem, or may need to add your gem executable directory to your PATH", when debug ruby in Aptana studio.



Install ruby-debug-ide by following command
gem install ruby-debug-ide

Then, restart your Aptana studio. You should be able to debug ruby file.

Okay! Hope you are already in coding. Thank you for reading.

2013年8月21日水曜日

Crop Images into pieces for java applet

By using CropImageFilter class, images can be cropped easily with few lines. According to Java docs, parameter of CropImageFilter constructor is
        CropImageFilter(int x, int y, int w, int h)

First, load the image from folder. Here's is the method I prepared.
    private Image loadImage(String img){
        return getImage(app.getDocumentBase(),"img/"+img);
    }

For eclipse IDE, I put my images file inside the src/img package.


After load the image, let's say "image_want_chop.png" is loaded and chop it with following code.
        Image originalImage = loadImage("image_want_chop.png");
        ImageFilter imageFilter = new CropImageFilter(x,y,width,height); 
        Image choppedImage = createImage(new FilteredImageSource(originalImage.getSource(),
                        imageFilter ));

Thank you for reading.

2013年8月20日火曜日

Split MySQL select data into multiple pages by using LIMIT


Our goal is to split the selected MySQL data into multiple pages and display current page and the data show on specify page.
mysql_page1


Introduce to LIMIT clause

Let’s say the limit of data on single page is 10 rows, and current page is page 2. It means I need to show the data begin from 20th to 30th. So what I need to do is to set the offset value and length of data selected.
    $offset = 0;
    $length_of_data = 10;
    $query = "SELECT * FROM mytable LIMIT {$offset} ,{$length_of_data}";

The parameter of LIMIT clause is LIMIT offset, length

Optimize the query


To prevent sql injection, we need to modify the code above into bottom.
    $length_of_data = 10;
    $query = "SELECT * FROM mytable LIMIT :offset ,{$length_of_data}";
    $page = intval(trim($_GET["page"]));
    $offset = $page * $length_of_data;
 
    try{               
        $stmt = $db->prepare($query);
        $stmt->bindParam(':offset',$offset,PDO::PARAM_INT);
        $stmt->execute();
    }catch(PDOException $ex){        
        die ($ex);
    }

Caution : According to stackoverflow, seem like there is a bug when using execute(array(“:offset”=>0)) or bindParam(“:offset”,trim($offset),PDO::PARAM_INT) without intval function. So it is better using what I given on above. I hope it works for your guys.

You can test it by add ?page=0 to your end of the url. It will show you the first 10 rows data without offset.

Display current page and all available pages


It is the last things we need to do. First get the total rows number from table. Then show the current page with special view and other pages with normal view.
 
    $query = "SELECT * FROM mytable";
    try{
        $stmt = $db->prepare($query);
        $stmt->execute();
    }catch(PDOException $ex){
        die ($ex); 
    }

    $rows = $stmt->fetchAll();
    $count = count($rows);
    for($num = 0;$num < $count / $limit_row;$num++){     
        if($page == $num){
            echo '<a href="forum_main.php?page'.$num.'"><span class="page_number current">'.   ($num+1).'</span></a>';
        }else{         
            echo '<a href="forum_main.php?page='.$num.'"><span class="page_number">'.($num+1).'</span></a>';
        }
    }

Here’s we done! Thank you for reading.