Monthly Archives: กันยายน 2012
jQuery plugin แบบง่ายๆ
<!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title>basic jQuery plugin By Pitt Phunsanit</title> </head> <body> <ul id="demo"> </ul> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> <script> /* jQuery Plugin Template */ (function($){ /* $.fn.ชื่อ plugin */ $.fn.instead = function(options){ /* ค่า default ถ้าไม่ส่งมา */ var settings = $.extend({ id:"instead", background:"green" }, options); /* ทำงาน - อ้างถึง selected โดยใช้ this - ใช้ตัวแปรตามรูปแบบ settings.ตัวแปร */ this.append('<li id="'+settings.id+'" style="background:'+settings.background+'">id = '+settings.id+'</li>'); /* Maintaining Chainability */ return this; }; })( jQuery ); </script> <script> $(function(){ $('#demo') /* default */ .instead() /* ปรับแต่ง */ .instead({id:"customID" ,background:"yellow"}) .instead({id:"customID2" ,background:"red"}) /* test chainability */ .css('color', 'white'); }); </script> </body> </html>
เก็บ CodeIgniter log ด้วย hook
วิธีที่จะพัฒนาและดูแลเว็บคือทำระบบ log ที่จะบันทึกข้อมูลการใช้งาน ซึ่งจะบันทึกสิ่งที่เกิดขึ้นไว้สำหรับเข้ามาดูเหตุการณ์ที่เกิดขึ้นย้อนหลังได้ มีประโยชน์ในการดูแลระบบโดยเฉพาะตัวที่เป็น API ให้ระบบอื่นๆเรียกใช้ สำหรับ ci เราสามารถเขียนได้โดยการใช้ hook
ระบบ Hook (ตะขอ) คือ เป็นตะขอที่เกี่ยวกับเหตุการณ์ซักอย่างแล้วจึงทำงาน เหมือนกับ tricker ในดาต้าเบส หรือจะ อธิบายการทำงานง่ายๆ ก็เหมือน คุณไปที่ร้านอาหารคิดไม่ออกว่าจะทานอะไร ใช้วิธีรอให้เพื่อนสั่งแล้วบอกว่า หมีทู่ (me too.) นั้นละครับ
- ก่อนอื่น ไปเปิดการใช้งาน hook ก่อน เปิดไฟล์ /application /config/config.php แก้ $config[‘enable_hooks’] = FALSE; เป็น TRUE;
- เปิดไฟล์ /application /config/ hooks.php เพิ่มบรรทัด
$hook['post_system'][] = array( 'class' => '', 'function' => 'log_Profiling', 'filename' => 'log_Profiling.php', 'filepath' => 'hooks' );
- จากนั้นไปสร้างไฟล์ log_Profiling.php ใน /application/hooks เนื้อหาตามนี้ครับ
<?php function log_Profiling(){ global $CI ,$application_folder; $output = '<html><body>'; $output .= '<fieldset id="ci_profiler_benchmarks" style="border:1px solid #c00000;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee"> <legend style="color:#c00000;">OUTPUT</legend>'.$CI->output->get_output().'</fieldset>'; if( ! isset($_POST['profiler'])){ $CI->load->library('profiler'); if ( ! empty($CI->_profiler_sections)){ $CI->profiler->set_sections($this->_profiler_sections); } $output .= $CI->profiler->run(); } $output .= '</body></html>'; $fp = fopen($application_folder.'/logs/Profiling/'.date('Y-m-d-H-i-s-U').'_'.$CI->router->fetch_class().'_'.$CI->router->fetch_method().'.html' ,'w'); flock($fp ,LOCK_EX); fwrite($fp ,$output); flock($fp ,LOCK_UN); fclose($fp); }
- สร้างโฟลเดอร์ application/logs/Profiling/
ทุกครั้งที่มีการเรียกใช้งาน จะมีการเขียนไฟล์ลงใน application/logs/Profiling/ ตามวันและเวลา
ดูเพิ่มเติม