6 ตุลาคม 2556

ลง mongodb ยังไง

ลง Mongo DB ยังไง ?


logo mongdb

ทักทายนิดหน่อย …

หลายๆคนเริ่มหันมามองทางแก้ไขปัญหาเกี่ยวกับการการจัดการฐานข้อมูล เพราะว่าการที่ฐานข้อมูลส่วนใหญ่จะใช้ Mysql กันนั้นเริ่มมีเพดาน ของความสามารถของมัน จึงทำให้การส่งข้อมูลของเว็บที่มีคนเข้าเยอะๆมีปัญหา โหลดช้า โหลดแล้วค้าง ซึ่งวันนี้ผมจะมาพูดถึงเจ้าตัว Mongo Database หรือเรียกว่า mongodb ซึ่งเป็นฐานข้อมูลแบบรองรับจัดเต็มกับข้อมูลที่เริ่มขยายตัวแบบมากมายไม่สิ้นสุด ข้อดีมันก็เยอะ ข้อเสียมันก็พอๆกันขึ้นอยู่กับคุณ ว่าตัวจัดการนั้นเหมาะกับงานของคุณหรือไม่ ถ้าใครอยากรู้ข้อดีของเสียหาเอาในเน็ตเลยจ้า แต่ตอนนี้โครงการ Pantip3G ได้นำ Mongo DB มาใช้แล้วจึงอยากแชร์ประสบการณ์ซักเล็กน้อยแก่ผู้ที่สนใจอยู่

สิ่งที่ควรจะรู้ก่อนจะเริ่มลง Mongo DB

สิ่งที่เราต้องรู้ก่อนจะเริ่มลงนั้นคือ ในตัวอย่างของผมนั้นจะมีสเปกของ software server นั่นคือ wamp version 2.2 ซึ่งตอนนี้ทางเว็บ wamp นั้นมี 2.2 xxx ซึ่งใกล้เคียงกันอยู่แล้วใช้ได้โลด ซึ่งในนั้นจะมี apache 2.2.21 สำหรับของผมและ php version 5.3.8 ซึ่งมันสำคัญสำหรับการลง mongo db คุณจะต้องรู้รุ่นหรือ version ของ php เพราะว่าไม่อย่างนั้นจะไม่สามารถ download ตัว driver หรือว่าถ้าจะอธิบายให้เข้าใจคือ คู่มือสำหรับ PHP ที่มันจะทำให้ PHP เข้าใจ command ต่างๆของ mongo db เหมือนเวลาที่คุณใช้คำสั่งพวก Query ใน mysql นั่นแหละครับ เอาล่ะในการลงจะมีสองขั้นตอนคือ
  1. การทำให้ PHP นั้นเข้าใจ command ต่างๆของ Mongo DB โดยเป็นการ install extension นั่นเอง
  2. การเปิด server ของ mongo db ให้มันทำงานอารมณ์เหมือนเปิด database นั่นแหละครับไม่งั้นก็ connect ไม่ได้
เอาล่ะเรามาเริ่มกันเลยครับ …

การลง extension mongodb สำหรับ PHP

ขั้นแรกเข้าไปหน้าเว็บ www.mongodb.org ก่อนเลยครับแล้วเลือกหัวข้อเมนู Drivers ด้านขวามือดังรูปด้านล่าง
จากนั้นก็เลือก PHP ดังภาพด้านล่างครับ โดยถ้าคุณถนัดภาษาอะไรก็จัดไป อันนั้นเลยครับแต่ผมใช้ PHP เน้อ >_<
เสร็จแล้วจะไปอีกหน้าซึ่งจะแสดงวิธีการลงต่างๆโดยของเราในตัวอย่างจะลงของ windows
โดยเขาอธิบายว่าให้เราไป download ตัว driver ของ PHP ตามลิงค์ที่เขาบอกแต่ผมแนะนำให้ไป download PHP extension ที่นี่ดีกว่าเพราะว่ามันใหม่กว่าจ้า แล้วเขาก็อธิบายว่า VC6 คือสำหรับ server ที่เป็น apache ( VC9 สำหรับ IIS window ) thread safe คือการให้ PHP รันเสมือนเป็น apache module ซึ่งเป็นแบบที่แนะนำให้โหลด ส่วน non-thread safe ก็สำหรับพวก CGI ก็เลือกเลยอันที่เหมาะสมกับ server ของคุณ ซึ่งของผมนั้นจริงๆผมใช้ apache นะครับแต่ตอนลงไปแล้วนั้นมันก็มีข้อความให้ลงของ IIS ผมจึงต้องโหลดตัว VC9 ดังรูปด้านล่างครับ
เมื่อคุณ download มาแล้วทำการ unzip จะได้มี 2 ไฟล์แต่จริงๆเราใช้ไฟล์เดียวคือ php_mongo.dll ให้นำไฟล์นี้ไปวางไว้ในโฟรเดอร์ ext หรือที่สำหรับวางไฟล์พวก extension ของ PHP ครับ และทำการเปิดไฟล์ php.ini ซึ่งจะอยู่แล้วตามที่แต่ถ้าคุณหาไม่เจอละก็ให้ดูใน phpinfo() แล้วดูที่บรรทัดนี้ตามรูปครับ
แล้วไปแก้ไขโดยการเพิ่ม extension=php_mongo.dll โดยบรรทัดที่คุณจะวางคำสั่งนี้แนะนำให้ค้นหาคำว่า extension ครับจะได้รู้ที่เขาเขียนพวก extension ต่างๆไว้ก็เพิ่มเข้าไปอีกบรรทัดแล้วก็ save เสร็จแล้วก็ทำการ restart server ของเราให้มันรับการแก้ไขค่าใหม่ของเราเข้าไปก็เป็นอันเสร็จพิธีของการทำให้ PHP รู้จักคำสั่งๆของ mongo db แต่ยังไม่จบนะมีอีกเราต้องทำการรัน mongo db ในการจัดเก็บด้วยพร้อมแล้วไปต่อกันเลยครับ

ขั้นตอนการรัน Mongo DB

ให้คุณเข้าไปที่ link นี้เลยครับ mongodb download ซึ่งจะมีให้สำหรับทุก OS ที่มีการ support ก็เลือกอันที่เหมาะสมครับดังรูปด้านล่าง
เมื่อ download มาเสร็จ และเราทำการ unzip โดยตัวไฟล์จะมีชื่อคล้ายๆกับตัวอย่างแหละครับก็ให้ทำการ unzip ไว้ซักที่ถ้าเอาง่ายก็ใส่ไว้ตรง drive C เลยก็ได้ครับเสร็จแล้วเมื่อ unzip ก็ให้ทำการ rename โฟรเดอร์เพื่อง่ายต่อการเรียกเวลาเราพิมพ์ใน command line เป็น mongo ก็พอครับ
โดยของผมเปลี่ยนชื่อเป็น mongodb ก็คล้ายๆกันเสร็จแล้วให้คุณทำการสร้างโฟรเดอร์มา ใน drive C: ชื่อว่า data และในโฟรเดอร์ data ให้สร้างอีกโฟรเดอร์ชื่อ db ครับต่อจากตรงนี้ผมจะอธิบายไว้ก่อนว่าไฟล์ที่อยู่ใน mongo/bin นั้นจะมีสองไฟล์ที่สำคัญต่อเราเวลาจะใช้ mongo db คือ
  1. ไฟล์ชื่อ mongod.exe ซึ่งตัวนี้เมื่อเราทำการสั่งด้วย command จะเป็นเหมือนการเปิด database ครับ
  2. ไฟล์ชื่อ mongo.exe ตัวนี้จะเป็นเหมือน admin shell ครับ
เราก็มาลองรัน mongodb ของเราซักทีกด Ctrl+r หรือเข้าไปที่ start > run แล้วพิมพ์ cmd จะขึ้นหน้าต่าง command line ครับ ตามรูปเลย
แล้วจะขึ้นหน้าต่างสำหรับให้พิมพ์ command สำหรับคนที่ไม่เคยได้ลองคำสั่ง command line นั้นก็ไม่เป็นไรครับผมก็จะค่อยบอกอยู่แล้ว โดยตอนเริ่มแรกของโฟรเดอร์อาจจะไม่เหมือนกันให้คุณพิมพ์ “cd \” ( ไม่ต้อง copy double quote ” ไปนะครับ ) เสร็จแล้วตัว cursor ของเราก็จะมาอยู่ drive C อย่างเดียว
ต่อมาให้คุณพิมพ์ cd mongo ( จริงๆเวลาพิมพ์ก็พิมพ์แค่ mo แล้วกด tab ก็จะช่วยได้นะครับ ) เราก็จะเข้ามาโฟรเดอร์ mongo แล้วต่อไปเราก็เข้าไปยังโฟรเดอร์ bin โดยการพิมพ์คำสั่งว่า cd bin แล้วเราก็ทำการเรียกให้ mongo รันฐานข้อมูลโดยการพิมพ์แค่ mongod เฉยๆก็พอไม่ต้องใส่ .exe นะครับนั่งรูปเลย โดยถ้าขึ้นเหมือนกับในรูปด้านล่างแล้วถือว่าเราได้ทำการเปิด mongo db ให้รันแล้วครับและก็ปล่อยหน้าต่างนี้ไปเลยอย่าปิดนะ
เสร็จแล้วให้ทำการเรียกหน้าต่าง command line อีกครั้งทำเหมือนเดิมแล้วก็เข้ามาที่เดิมเลยครับจากนั้นก็พิมพ์คำสั่ง mongo ก็พอซึ่งหน้าต่างเราไว้รันคำสั่งในการ query ต่างๆของ mongo db โดยถ้าจะให้เปรียบเทียบหน้าต่างนี้เหมือนกับที่คุณเข้า phpmyadmin แล้วทำการรันคำสั่ง query ในเมนู sql อ่ะครับ
จะเห็นว่าเราทำการเชื่อมต่อ DB ที่ชื่อว่า test แต่ต่อไปจะเรียก collection นะครับเพราะว่า mongo เขาเรียกกันแบบนี้แต่ให้เข้าใจว่าคล้ายๆกับเราเรียก db นั่นแหละครับ โดยเราสามารถ test ได้โดยพิมพ์พวกคำสั่งต่างๆได้ดังรูปด้านล่างครับ
ก็เป็นอันว่าเราได้ทำการเปิด service ของ mongdb แล้วแต่ … การเปิดแบบนี้จะทำให้เรารำคาญได้เพราะว่าคุณก็ต้องเปิด command line ทุกครั้งแล้วก็พิมพ์คำสั่งแบบนี้ทุกครั้งคงไม่ดีแน่เราจึงจะทำให้การเปิด service ของ mongodb นั้นสามารถรันทุกครั้งที่เปิดเครื่องทำได้โดยการให้คุณทำการเข้าไปยัง path นี้วิธีเหมือนเดิมที่สอนไปเข้าไปทาง command line นะครับเข้าไปที่ C:/mongo/bin แล้วทำการใส่คำสั่งนี้ครับ
[php]
mongod –logpath “d:\my mongo\logs\my log file name.log” –logappend
–dbpath “d:\my mongo\data” –directoryperdb –install
[/php]
โดยให้แก้ไขตรง path หลังคำว่า –logpath ก็คือสถานที่ที่คุณต้องการเก็บ logs จาก mongodb ครับ และก็แก้ไขอีกที่ตรง –dbpath ให้แก้เป็นที่ที่เราจะทำการเก็บ DB ของเราครับแล้วเราจะรู้ได้ไงว่ามันทำสำเร็จแล้วให้คุณเรียกหน้าต่าง run แล้วพิมพ์ว่า services.msc แล้วจะมีหน้าต่าง รายงานว่ามี service ตัวไหนกำลังทำงานอยู่ถ้าคุณเห็น Mongo DB อยู่ก็ถือว่าสำเร็จแล้วดังรูปด้านล่าง
แล้วถ้าคุณลองสร้างไฟล์ PHP ซักไฟล์มาลอง test mongo db แนะนำให้เข้าไปดูตัวอย่างที่หน้านี้เลยครับ example php ถ้ารันได้ก็แปลว่าคุณทำสำเร็จแล้ว !!! เย้ ^_^ / สำหรับบทความนี้ก็ขอจบลงเพียงเท่านี้ครับ ถ้ามีปัญหาอะไรก็ comment ไว้เลยจ้าจะมาตอบให้นะครับผม

16 กันยายน 2556

10 มิถุนายน 2556

custom accordion jquery

Margin and Padding

Try applying margin-bottom:5px; to .ui-accordion-header.
This will cause a gap between the open header and it's content, but you can fix that by removing the margin on .ui-accordion-header.ui-state-active and applying it to .ui-accordion-content-active instead.
So your css would look like this:
.ui-accordion-header {
margin-bottom:5px;
}
.ui-accordion-header.ui-state-active {
margin-bottom:0;
}
.ui-accordion-content-active {
margin-bottom:5px;
}

That might need a bit of tweaking, but should get you mostly there.

อ้างอิง http://stackoverflow.com/questions/3229723/jquery-ui-accordion-issue

สำหรับทดสอบ script online
http://jsbin.com/adele3/687/edit 

Extending Jquery accordion to rows of a table
http://stackoverflow.com/questions/16046654/extending-jquery-accordion-to-rows-of-a-table

8 พฤษภาคม 2556

Simple export php to excel
  1. $result=mysql_query("select * from tbl_name");
  2. function xlsBOF()
  3. {
  4. echo pack("ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0);
  5. return;
  6. }
  7. function xlsEOF()
  8. {
  9. echo pack("ss", 0x0A, 0x00);
  10. return;
  11. }
  12. function xlsWriteNumber($Row, $Col, $Value)
  13. {
  14. echo pack("sssss", 0x203, 14, $Row, $Col, 0x0);
  15. echo pack("d", $Value);
  16. return;
  17. }
  18. function xlsWriteLabel($Row, $Col, $Value )
  19. {
  20. $L = strlen($Value);
  21. echo pack("ssssss", 0x204, 8 + $L, $Row, $Col, 0x0, $L);
  22. echo $Value;
  23. return;
  24. }
  25. header("Pragma: public");
  26. header("Expires: 0");
  27. header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  28. header("Content-Type: application/force-download");
  29. header("Content-Type: application/octet-stream");
  30. header("Content-Type: application/download");;
  31. header("Content-Disposition: attachment;filename=list.xls");
  32. header("Content-Transfer-Encoding: binary ");
  33. xlsBOF();
  34. xlsWriteLabel(0,0,"Heading1");
  35. xlsWriteLabel(0,1,"Heading2");
  36. xlsWriteLabel(0,2,"Heading3");
  37. $xlsRow = 1;
  38. while($row=mysql_fetch_array($result))
  39. {
  40. xlsWriteNumber($xlsRow,0,$row['field1']);
  41. xlsWriteLabel($xlsRow,1,$row['field2']);
  42. xlsWriteLabel($xlsRow,2,$row['field3']);
  43. $xlsRow++;
  44. }
  45. xlsEOF();

22 เมษายน 2556

import csv to mysql with php


# Author: Keegan
# Email: keegan@sifizm.com
# Web Site: www.sifizm.com

# I run this script from a cron job every night to update
# the mysql database I use with my employee web site
# so it matches my local database every day. Feel free to
# modify it to meet your specific needs. If you find it
# usefull, drop me an email and let me know.

# edit the follow six items to use the script

# first connect to your mysql database
# i have my connection settings in a diferent file
# so i just include that file in all my scripts
include("db.php");

# assign the tables that you want to import to to the table array
$table = array(
'table1',
'table2',
'table3',
'table4',
'table5',
);

# if the first row of your csv file contains column headings:
# $columnheadings=1
# if the first row does not contain column headings and should be imported:
# $columnheadings=0
$columnheadings = 0;

# contains the email address you want the results sent to
$emailaddress = "user@domain.com";

# contains the subject you want the message to have
$subject = "Enter Subject Here";

# contains the email address that will show in the from line
$emailfrom = "user@domain.com";

# you should not have to edit anything below this line



# perform the required operations for every table listed in the table array
foreach ($table as $tablename) {

# empty the table of its current records
$deleterecords = "TRUNCATE TABLE `$tablename`";
mysql_query($deleterecords);

# intialize your counters for successful and failed record imports
$pass = 0;
$fail = 0;

# the csv file needs to be the same name as the table,
# comma seperated with the columns in the same order as the table,
# and in the same dir as this script
$filecontents = file ("$tablename.csv"); # .csv is added to the table name to get the name of the csv file

# every record in the csv file will be inserted into the table unless an error occurs with that record
for($i=$columnheadings; $i$insertrecord = "Insert Into `$tablename` Values ($filecontents[$i])";
mysql_query($insertrecord);
if(mysql_error()) {
$fail += 1; # increments if there was an error importing the record
}
else
{
$pass += 1; # increments if the record was successfully imported
}
}

# adds a line to the email message we will send stating how many records were imported
# and how many records failed for each table
$message .= "Table $tablename: Success=$pass Failure=$fail \n";
}

# set to the date and time the script was run
$runtime = (date("d M Y H:i"));

# add the run time to the body of the email message
$message .= "\nTime of the message: $runtime (server time zone)\n\n";

# Send the email message
mail($emailaddress, $subject, $message, "From: '$emailfrom'");

?>