Codeigniter
How to change CI's config value?
Saturday, 25 September 2010 20:51
ကြၽန္ေတာ္ ဒီ Post ကေတာ့ CI Tutorial တစ္ခုမဟုတ္ေပမယ့္ Coding ေရးရင္းေတြ႔တဲ့ အေၾကာင္းအရာေလး တစ္ခုကို ေျပာျပပါမယ္။ ဒီလိုပါ CI ရဲ႕ application\config\config.php မွာ configuration value ေတြသိမ္းတယ္ဆိုတာ အားလုံးသိၾကပါတယ္။ ဒါေပမယ့္ တစ္ခါတစ္ရံမွာ တစ္ခ်ိဳ႕ setting ေတြက True ျဖစ္ဖို႔ လိုအပ္သလိုေနေပမယ့္ တစ္ခ်ိဳ႕ Plugin ေတြသုံးလိုက္တဲ့ အခါ FALSE ျဖစ္မွ အလုပ္လုပ္ၾကတယ္။
ဥပမာ အေနနဲ႔ ကေတာ့
$config['enable_query_strings'] = FALSE; အဲဒီ value က Default အေနနဲ႔က FALSE ျဖစ္ပါတယ္။ Project Requirement အရ AJAX ကိုေပါင္းထည့္လိုက္ေတာ့ TRUE လို႕ျပင္ေပးမွ အလုပ္လုပ္တယ္ဗ်ာ။ ဒါေပမယ့္ အဲဒီလိုျပင္ေပးလိုက္တဲ့ အက်ိဳးသက္ေရာက္မႈက CI Pagination မွာသြားျဖစ္တာကိုေတြ႕ ရတယ္။ အဲ
ဒီေတာ့ လုပ္ႏိုင္တာက Pagination Code ေတြကို ျပင္ရင္ျပင္ မဟုတ္ရင္ေတာ့ configuration value ေတြကို လိုအပ္သလို ကစားေပးဖို႔ပဲ ျဖစ္ပါတယ္။ configuration value ေတြကို ျပင္ေပးတဲ့ နည္းကပိုလြယ္ျပီး အလုပ္႐ႈပ္ သက္သာေစပါတယ္။ ဒါေၾကာင့္ အဲဒီ value ေတြကို ျပင္ခ်င္တယ္၊ controller (or) views files ေတြထဲကေန override လုပ္ခ်င္တယ္ဆိုရင္ေတာ့ $this->config->set_item('enable_query_strings', 'FALSE');
//your code here
//after finish your code, you need to reset value for other stuff
$this->config->set_item('enable_query_strings', 'TRUE');
အဲလိုမ်ိဳးေလးေျပာင္း config value ေတြကို လိုအပ္ရင္ ေျပာင္းေပးလို႕ ရတယ္ဗ်ာ။ ဒီတစ္ပတ္ေတာ့ CI Post အသစ္ေရးႏိုင္မယ္ မထင္ေတာ့ဘူး။ ခုထိရုံး အလုပ္မျပီးလို႕လုပ္ေပးေနရတယ္။
ဒါေၾကာင့္ အသစ္ မေရး ဘူးလားလို႕ ေမးတဲ့သူေတြကို ကြၽန္ေတာ္ေတာင္းပန္ပါတယ္ဗ်ာ။
Experience ရွိလာရင္ MVC ကိုေျပာင္းသင့္ မေျပာင္းသင့္ ?
Saturday, 18 September 2010 19:32
ကြၽန္ေတာ္ ဒီ Post ကေတာ့ MVC ေတြကို ဘာေၾကာင့္ ေျပာင္းသုံးသင့္ သလဲ ဆိုတာကို Programmer ရဲ႕ ႐ႈေတာင့္ တစ္ခုထဲကေန ၾကည့္ျပီး ေျပာင္းသင့္ မေျပာင္းသင့္ ဆိုတာေလးကို ေရးမွာ ျဖစ္ပါတယ္။ ကြၽန္ေတာ္တို႔ Pure PHP Script ေတြေရးေနရာ ကေန MVC ကိုေျပာင္းခ်င္တယ္ ဆိုျပီး ေမးတဲ့ သူေတြကိုလဲ ဒီ POST ေလးကိုပဲ ဖတ္ေစခ်င္ပါတယ္။ က်န္တဲ့ Technical Point of View ေတြမပါပဲ Programmer Point of View ကေနပဲ ေရးထားတာေျဖစ္ပါတယ္။ ကြၽန္ေတာ္ CodeIgniter Post ေတြမစခင္တုန္းက MVC အေၾကာင္းေလး ဒီမွာေျပာ ထားတာေတာ့ ရွိပါတယ္။
ကြၽန္ေတာ္တို႔ PHP စျပီးေရးခါစက ဒါမ်ိဳး Coding ေလးေတြကို ေရးခဲ့ၾကတာ မွတ္မိမယ္ ထင္ပါတယ္။ <?php include_once “db.php”;
$sql = “select * from pages where status=’live’ and type=’home’ limit 1”;
$result = mysql_query($sql);
while($data = mysql_fetch_object($result)){
$title = $data->title;
$css = $data->css;
$bodycopy = $data->bodycopy;
$kw = $data->keywords;
$desc = $data->description;
} ?>
<html>
<head>
<title><?php echo $title; ?></title>
<link href=”<?php echo $css; ?>” rel=”stylesheet” type=”text/css”/>
<meta name=”keywords” value=”<?php echo $kw;?>”/>
<meta name=”description” value=”<?php echo $desc?>”/>
</head>
<body>
<?php include_once “topnav.php”;?>
<div id=”wrapper”>
<h1><?php echo $title;?></h1>
<p><?php echo nl2br($bodycopy);?></p>
</div>
<?php include_once “footer.php”;?>
</body>
</html>
အဲလိုေရးလို႕ မွားလား ဆိုရင္ မမွားပါဘူးဗ်ာ။ ဒါအျပင္ ကြၽန္ေတာ္တို႔ Project မွာ လိုအပ္တဲ့ CSS, JS, Images ေလးေတြ ထည့္ေပး လိုက္မယ္ ဆိုရင္ပဲ Running Stable ကိုေရာက္သြားျပီး Client ကိုျပမယ္။ Client ကလည္း သူလိုခ်င္တဲ့ Design ေလးနဲ႔ Functionality ေလးရမယ္ ဆိုရင္ Client လည္း ေပ်ာ္သည္ ေပ်ာ္သည္။ Programmer လည္း ေပ်ာ္သည္ ေပ်ာ္သည္ေပါ့။

အင္း..... ဒီလိုနဲ႔ပဲ ၆လ ေလာက္ၾကာေတာ့ Client က သူ႕ Business ေပၚမူတည္ျပီး Database ထဲမွာ Fields အသစ္ေလးေတြ ထည့္ခ်င္လာတယ္။ ျပီးေတာ့ Design ေလးကိုလည္း နဲနဲေလး ေျပာင္းခ်င္တယ္လို႕ ေျပာပါျပီ။ Programmer ကလည္း ဒီ Project ျပီးလို႕ ေနာက္ Projects ေတြဆက္လုပ္ ေနတုန္းClient request အရ သူ႕ Project ကိုျပန္ၾကည့္လိုက္ေတာ့ အဆင္မေျပတာေတြကို ေတြ႔ရပါျပီ။ Field တိုးလို႕ SQL ကိုေျပာင္းမယ္ ဆိုေတာ့ Design Stuff ေတြနဲ႔ ေရာေန၊ ဒီလိုပဲ Design ကိုေျပာင္းမယ္ ဆိုေတာ့လည္း Database accessing PHP Code ေတြနဲ႔ ေရာေနတာ ကိုေတြ႔ ရျပန္ေပါ့။
Programmer ကလည္း လက္ရွိ အလုပ္ေတြက တစ္မ်ိဳး ဒီ Project ကို enhancement လုပ္ရမွာ တစ္မ်ိဳးနဲ႔ စဥ္းစား ေခါင္း႐ႈပ္ေနတုန္း Client က ထပ္ေျပာပါျပီ။ Record မွားျဖတ္မိလို႕ Home Page မွာ Error တက္ေန ျပီးတဲ့ အျမန္ျပင္ေပးပါတဲ့။ ျပန္ၾကည့္ လိုက္ေတာ့ Database ထဲက ဖတ္တဲ့ ေနရာမွာ Error Handling လုပ္မ ထားလို႕ ျဖစ္တာကို သိလိုက္ပါတယ္။

Programmer လည္း ေခါင္းက်ိန္းေနျပီး Quick Fix နည္းလမ္းေလးေတြကို လိုက္ရွာရင္း ဒီလိုေလး ေျပာင္းဖို႔ စဥ္းစားလိုက္တယ္ဗ်ာ။ <?php <?php
function fetchHomePage(){
$sql = “select * from pages where status=’live’ and type=’home’ limit 1”;
$result = mysql_query($sql);
while($data = mysql_fetch_object($result)){
$hp[‘title’] = $data->title;
$hp[‘css’] = $data->css;
$hp[‘bodycopy’] = $data->bodycopy;
$hp[‘kw’] = $data->keywords;
$hp[‘desc’] = $data->description;
}
if (count($hp)){
return $hp;
}else{
$hp[‘title’] = “Welcome to our web site!”;
$hp[‘css’] = “default.css”;
$hp[‘bodycopy’] = “This is our web site!”;
$hp[‘kw’] = “welcome”;
$hp[‘desc’] = “our cool site”;
return $hp;
}
} ?>
include_once “db.php”;
include_once “functions.php”;
$home = fetchHomePage();
?>
<html>
<head>
<title><?php echo $home[‘title’]; ?></title>
<link href=”<?php echo $home[‘css’]; ?>” rel=”stylesheet” type=”text/css”/>
<meta name=”keywords” value=”<?php echo $home[‘kw’];?>”/>
<meta name=”description” value=”<?php echo $home[‘desc’]?>”/>
</head>
<body>
<?php include_once “topnav.php”;?>
<div id=”wrapper”>
<h1><?php echo $home[‘title’];?></h1>
<p><?php echo nl2br($home[‘bodycopy’]);?></p>
</div> <?php include_once “footer.php”;?>
</body>
</html>
ဒီ အဆင့္မွာ ဆိုရင္ေတာ့ Database Function ေတြအတြက္ function.php ကိုခြဲထုတ္ထား သလို Layout Design အရလည္း topnav.php နဲ႔ footer.php စသည္ျဖင့္ ပိုျပီး Flexible ျဖစ္ကာ အမွားခံႏိုင္ ေအာင္လည္း ျပင္လိုက္ပါျပီ။ Folder Structure ေလးေတြကိုလည္း ေသေသခ်ာလုပ္ေပး လိုက္မယ္ဆိုရင္ ဒီအဆင့္ဟာ တကယ္ကိုသပ္ရပ္တဲ့ Coding Standard တစ္ခုကို ရသြားမွာ အမွန္ပဲ ျဖစ္ပါတယ္။
![]()
ဒီလိုနဲ႔ Client လိုခ်င္တဲ့ အတိုင္းေလး ရသြားျပန္လို႕ Version Upgrade လုပ္ျပီး Site ကို launch လုပ္လိုက္ျပန္ေသာ္လည္း Programmer စဥ္းစားမိေနတာက ေနာက္တစ္ခါ requirement အသစ္ေတြနဲ႔ သူ႕ Site ကိုထပ္ျပင္ခ်င္တယ္ ဆိုရင္ ဘယ္လိုလုပ္ရမလဲ ဆိုတာကို ပဲျဖစ္ပါတယ္။ ဒီ Version ေလးကလည္း ေတာ္ေတာ္တန္တန္ ခရီးေရာက္လာခ်ိန္၊ Site ကို User ေတြကလည္း ဝင္လာ Site Traffic ကလည္းတတ္လာ ေနျပီးျဖစ္ေတာ့ Client ကလည္း User ေတြအသုံးဝင္မယ့္ တျခား Features ေတြကို စဥ္းစားမိလာျပန္တယ္။
ဒီတစ္ၾကိမ္ သူလုပ္ခ်င္တာေတြက ေျပာမယ္ဆိုရင္ Render Calendar View ေတြ, RSS Feedsေတြ၊Record အရမ္းမ်ားတဲ့ Page ေတြမွာ Pagination၊ URL ေတြကိုလည္း User Friendly ေျပာင္းခ်င္၊ languages ကိုလည္း ႏွစ္ခုေလာက္ထားခ်င္ စသည္ ... စသည္ျဖင့္ေပါ့။
Programmer ကလည္း အရင္ထဲက သူေတြးထားတဲ့ အတိုင္း Client ကေျပာင္းခ်င္ ျပန္ျပီဆိုေတာ့ Brain Storming ထပ္လုပ္ရျပီျပန္ျပီ။ Programmer ဆုပ္ကိုင္ထားတဲ့ ခံယူခ်က္က Where there's a will there's a way ေပါ့။

Time Line ကကပ္ေနေတာ့ Feature ေတြကို အစေနလဲမ ေရးခ်င္၊ သူမ်ား ေရးထားျပီးသား Coding ေတြကို ဟိုတစ္စ ဒီတစ္စ လိုက္ေပါင္းထည့္ရင္လဲ Coding Standard ကိုစဥ္းစား ရတဲ့ အျပင္ Security Issue ေတြကိုလည္း ထည့္တြက္ရပါတယ္။ Googling လုပ္ရင္း MVC Pattern Framework ေတြကိုေတြ႔ လာျပီး Client Requirements နဲ႔ ခ်င့္ခ်ိန္ျပီး သုံးသင့္မသုံးသင့္ကို စဥ္းစားေတာ့ Framework ေတြမွာ Client လိုခ်င္တဲ့ Feature ေတြကို support လုပ္တဲ့ Library ေတြ၊ helper ေတြပါေနပါတယ္။ Programmer လည္း Framework ေတြရဲ႕ tutorial ေတြကိုၾကည့္ Wiki ေတြကို overall ဖတ္ၾကည့္ျပီး လုပ္ႏိုင္မယ္ ဆိုတဲ့ confidence ရွိလာပါျပီ။ Project ၾကီးလာလို႕ Team နဲ႔ ဝိုင္းေရးရင္လည္း Coding Structure ကို အလြယ္တကူ ထိန္းလို႕လဲ ရႏိုင္ပါတယ္။ ဒါအျပင္ နာမည္ၾကီး Framework ေတြရဲ long term support နဲ႔ community ေတြကိုလည္း ရိပ္စားမိလာပါတယ္။ ေနာက္ဆုံးတာ့ Next Phrase Enhancement ေတြကိုTime Line အတိုင္း လုပ္ႏိုင္မယ္ ျဖစ္ေၾကာင္း Client ကို Reply ျပန္လိုက္ ပါေတာ့တယ္။
ကဲ .... ဒီေလာက္ဆိုရင္ေတာ့ Pure PHP ကေန experience ရွိလာျပီး ဆိုရင္ MVC ကိုေျပာင္းလဲ ေရးသင့္ မေရးသင့္ ဆိုတာကို Programmer Point of View ကေန ၾကည့္ျပီး တစ္စုံတစ္ခု ရသြားမယ္ ဆိုရင္း ေရးရၾကိဳးနပ္ပါျပီဗ်ာ။
(မွတ္ခ်က္။ ။ ကြၽန္ေတာ္ ေျပာေသာ MVC Framework ဆိုသည္မွာ CodeIgniter ကိုေျပာခ်င္တာ မဟုတ္ပါ။ ကိုယ့္နဲ႔ သင့္ေတာ္မွာကိုေရြးခ်ယ္ကာ သုံးၾကည့္ႏိုင္ပါတယ္။ ကြၽန္ေတာ္ အတြက္ေတာ့ CI ဟာ So Far So Good! ပါပဲ။ Framework ေတြရဲ႔ List ေတြကို ေအာက္မွာ ၾကည့္ႏိုင္ပါတယ္။ )

Sorting Tabular Data in CodeIgniter
Saturday, 11 September 2010 15:24
ကြၽန္ေတာ္ CI Project မွာ Table Column ေတြကို Click လိုက္ရင္ Sort လုပ္ေပးတဲ့ Sortable Feature ေလးကို ထည့္ခ်င္လို႕ လိုက္ရွာေနရင္း အဆင္ေျပခ်င္ေတာ့ Nettuts မွာ CI Series Day-16 ထြက္လာတယ္။ အဲဒီ Tutorial ေလးက ေတာ္ေတာ္ေလး ႐ိုးရွင္းျပီး အေထာက္အကူျပဳတာေၾကာင့္ ျပန္လည္မ်ွေဝ လိုက္ပါတယ္။ အရင္ဆုံး အေနနဲ႕ Pagination Concept ကိုေတာ့ သိထားဖို႔ လိုျပီး မသိေသးရင္လည္း ဒီမွာ ကြၽန္ေတာ္ေရးထားတာ ရွိပါတယ္။ ဒါေၾကာင့္ ဒီ Post မွာ Pagination အေၾကာင္းေတြကို ျပန္မရွင္းေတာ့ပဲ Sorting လုပ္တာကိုပဲ အဓိကထားေျပာပါမယ္။ ဒီ Post ကိုစမ္းၾကည့္ဖို႔ Dummy Data လိုအပ္တဲ့ Data ေတြကို ဒီမွာ Download ရယူႏိုင္ပါတယ္။ တကယ္လို႕မ်ား အဲဒီ link မွာအဆင္မေျပရင္ေတာ့ comment ေပးထားႏိုင္ပါတယ္။ အဲဒီ Dummy Data မရွိပဲ စမ္းလို႕ မရတာေၾကာင့္ ျဖစ္လို႕ အဲဒီ unzip လုပ္ျပီးတဲ့ အခါ sakila-data.sql နဲ႔ sakila-schema.sql ေတြကို sakila database ထဲကို ထည့္ေပးဖို႔ လိုတယ္ဗ်ာ။ အဲဒီ Database ထဲက တကယ္သုံးမွာကေတာ့ film_list ဆိုတဲ့ table တစ္ခုထဲပဲ ျဖစ္ပါတယ္။ Dummy Data ရျပီးရင္ေတာ့ Configuration ကိုေတာ့ သင့္ေတာ္သလို ျပင္လိုက္ပါ။ <?php $data['fields'] = array(
1) Creating Controller
ထုံးစံအတိုင္း file name ကိုေတာ့ film.php လို႕ေပးျပီး Film ဆိုတဲ့ Controller တစ္ခု အရင္ေဆာက္လိုက္ပါမယ္။ အဲဒီ Controller ထဲမွာ display ဆိုတဲ့ function ေလးတစ္ခုရွိမယ္ဗ်ာ။
class Films extends Controller {
function display($sort_by = 'title', $sort_order = 'asc', $offset = 0) {
$limit = 20;
$data['fields'] = array(
'FID' => 'ID',
'title' => 'Title',
'category' => 'Category',
'length' => 'Length',
'rating' => 'Rating',
'price' => 'Price'
);
$this->load->model('Film_model');
$results = $this->Film_model->search($limit, $offset, $sort_by, $sort_order);
$data['films'] = $results['rows'];
$data['num_results'] = $results['num_rows'];
// pagination
$this->load->library('pagination');
$config = array();
$config['base_url'] = site_url("index.php/films/display/$sort_by/$sort_order");
$config['total_rows'] = $data['num_results'];
$config['per_page'] = $limit;
$config['uri_segment'] = 5;
$this->pagination->initialize($config);
$data['pagination'] = $this->pagination->create_links();
$data['sort_by'] = $sort_by;
$data['sort_order'] = $sort_order;
$this->load->view('films', $data);
}
}
ဒီ Function ေလးကိုၾကည့္မယ္ ဆိုရင္ေတာ့ $sort_by, $sort_order, $offset တြကို pagination နဲ႔ sorting function အတြက္ parameter ယူထားတာေတြ႔ ရပါမယ္။
'FID' => 'ID',
'title' => 'Title',
'category' => 'Category',
'length' => 'Length',
'rating' => 'Rating',
'price' => 'Price'
);
ဒါကေတာ့ Table ထဲက Column ေတြနဲ႔ View မွာ User ကိုျပမယ့္ Column Name ေတြကို Mapping လုပ္တာ ျဖစ္ပါတယ္။ က်န္တဲ့ Code ေတြကေတာ့ Pagination အေၾကာင္းနဲ႔ေတြ ျဖစ္လို႕ Pagination တုန္းက ရွင္းျပထားျပီး ျဖစ္တာေၾကာင့္ ေက်ာ္လိုက္ပါမယ္။ Controller ထဲကေန Film_model ကိုေခၚထားတာေၾကာင့္ Model ကိုဆက္လိုက္ရေအာင္။ <?php
2) Creating Model
ကြၽန္ေတာ္ Film_model.php ဆိုတဲ့ Film_model ထဲမွာ search function ေလးရွိပါမယ္။
class Film_model extends Model {
function search($limit, $offset, $sort_by, $sort_order) {
// results query
$q = $this->db->select('FID, title, category, length, rating, price')
->from('film_list')
->limit($limit, $offset)
->order_by($sort_by, $sort_order);
$ret['rows'] = $q->get()->result();
// count query
$q = $this->db->select('COUNT(*) as count', FALSE)
->from('film_list');
$tmp = $q->get()->result();
$ret['num_rows'] = $tmp[0]->count;
return $ret;
}
}
$ret ထဲကို parameter ေတြအရ run လို႕ရလာတဲ့ Result Array ရယ္၊ Total Record Count ကိုထည္ျ့ပီး Return ျပန္ထားတာကို ေတြ႔ရပါမယ္။
Model မွာ သုံးသြားတဲ့ Database Library ထဲက Function ေတြကို မသိရင္ေတာ့ CodeIgniter Database Library ကိုျပန္ဖတ္ ေစလိုပါတယ္။ ဒါဆိုရင္ေတာ့ view အပိုင္းေလးကို ဆက္လိုက္ရေအာင္။
<!DOCTYPE HTML> <div> <?php foreach($films as $film): ?> <?php if (strlen($pagination)): ?>
3) Creating View
Controller ရဲ႕ ေနာက္ဆုံး Line မွာ $this->load->view('films', $data); ဆိုျပီး View ကိုေခၚထားလို႕ film.php ဆိုျပီး view တစ္ခုေဆာက္လိုက္ပါမယ္။
<html lang="en-US">
<head>
<title>Films</title>
<meta charset="UTF-8">
</head>
<body>
<div>
Found <?php echo $num_results; ?> films
</div>
<table>
<thead>
<th>ID</th>
<th>Title</th>
<th>Category</th>
<th>Length</th>
<th>Rating</th>
<th>Price</th>
</thead>
<tbody>
<?php foreach($films as $film): ?>
<tr>
<?php foreach($fields as $field_name => $field_display): ?>
<td>
<?php echo $film->$field_name; ?>
</td>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php if (strlen($pagination)): ?>
<div>
Pages: <?php echo $pagination; ?>
</div>
<?php endif; ?>
</body>
</html>
View Code ေလးကိုၾကည့္မယ္ ဆိုရင္ေတာ့ Total Records ေတြကိုျပဖို႔ပါ။
Found <?php echo $num_results; ?> films
</div>
<thead></thead> ႏွစ္ခုၾကားမွာ Model ထဲက ဆြဲထားတဲ့ Fields ေတြအတိုင္း Table Column Name ေတြကို ျပထားပါတယ္။
ဒီေနရာကေတာ့ Controller ထဲက Pass လုပ္လိုက္တဲ့ Data ေတြကို foreach သုံးျပီး ျပန္႐ိုက္ျပတာ ျဖစ္ပါတယ္။
<tr>
<?php foreach($fields as $field_name => $field_display): ?>
<td>
<?php echo $film->$field_name; ?>
</td>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
ေအာက္ဆုံးကေတာ့ Pagination ကိုျပဖို႔ပါ။
<div>
Pages: <?php echo $pagination; ?>
</div>
<?php endif; ?>
ဒီအဆင့္ထိျပီးလို႕ Browser မွာ Run ၾကည့္မယ္ ဆိုရင္ေတာ့ ဒီလိုေလးျမင္ ရပါလိမ့္မယ္။

Sortable Feature မထည့္ရေသးတာကို သတိျပဳမိမယ္လို႕ ထင္ပါတယ္။ ခုထိျပီးသြားတာကေတာ့ Data Retrieve လုပ္ျပီး Pagination နဲ႔ ျပန္ျပတဲ့ အပိုင္းပဲျဖစ္ပါတယ္။ Sortable ျဖစ္ဖို႔ Code ကိုအနည္းငယ္ ျပင္ဖို႔လိုပါတယ္။ $sort_order = ($sort_order == 'desc') ? 'desc' : 'asc';
4) Editing to existing model file
Model မွာ ပိုျပီး safe ျဖစ္သြားေအာင္ ဒီ code ၃ ေၾကာင္းကိုျဖည့္ ေပးလိုက္ပါမယ္။
$sort_columns = array('FID', 'title', 'category', 'length', 'rating', 'price');
$sort_by = (in_array($sort_by, $sort_columns)) ? $sort_by : 'title';
ျဖည့္လိုက္ရတဲ့ ရည္ရြယ္ခ်က္ကေတာ့ User က URL parameter ေတြကို ျပင္လိုက္ရင္ေတာင္ error မတတ္ေစဖို႔ ထည့္လိုက္တာ ျဖစ္ပါတယ္။ $sort_order ကေတာ့ string value တစ္ခုပဲ ျဖစ္တာေၾကာင့္ If-Else ပုံစံေလးမ်ိဳးနဲ႔ စစ္ႏိုင္ေပမယ့္ $sort_columns ကေတာ့ တစ္ခုထက္ပိုတာေၾကာင့္ array ပုံစံနဲ႔ in_array function သုံးျပီးစစ္ ပါမယ္။ ေနာက္ဆုံး ျဖစ္သြားမယ့္ Model Code ကေတာ့ <?php <style> <thead> <?php echo anchor("index.php/films/display/$field_name/" .
class Film_model extends Model {
function search($limit, $offset, $sort_by, $sort_order) {
$sort_order = ($sort_order == 'desc') ? 'desc' : 'asc';
$sort_columns = array('FID', 'title', 'category', 'length', 'rating', 'price');
$sort_by = (in_array($sort_by, $sort_columns)) ? $sort_by : 'title';
// results query
$q = $this->db->select('FID, title, category, length, rating, price')
->from('film_list')
->limit($limit, $offset)
->order_by($sort_by, $sort_order);
$ret['rows'] = $q->get()->result();
// count query
$q = $this->db->select('COUNT(*) as count', FALSE)
->from('film_list');
$tmp = $q->get()->result();
$ret['num_rows'] = $tmp[0]->count;
return $ret;
}
}
တဆက္ထဲပဲ View ကိုပါဆက္ျပင္ လိုက္ရေအာင္။
5) Editing to existing view file
ၾကည့္ေပ်ာ္႐ႈေပ်ာ္ ျဖစ္သြားေအာင္ ဒီ CSS ေလးကို အရင္ထည့္ လိုက္ပါမယ္။
* {
font-family: Arial;
font-size: 12px;
}
table {
border-collapse: collapse;
}
td, th {
border: 1px solid #666666;
padding: 4px;
}
div {
margin: 4px;
}
.sort_asc:after {
content: "▲";
}
.sort_desc:after {
content: "▼";
}
</style>
ျပီရင္ေတာ့ <thead></thead> ၾကားထဲမွာ ရွိေနတဲ့ Table Column ကို႐ိုက္ျပတဲ့ Code ေလးေတြကို နဲနဲ ျပင္လိုက္ပါမယ္။ ပထမ အဆင့္မွာ ႐ိုး႐ိုးရွင္းရွင္း ႐ိုက္ျပထားတဲ့ ေနရာေတြမွာ ဒီလိုေလး ျပင္လိုက္ပါမယ္။
<?php foreach($fields as $field_name => $field_display): ?>
<th <?php if ($sort_by == $field_name) echo "class=\"sort_$sort_order\"" ?>>
<?php echo anchor("index.php/films/display/$field_name/" .
(($sort_order == 'asc' && $sort_by == $field_name) ? 'desc' : 'asc') ,
$field_display); ?>
</th>
<?php endforeach; ?>
</thead>
foreach ကေတာ့ Controller ထဲမွာ Mapping လုပ္ထားတဲ့ array ထဲကေန User ကို Display ျပမယ့္ Column Name ေတြကို ႐ိုက္ျပတာပါ။
<th <?php if ($sort_by == $field_name) echo "class=\"sort_$sort_order\"" ?>> ကေတာ့ asc လား desc လားေပၚမူတည္ျပီး CSS ကို ေျပာင္းလဲ ေပးဖို႔ သုံးသြားတဲ့ Code ပဲျဖစ္ပါတယ္။ Table columns ေတြကို link ထည့္ရမွာ ျဖစ္လို႕ anchor ကိုသုံး link ထည့္ေပး လိုက္ပါမယ္။
(($sort_order == 'asc' && $sort_by == $field_name) ? 'desc' : 'asc') ,
$field_display);
?>
User က Column တစ္ခုကို ပထအၾကိမ္ Click ရင္ asc ေနာက္တခါ ထပ္ျပီး click မယ္ဆိုရင္ desc အျဖစ္ေျပာင္းလဲ ေပးဖို႔ Flip-Flop လုပ္ထားတဲ့ Code ပဲျဖစ္ပါတယ္။ ဒီတစ္ၾကိမ္ျပန္ Run ၾကည့္မယ္ ဆိုရင္ေတာ့ Sortable ပါျပီး ပိုၾကည့္ေကာင္းမယ့္ Table တစ္ခုကို ျမင္ရမွာ ျဖစ္ပါတယ္။

asp.net မွာ setting ထဲမွာ ျပင္ေပးလိုက္တာနဲ႔ auto generate ထြက္သြားမယ့္ Code ကို PHP မွာ ေရးေန ရပါ လာလို႔ ေတာ့စဥ္းစားမိပါတယ္။
ကြၽန္ေတာ္ အတတ္ႏိုင္ဆုံး ရွင္းေအာင္ ေရးထားေပမယ့္ ဖတ္ျပီးနားမလည္ရင္ေတာ့ comment ေလးေတြထားခဲ့ ႏိုင္ပါတယ္။ အမွန္အတိုင္းေျပာရရင္ ဒီ Post ကိုေရးရတာ သိပ္အားမရဘူးဗ်ာ။
Nettuts ရဲ႕ Article မွာေတာ့ Video ေလးပါတာေၾကာင့္ ဖတ္ျပီး မရွင္းရင္လဲ သူ႔ Video ေလးကို ဆက္ၾကည့္ ႏိုင္ပါတယ္။
Ref: http://net.tutsplus.com/tutorials/php/codeigniter-from-scratch-displaying-sorting-tabular-data/
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 ေတြကို ျပန္ဖတ္ခ်င္ရင္ေတာ့
-
CodeIgniter and MVC for Beginners
-
Structure and Configuration of CodeIgniter
-
CodeIgniter Database Library
-
CodeIgniter Form Helper
-
Simple Data Entry in CodeIgniter
-
Pagination with CodeIgniter
-
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/
More Articles...
Page 2 of 4
«StartPrev1234NextEnd»Login Form
Latest Post
Categories Table View
- Reader's Conner (133)
- PHP (48)
- Joomla CMS (46)
- Codeigniter (18)
- jQuery (12)
- iDhamma (11)
- Mobile Development (10)
- PHP & AJAX (4)
- Apache (3)
- For Mac (3)
- mySQL (2)
- DhammaDroid (1)