2011/03/30

[CodeIgniter] Form Post data 簡短寫法

codeigniter (ci) 當用form post data的時候要逐個input對應太麻煩了...

例子:
 $data = array(
       'baioti'   => $this->input->post('biaoti'),
        'neirong'     => $this->input->post('neirong'),
        'zuozhe' => $this->input->post('zuozhe'),
         'shijian'    => (string) date('Y-m-j'),
                        );
  $this->db->insert('news',$data);

轉換成新寫法:


$data =$_POST;
$data["shijian"]=(string) date('Y-m-j');
$this->db->insert('news',$data);


以上代碼因為字段太少,不會有很明顯的縮短,但是當你添加的內容需要很多字段的時候,可以明顯縮短許多許多!


當然這樣用的前提是,表單中的域要與數據庫中的字段要對應


有時當直接想用來Insert/update DB時, 會發現"submit" 都同時post去update了...而db沒有"submit "這column, 所以出現error, 這時只要在assign value前加上


unset($_POST['submit']);
$data =$_POST;
$data["shijian"]=(string) date('Y-m-j');
$this->db->insert('news',$data);


得左!

2011/03/28

讓webapp 似 native app 的幾個tag

1. home screen icon
iphone 3g/3gs: 57px X 57px
iphone 4: 114px X 114px
ipad: 96px X 96px
但其實正方形都ok~

<!-- 會加gloss effect -->
<link rel="apple-touch-icon" href="icon.png"/> 
<!-- 不會加gloss effect -->
<link rel="apple-touch-icon-precomposed" href="icon.png"/> 

2. 收起safari 的toolbars
<meta name="apple-mobile-web-app-capable" content="yes">

3. 加 start up image
一定是320px X 460px
<link rel="apple-touch-startup-image" href="startup.png">

EXTRA:
改最高那條status bar 的顏色
content 可以是 default, black, black-translucent
<meta name="apple-mobile-web-app-status-bar-style" content="black">

定義viewport
user-scalable=no <-- 不讓人放大或縮少你的webpage
<meta name = "viewport" content = "user-scalable=no, width=device-width">

reference link:
http://developer.apple.com/library/safari/#documentation/appleapplications/reference/safariwebcontent/ConfiguringWebApplications/ConfiguringWebApplications.html

2011/03/25

css webkit gradient radial

.gradient_radial{
    /*
    *第一個是type (有linear 同radial)
    *第二個是開始的位置, x 同y, 用%, 用space 分隔
    *第三個是第一個㘣的radius
    *第四個是結束的位置, x 同y, 用%, 用space 分隔
    *第五個是第二個㘣的radius
    *第六個是開始的color, 一定要有from
    *最後一個是結束的color, 一定要有to
    *
    *第六個同最後一個中間可以加 color-stop(位置, 顏色), 咁就可以做出幾隻色的gradient
    *
    *如果第一個圓的radius 大過第二個圓的radius, d 顏面就會掉轉 (由出至入)   
    */
    background: -webkit-gradient(radial, 70% 70%, 20, 50% 50%, 130,
    from(red),
    color-stop(16%, orange),
    color-stop(33%, yellow),
    color-stop(49%, green),
    color-stop(66%, blue),
    color-stop(83%, indigo),
    to(violet));
}

2011/03/22

CodeIgniter - split() 和eregi() is deprecated

可能是舊VERSION PHP 的關係

有時未必support split() 和eregi()的功能...

這時只要轉代取function 就可以了

split()  ->explode()

eregi() -> preg_match()

注意: preg_match() 的pattern 要加pattern "/word/i"

2011/03/21

[Magento] 更改language drop down menu

magento 最煩既一樣野就係你搵唔到個file出黎改....

當然你可以去admin config 個 show path hints, 但有時template 入面再有template, 就不免要人手入去搵

今日想改langauge 個drop down menu, 變左<a href=""....>既link

路徑幾經辛苦找到是base\default\template\page\switch\

language.phtml, 把select tag comment掉, 

我希望做到, 當store view是中文的時候, 顯示"eng" link ; 當英文的是時候, 顯示"中文"link

這時要了解一下magento 部份api function...

$this->getCurrentStoreId();    // (int) 取得store view的id (e.g. 中文→ 1 , 英文 →2)

$this->getStores() ;      //  (array) 取得所有store view的data  (包含id, name)

$_lang = $this->getStores() ;   //   先用variable裝住佢

$_lang->getId()              // 取得array中的store id

$_lang->getName()    // 取得array 中的store name

$_lang->getCurrentUrl()  //取得array中的url

基本上有齊以上function就能做大部份....

e.g.



<?php $curr_lang = $this->getCurrentStoreId();?>
<?php foreach ($this->getStores() as $_lang): ?>
<?php if ( $this->htmlEscape($_lang->getId())!= $curr_lang ) { ?>
        <a href="<?php echo $_lang->getCurrentUrl() ?>"><img
src="<?php echo $this->getSkinUrl('images/flag_' . $this->htmlEscape($_lang->getName()) . '.gif') ?>"
alt="<?php echo $this->htmlEscape($_lang->getName()) ?>"/></a>
<?php }?>
    <?php endforeach; ?>

2011/03/19

css webkit gradient linear

用css 去砌d 好靚的gradient
gradient
主要分linear同 radial
但暫時仲未統一
所以個個browser 的寫法都唔同

.gradient_linear{
    /*
    *第一個是type (有linear 同radial)
    *第二個是開始的位置, x 同y, 用space 分隔
    *第三個是結束的位置, x 同y, 用space 分隔
    *第四個是開始的color, 一定要有from
    *最後一個是結束的color, 一定要有to
    *
    *第四個同最後一個中間可以加 color-stop(位置, 顏色), 咁就可以做出幾隻色的gradient
    */

   /*圖一*/
   background: -webkit-gradient(linear, left top, left bottom,
   from(yellow),
   to(blue));

   /*圖二*/
    background: -webkit-gradient(linear, left top, right bottom,
    from(red),
    color-stop(16%, orange),
    color-stop(33%, yellow),
    color-stop(49%, green),
    color-stop(66%, blue),
    color-stop(83%, indigo),
    to(violet));
}

圖一

圖二

drop down menu

兩個幾好用的drop down menu

唔依賴framework 的~


tiny drop down
http://www.scriptiny.com/2008/11/drop-down-menu/

會有d fade in / fade out 的effect
但ie 6 睇會有d 奇怪

用法:
var menu=new menu.dd('menu');
menu.init('menu','menuhover');
(放在page 的最後, or onload 之後)
再用ul li set class


Professional dropdown #2
http://www.stunicholls.com/menu/pro_dropdown_2.html
唔駛用javascript, 用ul li set class 就ok 了

冇任何effect, 好直接, 好clean

2011/03/18

java write to file

點樣可以將d string output 做一個file 呢~?

其實好簡單的~

有幾個方法都得
咁我就用左其中一個la

//filePath: 目標file 的path e.g: /logs/example/output.txt
//str: 要寫落file 的text
public static void writeToFile(String filePath, String str){
    try {     
        // 開個file, 如果冇呢個file, 就會自動create
        FileOutputStream outFile = new FileOutputStream(filePath);

        // 寫string 落個file, 但要轉做bytes 先
        outFile.write(sb.toString().getBytes("UTF-8"));

         // 以防d bytes 未寫好, 要flush 一下
        outFile.flush();

         //  寫完後 close 個file =], 完
        outFile.close();

    } catch (IOException e){
        e.printStackTrace();
    }
}

2011/03/16

iphone4 detection 找出iphon4

一個用javascript 找出iphone 4 的方法
iphone 4 的mon 同其他iphone 主要唔同之處係個mon
因為個mon 的display 唔同, 所以寫website 的時候要用唔同的方法~~

  1. 先用user agent 找出iphone
  2. 再用webkit 獨有的javascript object  (devicePixelRatio) , 因為iphone 4 有 retina display, 所以device ratio 會是2, iphone 3 同iphone 3gs 是 1

vas isiPhone4 = false;
var userAgent = window.navigator.userAgent;
if (userAgent.toLowerCase().indexOf('iphone') != -1){
  // is iphone
  if (window.devicePixelRatio && window.devicePixelRatio == 2){
    // is iphone4
    isiPhone4 = true;
  }
}

useful websites

some testing browser website

HTML5 (可以知道唔同browsers support html5 的情況)


CSS3 (唔同browser 對css3 selector 的tester)
http://tools.css3.info/selectors-test/test.html

2011/03/14

(Drupal)Photoshop 根本轉唔到ICO...但可以裝PLUGIN

整完Drupal site, 最後發覺唔記得轉browser logo, 開一開ps 完來唔support

原來有plugin

ICO (Windows Icon) file format plugin for Photoshop

Copyright 2002-2011 Toby Thain, toby@telegraphics.com.au
The plugin gives Photoshop the ability to directly Open and Save Windows icon (.ICO) and cursor (.CUR) files (saving .CUR files is supported by version 2.1 or later)


http://www.telegraphics.com.au/svn/icoformat/trunk/dist/README.html


裝完再export 的時候, 就多了個選項了!

--------------------------------------------------------------
但轉icon 時, 若果就咁replace misc 入面既ico , 或者放root, 或者放theme, 係轉唔到icon既!

要入番 appearance -> [theme] - > shortcut icon setting 

2011/03/13

css3 border-radius

border-radius 係可以好簡單整到圓角邊

支援的browser: firefox, chrome, safari, ie9
ff4 以上先supoort 冇browser prefix
ie9 唔駛prefix的


.border_radius{
    border:10px solid #ffdd00;
    
    /*
    *4個角都是以20px 為radius 的圓角
    *(圖1)
    */
    border-radius: 20px; 
    -webkit-border-radius: 20px; 
    -moz-border-radius: 20px;    
    
    
    /*
    *左上角 同 右下角 是10px, 右上角 同 左下角 是40px
    *有-webkit-字頭會好奇怪, 要小心~!
    *(圖2)
    */
    border-radius: 10px 40px;
    -webkit-border-radius: 10px 40px;
    -moz-border-radius: 10px 40px;
    
    
    /*
    *左上角 是10px, 右上角 同 左下角 是20px, 右下角 是40px
    *(圖3)
    */
    border-radius: 10px 20px 40px;
    -webkit-border-radius: 10px 20px 40px;
    -moz-border-radius: 10px 20px 40px;
    

    /*
    *由左上角開始順時針的4個角
    *(圖4)
    */
    border-radius: 10px 20px 30px 40px;    
    -webkit-border-radius: 10px 20px 30px 40px;    
    -moz-border-radius: 10px 20px 30px 40px;    

    
    /*
    *border-*, -webkit-border-*, -moz-border-radius-* *: 指某一個角 
    *-moz- 的寫法有少少唔同, 要小心~!
    *(圖4)
    */
    border-top-left-radius:10px;
    border-top-right-radius:20px;
    border-bottom-right-radius:30px;
    border-bottom-left-radius:40px;    
    -webkit-border-top-left-radius:10px;
    -webkit-border-top-right-radius:20px;
    -webkit-border-bottom-right-radius:30px;
    -webkit-border-bottom-left-radius:40px;    
    -moz-border-radius-topleft:10px;
    -moz-border-radius-topright:20px;
    -moz-border-radius-bottomright:30px;
    -moz-border-radius-bottomleft:40px;
}

圖1

圖2
圖3


圖4

css3 box-shadow

box shadow 可以好易幫d box 打陰影
圖片都得, 但inset 就冇效果

支援的browser: firefox, chrome, safari, opera, ie9
ff4 以上先supoort 冇browser prefix

.box_shadow{
    box-shadow: 5px -5px 8px 3px #0000ff, -10px -10px 15px 5px #ff0000 inset;
    -webkit-box-shadow: 5px -5px 8px 3px #0000ff, -10px -10px 15px 5px #ff0000 inset;
    -moz-box-shadow: 5px -5px 8px 3px #0000ff, -10px -10px 15px 5px #ff0000 inset;
}

第1個 (5px): x 的偏離, 正數係右面, 負數係左面
第2個 (-5px): y 的偏離, 正數係下面, 負數係上面
第3個 (8px):  blur 的長度, 個影的邊邊幾長 (可有可無)
第4個 (3px): 影的大小, 每邊長多幾多px (可有可無)
第5個 (#0000ff): 影的顏色
第6個 (inset): 影是向外定向內 (ourset/ inset), default 係outset (可有可無) (向內的話唔計padding)

可以set多過一個shadow, 用',' 分隔

.box_shadow 的效果

2011/03/12

css3 text-shadow

text-shadow 係css3 的standard
唔駛加browser prefix 都用到 =]
支援的browser: firefox, chrome, safari, opera [no ie =.=]

.text_shadow{
    text-shadow: 5px -10px 20px #ff0000;
}

第1個 (5px): x 的偏離, 正數係右面, 負數係左面
第2個 (-10px): y 的偏離, 正數係下面, 負數係上面
第3個 (20px): blur 的長度 (越大越濛)
第4個 (#ff0000): 顏色

 可以有幾個shadow 的, 用 ',' 分隔
.text_shadow 的效果

2011/03/10

js change obj to param string

js 用會 {id:12, name:'user'} 來做data,
點可以好快咁變成 id=12&name=user 呢?
ajax call 時好好用的~!

var params = {id:12, name:'user'};

jquery:
$.param(params);
PS: space 會變做+


prototype:
Object.toQueryString(params);
PS: space 會變做%20


sql get all child cat by parent id

改少少就可以拎哂所有child
平時冇咩用, 但searching 好有用 =]

SELECT t1.* FROM category AS t1
LEFT JOIN category AS t2 ON t2.category_id= t1.parent
LEFT JOIN category AS t3 ON t3.category_id =t2.parent
WHERE 
(t1.category_id = [child_id] OR t2.category_id = [child_id] OR t3.category_id = [child_id])
 ORDER BY t1.LEVEL

js trim

how to trim a string using js?

function trim(str){
 return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}


function trim(str){
 return str.replace(/^\s+|\s+$/g, '');
}

str = trim(str);


jquery:
str = $.trim(str);


str =  str.strip();

2011/03/09

DRUPAL 7中能夠TRIM Teaser的地方

今日搵了很久, 為何在content type 的teaser選了trim length, 但frontend 仍然不見效果..

原來我除了在content type trim了,還在 views 裏的"field"  再trim了一次, 例如在content type set 了length 300: , 之後pass到view裏即使set 到trim 350也沒有效果, 因為一早被content type的override了!

當一個node 經panels, views ,block 顯示既時候, 真的要set好rule 搞清楚次序, 因為太多setting 可以互相override, 見一個set一次的話之後debug就頭痛了..

關於module 互相override的次序, 其實都可以check得番, 就係去database 睇"system"既table, 睇番你想睇既module, 留意weight, 通常會係0, views既default 係10 , 通常taxonamy, node 等等都會被view override 

但對於node .tpl file 同view 比較, 好似node .tpl file 永遠係最高prority, 這個有待確認

2011/03/08

sql ifnull

如果sql 要用concat 但又有null, 咁 ifnull 就好好用了
因為concat null 會成個value 變null 的

SELECT CONCAT(IFNULL(FIRSTNAME, ''), ' ', IFNULL(LASTNAME, '') AS FULLNAME FROM USER;

sql group_concat group by

今日先知原來有group_concat
會自動將那個column 的value 用 ',' group
完全救左我~!!

SELECT Class, GROUP_CONCAT(Animal) AS Animal FROM Zoo GROUP BY Family;

table
Class   Animal
===========
Fish   Goldfish
Fish   Grouper
Mammal Tiger
Mammal Lion


Result:
Fish   Goldfish,Grouper
Mammal  Tiger,Lion

"Update" SQL joining more than 1 tables

平時用CODE做, UPDATE SQL 都係好簡單, 但如果想做複雜少少既UPDATE, 其實亦可以COMBINE 埋 SELECT 一齊UPDATE , 但個SYNTAX有少少唔同

假設我select 左一堆node type係 "recipe" ,同profile_values 係"member"既row出黎

select * from node ,users ,profile_values 
where node.type='recipe' 
and users.uid = node.uid 
and profile_values.nid = node.nid
and profile_values.value='member'

之後我想將result 既某一個column (e.g. language )一次過update做新value
update node ,users ,profile_values 
set node.language ='sc_kit_cn' 
where node.type='recipe' and users.uid = node.uid 
and profile_values.nid = node.nid
and profile_values.value='member'

簡單講, 
就係將from 既table list 放去update後面
set 既column一定要簡番要update既table
where 果堆照keep番

2011/03/05

add element after an element

如何在某個element之後新增一個element呢~?
原來jQuery 同 Prototype 都各自有 method



jQuery--
$('#eleId').after('<b>Hello</b>');

Prototype--
$('eleId').insert({
after: '<b>Hello</b>'
});

2011/03/03

sql get parent cat by child id

原來呢個網有教



我再改一下


#get the path by child id, only one row,
SELECT t1.name, t2.name, t3.name
FROM category AS t1
LEFT JOIN category AS t2 ON t2.parent = t1.category_id
LEFT JOIN category AS t3 ON t3.parent = t2.category_id
WHERE (t3.category_id = [child_id])


#get the path by child id, row number = member of family
SELECT t1.* FROM category AS t1
LEFT JOIN category AS t2 ON t2.parent = t1.category_id
LEFT JOIN category AS t3 ON t3.parent = t2.category_id
WHERE 
(t1.category_id = [child_id] OR t2.category_id = [child_id] OR t3.category_id = [child_id])
 ORDER BY t1.LEVEL

2011/03/02

sql sub category

寫了一條sql 去拎sub category
有3層category, 用個parent id 去拎

寫左我好耐... ><

但sub select一定會慢... 要諗下有冇再好d寫先 


#get all child cat by parent id
SELECT ID FROM CATEGORY WHERE ID IN 
(SELECT ID FROM CATEGORY WHERE ID = [parent_id] OR PARENT_ID = [parent_id]) OR PARENT_ID IN
(SELECT ID FROM CATEGORY WHERE ID = [parent_id] OR PARENT_ID = [parent_id])

2011/03/01

css opacity


IE 總是不接受css 的standard
明明css2 已有opacity....

.opacity50{
  -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=50)";  /*IE8 or bove*/
  filter: alpha(opacity=50); /*IE 5.5 or above*/
  opacity:0.5; /*other browser*/
}

form submit, not reload

為左唔想form submit 之後reload 過那page
其中一個方法係set form 的target 做iframe 的id
<form action="form.asp" method="post" target="iframeId">

但呢個方法在ff 上有d 失寧
好似轉唔到個target 咁..

所以要再用javascript set 一次先得

-------------------------------------

var form;

if( document.loginForm ){ //IE
    form = document.loginForm;
} else { // FF
    form = document.getElementById('loginForm');
}

form.setAttribute('target','iframeId');
form.submit();

Google Maps EZ in Drupal - Fail to load

GMapEZ 是一個google map的第三方plugin
基本上google 本身也提供了api, 亦設了各種為non developer而設既embed方法, 我都唔明點解會有呢個gmapez的出現

講回正題, 由於此plugin 用落Drupal(上一手加既....), 並用了GMapEZ 的Drupal module, 即是第三方的第三方plugin..

打完latitude, 發覺show唔到map; 入去試下加番spacing, 今次show到, 但發覺show左個錯的位置 ; 開firebug 睇下, 同fbml撞左, 話有Error: GMapTypeControl is not defined ; 

我怕是module太舊, 即刻上官網dl番個新既js, 沒改變; 

多次嘗試後再入番我果頁 突然甚麼都不見了....

亂打亂撞之下用ie 睇下, 頂你, 睇得番, 仲show到正確位置;

即時入番ff clear左browser cache.... 再睇就正常了

總結: 解決方法竟然是 Clear browser Cache   .....




Editing Magento’s Top Links

It looks like in 1.4 they changed the wishlist.xml – just replace:
<action method="removeLinkByUrl"><url helper="wishlist/"/></action>
with:
<remove name="wishlist_link"/>
Because of the “remove” global effect, if you want to re-add the wishlist link somewhere else, you need to create a custom block. I just copy it from wishlist.xml, change the name attribute of the block, and place it in my local.xml file.
<reference name="WHEREVER">
    <block type="wishlist/links" name="wishlist_link_custom">
        <action method="addWishlistLink"></action>
    </block>
</reference>