Codeigniter

CKEditor in CodeIgniter

Sunday, 29 August 2010 22:32

ကြၽန္ေတာ္ ဒီ Post မွာ CKEditor ကို CodeIgniter မွာ ဘယ္လိုေပါင္း ထည့္မလဲ ဆိုတဲ့ Tutorial ကိုေရးမွာ ျဖစ္ပါတယ္။ ဒါေၾကာင့္ CI မွာ Helper ေတြ၊ Library ေတြကို ဘယ္လိုေပါင္းထည့္လို႕ ရတယ္ ဆိုတာကို ေရးထားတဲ့ ဒီ Post ကိုအရင္ ဖတ္ထားဖို႔ လိုပါတယ္။

CI ထဲကို ေပါင္းထည့္မယ့္ CKEditor အေၾကာင္းေလး ဗဟုသုတ အျဖစ္ နဲနဲၾကည္ လိုက္ရေအာင္။ CKEditor ဆိုတာကေတာ့ Web ေပၚမွာ user friendly ျဖစ္ေအာင္ လုပ္ေပးထားတဲ့ WYSIWYG editor အမ်ိဳးအစားပဲ ျဖစ္ပါတယ္။ WYSIWYG ကဆိုတာကေတာ့ What-You-See-Is-What-You-Get ကိုေျပာတာ ျဖစ္ပါတယ္။ ကြၽန္ေတာ္တို႔ Word ထဲမွာ Font Size, Font color, Font Style ေတြ၊ Number & Bullet ေတြ၊ Table Format ေတြကို အလြယ္တကူ လုပ္ႏိုင္ သလိုမ်ိဳး Web Page ေတြမွာ CKEditor ကိုသုံးျပီး လုပ္ႏိုင္ပါတယ္။ Web based ျဖစ္တာေၾကာင့္ Word ကိုေတာ့ Feature ေတြအကုန္ ယွဥ္လို႕ မရေပမယ့္ အသုံးဝင္တဲ့ Editor တစ္ခု ျဖစ္တာကေတာ့ အေသအခ်ာပါပဲ ခင္ဗ်ာ။ CKEditor ဆိုတာကေတာ့ 2003 ေလာက္ထဲက စတင္ခဲ့တဲ့ FCKEditor ရဲ႕ new generation အျဖစ္ 2009 မွာေျပာင္းလဲ ခဲ့တာျဖစ္ပါတယ္။

ဒါေၾကာင့္ ခုခ်ိန္မွာ Google မွာ FCKEditor လို႔ရွာရင္လဲ CKEditor ကိုပဲ ေတြ႔ရပါ လိမ့္မယ္။

အဲဒီ အသုံးဝင္တဲ့ကို CI ထဲကို ဘယ္လို ေပါင္းထည့္ မလဲ ဆိုတာ ဆက္ၾကည့္ ရေအာင္။

1) Download CKEditor

ဒီမွာ CKEditor ကို အရင္ Download လုပ္ဖို႔ လိုပါတယ္။

2) Create folder in CI for CKEditor

Download လုပ္ထားတဲ့ Folder ကိုဖြင့္ လိုက္လို႕  _samples ၊  _sources folder ေတြကိုေတြ႔ ခဲ့ရင္ေတာ့  အဲဒီ Folder ေတြ မရွိလည္း အလုပ္လုပ္တာမို႔ Delete လုပ္ျပစ္ရင္ ပိုေကာင္းပါတယ္။ မလုပ္လဲ ျပသနာ မရွိေသာ္လည္း server ေပၚအတင္အခ်ေတြမွာ အခ်ိန္ၾကာတာေတြ ျဖစ္ေစႏိုင္ပါတယ္။ ျပီးရင္ေတာ့ js folder ကို system folder နဲ႔ level တူေဆာက္ေပးျပီး အဲဒီေအာက္မွာ ckeditor ဆိုျပီး ေနာက္ folder တစ္ခုထပ္ေဆာက္ျပီး ခုနက _samples ၊  _sources folder ေတြ Delete လုပ္ျပီး က်န္ေနတဲ့ files ေတြ၊ folders ေတြ အားလုံးကို အဲဒီ ေအာက္မွာ copy ကူးျပီး ထည့္လိုက္ပါ။ Folder Structure ကိုၾကည့္မယ္ ဆိုရင္ေတာ့



3) Add CKEditor Helper in CI

ဒီအဆင့္မွာ helper file ေရးရမွာ ျဖစ္လို႕ application/helpers ေအာက္မွာ file အသစ္ တစ္ခု ေရးျပီးထည့္ ေပးရပါမယ္။ ဒီမွာ ေရးျပေနရင္ အရွည္ၾကီး ျဖစ္သြားမွာ စိုးတာေၾကာင့္ ဒီေနရာမွာ Download လုပ္ျပီး application/helpers ကိုထည့္ေပး လိုက္ပါ။ File name ကိုေတာ့ ckeditor_helper.php လို႔ေပး လိုက္ပါ။ အဲဒီ file ကို ဖြင့္ၾကည့္မယ္ ဆိုရင္ CKEditor ကိုသုံးရန္ လိုအပ္တဲ့ Function ေတြကို စုေရးထားတာ ေတြ႔ရပါမယ္။ Author ေရးထားတဲ့ အတိုင္းသုံးရတာ အဆင္ေျပေနတဲ့ အတြက္ျပင္ေနဖို႔ မလိုပါ။

4) Create Controller

ကြၽန္ေတာ္ထည့္လိုက္တဲ့ helper ကိုျပန္ေခၚဖို႔ Controller ကိုဆက္ လုပ္ပါမယ္။ ckeditor.php လို႔ နာမည္ေပးျပီး ဒီ code ေလးကိုထည့္ ေပးလိုက္ပါ။

<?php

class Ckeditor extends Controller {
   
    public $data     =     array();
   
    public function __construct() {
       
        parent::Controller();

        $this->load->helper('url'); //You should autoload this one ;)
        $this->load->helper('ckeditor');
       
        //Ckeditor's configuration
        $this->data['ckeditor'] = array(
       
            //ID of the textarea that will be replaced
            'id'     =>     'content',
            'path'    =>    'js/ckeditor',
       
            //Optionnal values
            'config' => array(
                'toolbar'     =>     "Full",     //Using the Full toolbar
                'width'     =>     "550px",    //Setting a custom width
                'height'     =>     '100px',    //Setting a custom height
                   
            ),
       
            //Replacing styles from the "Styles tool"
            'styles' => array(
           
                //Creating a new style named "style 1"
                'style 1' => array (
                    'name'         =>     'Blue Title',
                    'element'     =>     'h2',
                    'styles' => array(
                        'color'             =>     'Blue',
                        'font-weight'         =>     'bold'
                    )
                ),
               
                //Creating a new style named "style 2"
                'style 2' => array (
                    'name'         =>     'Red Title',
                    'element'     =>     'h2',
                    'styles' => array(
                        'color'             =>     'Red',
                        'font-weight'         =>     'bold',
                        'text-decoration'    =>     'underline'
                    )
                )               
            )
        );
       

    }
   
    public function index() {
       
        $this->load->view('ckeditor', $this->data);
       
    }
}



$this->load->helper('ckeditor'); ကေတာ့ ခုနလုပ္ထားတဲ့ helper file ကိုျပန္ေခၚတာ ပါတယ္။
CKEditor အတြက္လိုအပ္တဲ့ configuration, styles စတာေတြကို လိုခ်င္တဲ့ အတိုင္းေရးျပီး $this->data['ckeditor'] ဆိုျပီး array ထဲကို ထည့္လိုက္ပါတယ္။

id - CKEditor ရဲ႕ ID ပါ။
path - CKEditor Source ေတြကို Download ခ်ျပီး js folder ေအာက္ထားခဲ့တာ မွတ္မိမယ္ ထင္ပါတယ္။ အဲဒီ path ကိုေျပာတာပါ။
config - ဒီမွာေတာ့ toolbar, width, height စတာေတြကို လိုအပ္သလို parameter ေပးႏိုင္ပါတယ္။
style - CKEditor မွာ style ဆိုတဲ့ dropdown ေလးရွိေနျပီး ကိုယ့္ေရးထားျပီးသားကို select လုပ္ျပီး style dropdown ထဲကေန style တစ္ခုခုကိုေရြးတာနဲ႔ auto ေျပာင္းသြားပါမယ္။ အဲဒီ dropdown ထဲမွာေပၚမယ္ style ေတြအတြက္ ေရးထားျခင္း ျဖစ္ပါတယ္။

အားလုံးျပီးသြားေတာ့ view ကိုေခၚရင္း အဲဒီ data array ကိုထည့္ေပး လိုက္ပါတယ္။
$this->load->view('ckeditor', $this->data);

5) Create View

ေနာက္ဆုံး အဆင့္အေနနဲ႔ ckeditor.php ဆိုတဲ့ view ကိုေဆာက္ျပီး ဒီ code ေလးထည့္ေပး လိုက္ပါ။ သုံးသြားတဲ့ display_ckeditor function ကေန ckeditor_helper.php ထဲမွာ ေရးထားျပီးသားကို ေခၚလိုက္တာ ျဖစ္ပါတယ္။

ဒါကေတာ့ Googling လုပ္ရင္း ရွာေတြ႔တာ ေလးကို ျပန္ေရး ထားတာ ျဖစ္လို႕ တျခားနည္း တစ္ခုခုနဲ႔ CI ထဲေပါင္းထည့္တာ ရွိရင္လည္း comment မွာေရးထား ခဲ့ႏိုင္ပါတယ္။

 

Ref: http://kromack.com/developpement-php/codeigniter/ckeditor-helper-for-codeigniter/


 

Extend in CodeIgniter Framework

Saturday, 28 August 2010 22:23

ဒီ Post မွာ အဓိကထားျပီး ေရးမွာ ကေတာ့ CodeIgniter Framework ေပၚမွာ ရွိေနျပီးသား Library, Helper ေတြနဲ႔ မလုံေလာက္ ရင္ ျဖစ္ေစ၊ ကိုယ့္ Project Requirement အရ ေရးထည့္ဖို႔ လိုလာရင္ ျဖစ္ေစ Library, Helper အသစ္ေတြကို ဘယ္လိုထည့္ မလဲ ဆိုတာကို ျဖစ္ပါတယ္။ တစ္ နည္း အားျဖင့္ CI Framework ကိုဘယ္လို Extend လုပ္မလဲ ဆိုတာလဲ ျဖစ္ပါတယ္။ ကြၽန္ေတာ္ ေရးထားျပီးသား CI Post ေတြကို ျပန္ဖတ္ခ်င္ရင္ေတာ့

  1. CodeIgniter and MVC for Beginners
  2. Structure and Configuration of CodeIgniter
  3. CodeIgniter Database Library
  4. CodeIgniter Form Helper
  5. Simple Data Entry in CodeIgniter
  6. Pagination with CodeIgniter
  7. File upload in CodeIgniter


How to add own Helper

Okay အရင္ဆုံး helper တစ္ခုဘယ္လို ေပါင္းထည့္ မလဲ ဆိုတာေလး ၾကည့္ရေအာင္။ CI Structure မွာ application နဲ႔ system ရွိေနျပီး Built-In Library ေတြ၊ Helper ေတြအားလုံးက system ေအာက္မွာပဲ ရွိတယ္ဗ်ာ။ ကိုယ့္ အသစ္ထည့္ ခ်င္တာကို အဲဒီ Folder ေတြေအာက္မွာ သြားမထည့္ ရပါဘူး။ ဒါဆိုဘယ္မွာ ထည့္ရမလဲ ဆိုေတာ့ ... application folder ေအာက္မွာလည္း helpers, libraries folder ေတြရွိေနပါတယ္။ အဲဒီမွာ ထည့္ေပးရပါမယ္။

 

ကြၽန္ေတာ္ကေတာ့ helpers folder ေအာက္မွာ calculation_helper.php  ဆိုျပီး php file အသစ္တစ္ခု ေဆာက္လိုက္တယ္။ အဲဒီမွာ  မွတ္သား စရာ တစ္ခုကေတာ့ file name မွာ ေနာက္က _helper ဆိုတာပါရပါမယ္။ ဆိုလိုတာကဗ်ာ abc_hepler.php, xyz_helper.php စသည္ျဖင့္ ေရွ႕က ျဖစ္ခ်င္တာ ျဖစ္ျပီး ေနာက္က _helper.php နဲ႔ ဆုံးေပးရမယ္။ ျပီးေတာ့ ေရးေနၾကအတိုင္းပဲ function ေရးလို႕ ရပါတယ္။ ကြၽန္ေတာ္ကေတာ့ function add_value ကိုေရးထားလိုက္ပါတယ္။ 

<?php

function add_value($val1,$val2){
    return $val1 + $val2;
}

 

အဲဒီ Code ကို Controller ထဲက ေခၚ ၾကည့္ဖို႔ controller တစ္ခုကို ဆက္ျပီးေဆာက္မယ္။ 

<?php


class Test extends Controller{


    function calculate(){
        
      

     $this->load->helper('calculation');
        
      

     $val1 = 5; 
      

     $val2 = 3;
      

     echo "Result is " . add_value($val1,$val2);
  

    }
}

test.php ဆိုျပီး နာမည္ေပး လိုက္မယ္ဗ်ာ။ ဒါက ခုန ကြၽန္ေတာ္ေရးထားတဲ့ Helper calculation ကို loading လုပ္တာပါ။

$this->load->helper('calculation');


ကြၽန္ေတာ္ လုပ္ထားတာ calculation_helper ျဖစ္ေပမယ့္ ျပန္ေခၚေတာ့ _helper မပါေတာ့ပါဘူး။ တကယ္လို႕ ကိုယ့္က abc_helper.php ကိုလုပ္ထားခဲ့ မယ္ဆိုရင္ေတာ့ $this->load->helper('abc'); ေပါ့ဗ်ာ။ Browser ကေန ဒီလို Run လိုက္မယ္ ဆိုရင္ေတာ့

http://localhost:8888/ci_extend/index.php/test/calculate/

ႏွစ္ခု ေပါင္းထားတဲ့ result ကိုျမင္ရဖို႔ သုံးသြားတဲ့ add_value ဆိုတာကေတာ့ calculation_helper.php ထဲက ေခၚသြားတာ ျဖစ္ပါတယ္။
ဒါဆိုရင္ေတာ့ CI မွာ Helper အသစ္တစ္ခု ဘယ္လိုထည့္ မယ္ဆိုတာ Idea အၾကမ္းေတာ့ ရမယ္ ထင္ပါတယ္။
:)

 

How to add own Library

Library အသစ္တစ္ခု ဘယ္လိုေပါင္းထည့္ မလဲ ဆိုတာကို ဆက္ျပီး ၾကည့္လိုက္ရေအာင္။ application ေအာက္က libraries folder မွာ ကိုယ္ထည့္ ခ်င္တဲ့ library အသစ္တစ္ခုကို ထည့္ေပးရမယ္။ ကြၽန္ေတာ္က hello ဆိုတဲ့ Library ကိုထည့္ ဖို႔ ဒီလိုေလး ေရးလိုက္တယ္ဗ်ာ။ file name ကေတာ့ hello.php ေပါ့ဗ်ာ။

<?php
class hello{

function message(){
    echo "How are you?";
}
    
}
?>



အဲဒီမွာ class hello ဆိုရင္ ရပါျပီ။ controllers ေတြ model ေတြေရးတုန္းကလို extends လုပ္စရာမလိုပါ။ ကြၽန္ေတာ္ေရးထားတဲ့ message ကို controller ကေန သုံးၾကည့္ဖို႔  ခုနကြၽန္ေတာ္ ေရးထားျပီးသား controller file ျဖစ္တဲ့ test.php မွာ ဒီ code ေလးကို ထပ္ေပါင္းလိုက္ရေအာင္။

function hello_lib(){
    $this->load->library('hello');
    $this->hello->message();
}



ျပီးရင္ေတာ့ Browser ကေန ဒီလိုေလး run ၾကည့္မယ္ ဆိုရင္ေတာ့ 
http://localhost:8888/ci_extend/index.php/test/hello_lib/
"How are you?" ဆိုတဲ့ message ကိုျမင္ရမွာ ျဖစ္ပါတယ္။

How to use resource from existing Libraries

အဲဒီ ကိုယ္ထည့္လိုက္တဲ့ Library မွာ ရွိေနျပီးသား library ေတြထဲက value ေတြ function ေတြကို ျပန္သုံးခ်င္ရင္ေရာ ဘယ္လို လုပ္မလဲ ဆိုတာေလးကို ၾကည့္ရေအာင္။ ခုန ေရးထားတဲ့ Library ထဲမွာ ဒီ code ေလးကိုထည့္ ၾကည့္မယ္ေနာ္။  
 

function message(){
        echo "How are you?";
        $this->load->library->('config');
}



အရင္ အရင္ ေတြတုန္းက controllers, models ေတြထဲကေန အဲလို သုံးရင္ error မတတ္ေပမယ့္ ခုခ်ိန္မွာေတာ့ ဒီလို error တတ္မယ္ဗ်ာ။

ဘာလိုလဲ ဆိုေတာ့ $this က point လုပ္ေနတာက hello class ကိုျဖစ္ေနလိုပဲ ျဖစ္ပါတယ္။ ဒီေတာ့ super class object ကိုျပန္ယူေပးဖို႔  လိုပါတယ္။ အဲဒါေၾကာင့္ Library ကို ဒီလိုေလး ျပင္လိုက္မယ္။

<?php
class hello{
    
    var $CI;
    
    function hello(){
        $this->CI = & get_instance();
        
    }
    
    function message(){
        echo "How are you?";
        $this->CI->load->library('config');
        echo "My Languages is " . $this->CI->config->item('language');
        //$this->load->library->('config');
    }
}
?>



Class ထဲမွာ သုံးဖို႔ CI ဆိုတဲ့ variable ထဲကို $this->CI = & get_instance(); ဆိုျပီး super object ကို Reference(&) ယူလိုက္ပါတယ္။ အဲဒါေၾကာင့္ ဒီေနရာေတြမွာ $this->CI->load->library('config'); လို႕ျပန္သုံးတဲ့ အခါ error မတတ္ေတာ့ပဲ သုံးလို႕ ရသြား တာ ျဖစ္ပါတယ္။ ေနာက္ Post  မွာ CKEditor Library ကိုဘယ္လို ေပါင္းထည့္  မလဲ ဆိုတာ ေရးပါမယ္။ ဖတ္ျပီး နားမလည္တာ မွားေနတာ မရွင္းတာေတြ ရွိေနရင္လည္း comment ေပးေဆြးေႏြး ေပးဖို႔ ေတာင္းဆိုပါတယ္။ :)

 

Ref: http://net.tutsplus.com/tutorials/php/codeigniter-from-scratch-extending-the-framework/

 

File upload in CodeIgniter

Saturday, 14 August 2010 21:50

ဒီ Post မွာေတာ့ CI နဲ႔ File/Image Upload တင္တဲ့ အေၾကာင္းကို ေရးမွာ ျဖစ္ပါတယ္။ ကြၽန္ေတာ္ ေရးထားျပီးတဲ့ CI Post

အားလုံးကို ျပန္ဖတ္ခ်င္တယ္ ဆိုရင္ေတာ့

  1. CodeIgniter and MVC for Beginners
  2. Structure and Configuration of CodeIgniter
  3. CodeIgniter Database Library
  4. CodeIgniter Form Helper
  5. Simple Data Entry in CodeIgniter
  6. Pagination with CodeIgniter

 

PHP မွာ File Upload လုပ္တဲ့ concept ကိုေတာ့ သိထားဖို႔  လိုပါတယ္ ခင္ဗ်ာ။ ဒီေတာ့ w3schools ရဲ႕ ဒီ  Article ေလးမွာ ျပန္ၾကည့္ ႏိုင္ပါတယ္။

http://www.w3schools.com/PHP/php_file_upload.asp

အဲဒါကို CI သုံးျပီး ဘယ္လို ျပန္ေရးမလဲ ၾကည့္ရေအာင္ :)

1) Change in config.php and autoload.php

config.php မွာ သင့္ေတာ္သလို ျပင္ေပးျပီး autoload.php မွာလဲ $autoload['helper'] ထဲကို file,form,url ေတြထည့္ ေပးရမယ္။


2) Create View
fileupload_view.php

<html>
    <head>
        <title>FIle Upload Tutorial By CodeIgniter</title>
    </head>
    <body>
        <?
        echo form_open_multipart('fileupload');
        echo form_upload('userfile');
        echo form_submit('upload', 'Upload');
        echo form_close();
        ?>
    </body>
</html>


ဒါကေတာ့ Form helper ကိုသုံးျပီး view တစ္ခုကို create လုပ္တာ ျဖစ္ပါတယ္။ ကြၽန္ေတာ္ Form helper ကိုေရးတုန္းက

ရွင္းထားျပီး ျဖစ္လို႔ usage ေတြကို ထပ္မရွင္းခ်င္ေတာ့ပါ။ ဒီမွာ ျပန္ဖတ္ၾကည့္ႏိုင္ပါတယ္။

3) Create Controller

View ကိုေခၚဖို႔ controller တစ္ခု လုပ္ပါမယ္။ fileupload.php လို႔ လုပ္လိုက္ပါမယ္။ Code ကေတာ့

<?php
class Fileupload extends Controller {
    function index() {
        $this->load->view('fileupload_view');
    }
}

ဒီအဆင့္ ျပီးလို႕ run ၾကည့္ရင္ေတာ့  ဒီလိုျမင္ရမယ္ဗ်ာ။


4) Create Model

file upload လုပ္တဲ့ Code ေတြအားလုံးကို model ထဲမွာမယ္လို႔ စိတ္ကူးလို႕ model တစ္ခုကို create လုပ္တာ ျဖစ္ပါတယ္။

တကယ္လို႕ controller ထဲမွာပဲ ဆက္ခ်င္ရင္လဲ ရတယ္ဗ်ာ။ application နဲ႔ level တူ "upload" folder နဲ႔ အဲဒီေအာက္မွာ

"thumbs" folder ကို create လုပ္ထားဖို႔ လိုပါတယ္။


fileupload_mod.php is here:

<?php
class Fileupload_mod extends Model {

   
    function Fileupload_mod() {
        parent::Model();
    }
   
    function do_fileupload() {
       
        $config['upload_path'] = './upload/';
        $config['allowed_types'] = 'gif|jpg|png';
        $this->load->library('upload', $config);
       
       
        if (!$this->upload->do_upload())
        {
            $error = array('error' => $this->upload->display_errors());
            echo $error;
        }   
        else
        {
            $image_data = $this->upload->data();
        }
       
        $config = array(
            'source_image' => $image_data['full_path'],
            'new_image' => './upload/thumbs',
            'maintain_ration' => true,
            'width' => 150,
            'height' => 100
        );
       
        $this->load->library('image_lib', $config);
        $this->image_lib->resize();
       
    }//do_fileupload

 }

အားလုံးျပီးသြားရင္ေတာ့ controller code ကိုဒီလိုေလး ျပန္ျပင္ ေပးလိုက္ရေအာင္။ model ကိုေခၚထားတဲ့ code ေတြရွိလို႕ ဒီလိုေလး ျပင္လိုက္မွ

perfect ျဖစ္သြားပါမယ္။

<?php
class Fileupload extends Controller {
    function index() {
        $this->load->model('Fileupload_mod');
        if ($this->input->post('upload')) {
            $this->Fileupload_mod->do_fileupload();
        }
        $this->load->view('fileupload_view');
    }
}

ဒါကေတာ့ upload library ကို loading လုပ္ဖို႔ လိုအပ္တဲ့ parameter ေတြကို $config ထဲကို ထည့္ပါတယ္။
ကိုယ့္ requirement အရ လိုအပ္တဲ့ preference variable ေတြကို ဒီဇယားမွာ ၾကည့္ႏိုင္ပါတယ္။

$this->upload->do_upload ဒါဆိုရင္ေတာ့ upload လုပ္သြားျပီဗ်ာ။ error တက္ရင္ေတာ့ error ကို ႐ိုက္ျပမယ္။ error မရွိရင္ေတာ့

thumbnail image တစ္ခုကို ဆက္ျပီး create လုပ္ၾကည့္မယ္။ မျဖစ္မေန လုပ္ရမွာေတာ့ မဟုတ္ပါဘူး။ တကယ္လို႕ ကိုယ့္က image

ကို thumbnail အတြက္ပါ လိုခ်င္တယ္ ဆိုရင္ေတာ့ ဒီလိုလုပ္ေပးရပါမယ္။

$this->load->library('image_lib', $config);
$this->image_lib->resize();

image_lib library ကို load လုပ္ဖို႔ $config variable ထဲကို လိုအပ္တာေတြကို တစ္ခါျပန္ျပီး ထည့္ပါတယ္။ thumbnail လုပ္ဖို႔လိုအပ္တဲ့

'source_image' => $image_data['full_path'], ခုန upload လုပ္ထားတဲ့ image ရဲ႕ path ကိုေျပာတာပါ။

'new_image' => './upload/thumbs',  thumb image ကိုထားမယ့္ ေနရာပါ။

'maintain_ration' => true, original image ရဲ႕ ratio ကိုဆက္ျပီး maintain လုပ္မလုပ္ပါ။

'width' => 150, thumb image ရဲ႕ width

'height' => 100 thumb image ရဲ႕ height

ဒါဆိုရင္ေတာ့ Thumb image ကိုပါ create လုပ္သြားျပီဗ်ာ။ upload နဲ႔ thumb folder ေတြမွာ upload လုပ္ထားတဲ့ image ေတြကို

ၾကည့္ႏိုင္ပါတယ္။ ကြၽန္ေတာ္ လုပ္ထားတဲ့ code ကိုဒီမွာ down ျပီးေတာ့ စမ္းၾကည့္ႏိုင္ပါတယ္။ အဆင္ေျပ ပါေစဗ်ာ။ 


 

 

Pagination with CodeIgniter

Saturday, 07 August 2010 23:46

ကြၽန္ေတာ္ CodeIgniter နဲ႔ Pagination Tutorial ေလးေရးျပပါမယ္။ ကြၽန္ေတာ္ CI နဲ႔ ပတ္သက္ျပီး ေရးထား သမ်ွကိုေတာ့

ဒီမွာ ျပန္ဖတ္ၾကည့္ ႏိုင္ပါတယ္။

  1. CodeIgniter and MVC for Beginners
  2. Structure and Configuration of CodeIgniter
  3. CodeIgniter Database Library
  4. CodeIgniter Form Helper
  5. Simple Data Entry in CodeIgniter

တစ္ရက္က comment တစ္ခုမွာလည္း CI Post အသစ္ေရးဖို႔ ေျပာထားတာ ေရးမယ္ေျပာျပီး မအားတာနဲ႔ ခုမွပဲ ေရး

လိုက္ပါတယ္။ OK စလိုက္ၾကရေအာင္။ Pagination ဆိုတာ သိျပီးသားလို႕ ယူဆလို႕ ဒီ Pagination Feature ကို CI မွာ

ရွိေနတဲ့ pagination library ကိုဘယ္လိုသုံးျပီး ဖန္တီးသလဲ ဆိုတာ ေလ့လာၾကည့္မယ္။

1) Setup 'Category' Tables

ဒီ category table script ကို run လိုက္ပါ။

CREATE TABLE IF NOT EXISTS `category` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `description` varchar(255) NOT NULL,
  `status` tinyint(1) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=18 ;


INSERT INTO `category` (`id`, `description`, `status`) VALUES
(1, 'Software Development', 1),
(2, 'Programmer', 1),
(3, 'Web Designer ', 1),
(5, 'Enginner', 1),
(6, 'AutoCAD', 1),
(7, 'Sale Person', 1),
(8, 'Driver', 1),
(9, 'IT', 1),
(10, 'Marketing', 1),
(11, 'Teaching', 1),
(13, 'Accounting1', 1);


2) Change to config.php and database.php

ကိုယ္ setting အတိုင္း config folder ေအာက္က config.php နဲ႔ database.php မွာ ေျပာင္းေပးဖို႔ လိုတယ္။


3) Create category controller

category.php

<?php

class Category extends Controller {
   
    function index()
    {
        $this->load->library('pagination');
        $this->load->library('table');
        $this->table->set_heading('ID', 'Description', 'Status');
       
        $config['base_url'] = 'http://localhost:8888/ci_pagination/index.php/category/index';   
        $config['total_rows'] = $this->db->get('category')->num_rows();
        $config['per_page'] = 5;

        $this->pagination->initialize($config);
        $data['records'] = $this->db->get('category', $config['per_page'], $this->uri->segment(3));
       
        $this->load->view('category_view', $data);
       
    }
}

ဒီ Code ကိုရွင္းရရင္ေတာ့ဗ်ာ။

$this->load->library('pagination');
$this->load->library('table');
$this->table->set_heading('ID', 'Description', 'Status');


CI pagination နဲ႔ table library ေတြကို loading လုပ္ျပီးေတာ့ table header column name ေတြကို သက္မွတ္ ပါတယ္။

Pagination လုပ္ဖို႔ အဓိက Parameter သံုးခုလိုတယ္ဗ်ာ။ ခု $config မွာအဲဒါေတြကို ထည့္ေပးပါတယ္။

$config['base_url']  - ဆိုတာကေတာ့ pagination ရွိေနတဲ့ controller ရဲ႕ function  ရွိေနတဲ့ full path ကိုထည့္ေပးရမယ္။

ကြၽန္ေတာ္ကေတာ့ category controller ထဲက index function ကိုေခၚထားထားပါတယ္။ Pagination မွာ page no ေတြကို

click တဲ့အခါ ဒီ base_url ကေန ျပန္စျပီ အလုပ္လုပ္မွာ ျဖစ္ပါတယ္။

$config['total_rows'] - ကေတာ့ table ထဲမွာ ရွိေနတဲ့ rows အားလုံး စုစုေပါင္းကို ေျပာတာ ျဖစ္ပါတယ္။ ဒီေနရာမွာ

database library  ထဲက get နဲ႔ num_rows ကို သုံးလိုက္ပါတယ္။

$config['per_page'] - ကေတာ့ page တစ္ခုမွာ ျပဖို႔ row အေရအတြက္ ျဖစ္ပါတယ္။

အားလုံး ထည့္ျပီးတဲ့ အခါမယ္ pagination ကို ဒီလို initialize လုပ္ပါတယ္။

$this->pagination->initialize($config);


ဒီအဆင့္ကေတာ့ page no ေတြကို click တဲ့ အခါ database ထဲက record ေတြကို သြားဖတ္တဲ့ အခါ ဒီ parameter ေလးေတြ

ကို ေပးဖို႔ လိုတယ္ဗ်ာ။


$data['records'] = $this->db->get('category', $config['per_page'], $this->uri->segment(3));

category - က table name ျဖစ္ပါတယ္။

$config['per_page'] - ကေတာ့ record ဘယ္ႏွခု ဖတ္မလဲ ဆိုတာ ျဖစ္ပါတယ္။

$this->uri->segment(3) - ကေတာ့ဗ်ာ number တစ္ခု ပါလာလိမ့္မယ္။ အဲဒီ number ကဘာကို ကိုယ္စားျပဳလဲ ဆိုရင္ေတာ့

ဘယ္ေနရာကေန စဖတ္မယ္ ဆိုတာျဖစ္ပါတယ္။ segment ကိုေတာ့ ကိုယ္ controller & function ေပၚမူတည္ျပီး

index.php  ေနာက္ကေန စျပီး သတ္မွတ္ ႏိုင္တယ္။  run ေနတဲ႔query ကို ဒီလိုျမင္ေနရင္ အဆင္ေျပသြားမယ္ဗ်ာ။

SELECT * FROM `category` limit start,end ျဖစ္ျပီး စတဲ့ start ေနရာကို ေျပာင္းသြားဖို႔ segment ယူျပီး ထည့္ေပးတာ

ျဖစ္ပါတယ္။  အိုေက ေနာက္ဆုံး view ကိုဒီလို ေခၚလိုက္ပါမယ္။

$this->load->view('category_view', $data);

view ထဲမွာ ဒီ ႏွစ္ေၾကာင္းကို ေရးေပး လိုက္တဲ့ အခါ

 

4) Create view

category_view.php (View folder ေအာက္မွာ create လုပ္ေပး ရမယ္။)

<h1>Pagination with CodeIgniter</h1>
<?php echo $this->table->generate($records); ?>
<?php echo $this->pagination->create_links(); ?>


ေအာက္ကပုံ အတိုင္း pagination လုပ္ထားျပီးသား table တစ္ခု ရပါလိမ့္မယ္။ 

CSS ေလးေတြကို သင့္ေတာ္သလို ျပင္ေပး လိုက္တဲ့ အခါ ဒီလိုေလး ျမင္ရပါမယ္။

ဖတ္ၾကည့္ျပီး စမ္ၾကည့္လို႕ မရရင္ comment ေပးျပီး ေဆြးေႏြးႏိုင္ပါတယ္။

 

CodeLobster IDE for CodeIgniter

Friday, 25 June 2010 23:56

ကြၽန္ေတာ္ Window ေပၚမွာ CodeIgniter ေရးဖို႔ IDE တစ္ခုျဖစ္တဲ့ CodeLobster နဲ႔ မိတ္ဆက္ေပးခ်င္တယ္။ ကြၽန္ေတာ္ CI ေရးေတာ့

auto completion၊ auto suggestion စတဲ့ features ေတြကိုလိုခ်င္မိပါတယ္ CI စေရးတုန္းက notepad++ နဲ႔ စေရးေပမယ့္ coding

အျမန္ေရး  ခ်င္လာတဲ့ အခါ IDE တစ္ခုလို လာပါတယ္။ notepad++ က CI syntax ေတြကို မသိဘူးဗ်ာ။ အဲဒီ  အတြက္ ကြၽန္ေတာ္

Notepad++ မွာ QuickText plug-in ကိုသုံးျပီး ေျဖရွင္း ၾကည့္ပါေသးတယ္။ CI syntax ေတြကို ကိုယ္လိုသလို႕ QuickText.ini မွာ

ထည့္ေပး ႏိုင္ေပမယ့္ ရွိသမ်ွ လိုက္ထည့္ေပးရမွာ မလြယ္ပါဘူး။ ဒါအျပင့္ QuickText on လိုက္ရင္ Notepad++ ရဲ႔ default tab ခုန္တာ

ေတြက အလုပ္ မလုပ္ေတာ့ျပန္ဘူး။ ကြၽန္ေတာ္ ေသခ်ာမလုပ္တတ္တာလည္း ျဖစ္ေကာင္းျဖစ္ႏိုင္ ပါတယ္။ ဒါေၾကာင့္ ေနာက္ အဆင္ေျပ

ႏိုင္မယ့္ IDE or text editor တစ္ခုကို အရွာအေဖြ ထြက္ခဲ့ ပါတယ္။ ဒီလိုနဲ႔ CodeLobster ဆိုတဲ့ IDE ကိုေတြ႔ ခဲ့ပါတယ္။ CodeIgniter

plug-in ကိုဝယ္ခိုင္းေနေတာ့ ကြၽန္ေတာ္ လက္ေလ်ာ့ျပီးခါမွ mmtuts ရဲ႔ QA မွာ တစ္ေယာက္က CodeLobster ကိုသုံးတယ္ေျပာေတာ့မွ

Try Again လုပ္ရင္း free serial key ကို request လုပ္ႏိုင္တယ္ဆိုတာ သိလိုက္ရတယ္။ Install လုပ္တာကေတာ့ ပုံမွန္ အတိုင္းေလးပဲ

လုပ္ေပးရုံပဲ။ Install ျပီးတဲ့ အခါ CodeLobster ကို run လိုက္ရင္ serial key request လုပ္ႏိုင္တာကို ေတြ႔ပါလိမ့္မယ္။ request လုပ္ျပီး

တဲ့ အခါ ကိုယ္ mail ထဲေရာက္လာတဲ့ user name နဲ႔ serial key ကိုထည့္ေပးလိုက္ရင္ ရပါျပီ။ အဲဒါဆိုရင္ေတာ့ CI ေရးဖို႔ IDE တစ္ခုရပါျပီ။

CodelobsterCodelobster

Coding ေရးတဲ့အခါ အဆင္ေျပပုံေလးကို နဲနဲ ဆက္ေရးခ်င္ပါတယ္။  model ေရးတဲ့ အခါ constructor ေရးဖို႔ ေအာက္ကလိုမ်ိဳး auto detect

သိေနတယ္။

constructor ေရးဖို႔ auto detect သိပုံconstructor ေရးဖို႔ auto detect သိပုံ

ေရးထားျပီးသား model ေတြကို ျပန္ေခၚတဲ့ အခါမွာလဲ အဆင္ေျပလွပါတယ္။
model ေတြကို auto detect သိပုံmodel ေတြကို auto detect သိပုံ

Database library ထဲက methods ေတြကို သုံးခ်င္အခါမွာလည္း auto suggestion လိုက္လုပ္ေပးတာကိုေတြ႔ႏိုင္ပါတယ္။

Database library ထဲက methods ေတြကို သိပုံDatabase library ထဲက methods ေတြကို သိပုံ Database library ထဲက methods ေတြကို သိပုံDatabase library ထဲက methods ေတြကို သိပုံ


ဒါေတြေၾကာင့္ ဒီ IDE ကို window ေပၚမွာ codeIgniter ေရးဖို႔ ညႊြန္းလိုက္ခ်င္ပါတယ္။ တကယ္လို႕ စာဖတ္သူ အေနနဲ႔လည္း အဆင္ေျပမယ့္

IDE ရွိခဲ့မယ္ ဆိုရင္ ျပန္ comment ေပးခဲ့ ႏိုင္ပါတယ္ ခင္ဗ်ာ။

 

Page 1 of 2

«StartPrev12NextEnd»

Login Form

Categories Table View

RSS Feed

JoomlaWatch Stats 1.2.9 by Matej Koval

Facebook Share

Share on facebook

mod_joomtouch

Version Iphone

Version Iphone by JoomTouch