Working with checkbox

Tôi thường hay dùng checkbox trong trường hợp list các record ra & cho phép chọn record tương ứng để xóa. Ngày trước, khi muốn lấy giá trị của các checkbox được chọn, tôi làm như sau:

1
2
3
4
5
6
7
8
9
if(is_array($_POST['checkbox_name'])) {
    $list_selected_id = '';
    foreach($_POST['checkbox_name'] as $key => $val) {
        $list_selected_id .= ','.$val;
    }
 
    $list_selected_id = substr($list_selected_id,1);
    //output will be: 1,2,3,...,n
}

Còn ngày nay, chỉ với 1 dòng đơn giản như sau:

1
$list_selected_id = implode(',', $_POST['checkbox_name']);

Hix, quá ngắn gọn, quá sáng sủa :(

Và thêm 1 điều nữa, tôi thấy Drupal (và các framework khác) thường dùng cách implode mảng để build các câu SQL. Quả thực khi áp dụng đúng lúc đúng chỗ thì phát huy tác dụng khỏi phải bàn cãi :D

[Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]
This entry was posted in PHP, Trick. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

CAPTCHA image
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>