FPDF is a freeware PHP class which allows to generate PDF files with pure PHP, no PDFlib library required. Works with both PHP4 and PHP5.
Tag: PHP
Installing PHP on Mac OS X
Soooo easy, just only few clicks! You download a dmg file, open it, double-click an installer, and that’s it! The http://127.0.0.1/~yourname/ will be there ready for a PHP scripting!
Note: this is only for Mac OS X Client (the typical one you’ll found on desktops/notebooks), not for Mac OS X Server.
ใช้เวลาลง 2 นาที – มาม่ายังไม่ทันสุก ต้องไปกินไวไวควิกหรือคัพนู้ดเดิ้ลวันแทน
so unnecessary code
I’m currently (busy) working with a web UI for one project.
There’s nothing much complicated, just displaying a requested document in a web browser and provide a user a way to highlight some “interesting parts” of the document interactively.
Something like underline all the words with a specified category, or mark a set of words that are related to each other, or hide away sentences that are not very important. No need to calculate anything, all the information are provided in a requested document (XML).
I chose a mix of PHP, XSLT, and JavaScript to do this.
At the first place, I tried it with PHP and JavaScript with no XSLT.
But don’t like the idea to convert XML to HTML by PHP (or whatever language) coding. I just feel that it’s not safe to do so, in terms of long-term maintenance.
So I moved the conversion logic to XSLT, which is quite programming language neutral (depends on the availability of the XSL library).
We may drop the use of PHP at some point, but the conversion is still there in the stylesheet, which can be used by many programming languages.
Hope that’s a right decision.
After using XSLT, I had realized that, in fact, with the XSLT library in JavaScript (like one from Google Code), I can do everything I need by JavaScript.
Anyway, the library seems to be not very cross-browser.
So I just let the PHP do XSLT, also other backoffice stuffs, and let the JavaScript do the user interaction stuffs. Which is, so far, quite nice.
The problem is, I’m not a JavaScript master. My JavaScript code is ‘workable’ but far from ‘elegant’.
After a while, I found myself in a middle of repeated pieces of code.
Most of them are just a code to traverse around my DOM tree,
and all of these traverse codes in every functions are actually using the same logic.
There should be a more elegant way to do this.
I think it should be possible to abstract these following components away from each other:
- traverse/looping
- test logic
- action to perform
something like:
(Note: the following code is just an idea, I’m pretty sure it’s not going to work.)
function process(objects, loop, test, trueAction, falseAction) {
loop (objects, function (object) {
if (test(object)) {
if (trueAction)
trueAction(object);
} else {
if (falseAction)
falseAction(object);
}
});
}
function traverseEveryNode(nodes, perfomer) {
for (node in nodes) { // just an example, the actual DOM is tree
performer(node);
}
}
and then you call:
process(dom, traverseEveryNode, isNoun, markYellow, false);
to paint yellow to all nouns in the given document. Given there’re functions named traverseEveryNode(), isNoun(), and markYellow().
How to do this in JavaScript ?
It seems like there’s something to do with a so-called “closure”.
But I still have no idea what it actually is at the moment — now reading.
If you have any clue, I’m very much appreciated to listen to 😉
Cheat Sheets Parade
JavaScript, MySQL, PHP, CSS, and mod_rewrite Cheat Sheets, topped up with an RGB Hex Colour Chart — A real lucky web developer fears no exam!
Deer Park hates long string without whitespace
วนลูป foreach ให้ PHP มัน append สตริง ด้วยค่าประมาณ “tok_N”, ไปเรื่อย ๆ เยอะเหมือนกัน
หลังจบลูป เราก็ตัดเจ้า , ตัวท้ายสุดทิ้ง จะได้ประมาณนี้
"tok_1","tok_2","tok_3",...,"tok_N"
แล้วก็พิมพ์ค่านี้ออกไป โดยใช้ echo ธรรมดา ๆ (ที่ตั้งใจคือจะเอาไปประกาศเป็น Array ใน JavaScript น่ะ)
พบปัญหาคือ ถ้าสตริงมัน “ยาวมาก ๆ” เนี่ย Deer Park (Firefox รุ่นก่อนรุ่นทดสอบ) มันไม่ยอมพิมพ์ให้แฮะ -_-“
คือ Deer Park มันคงต้องรับสตริงชุดนี้เข้าไปแล้วแหละ (เพราะเว็บเบราเซอร์มันไม่ได้ทำหน้าที่สร้างหนิ ตัวเว็บเซิร์ฟเวอร์ตะหากที่สร้าง) แต่ไม่ยอมแสดง กดดู Page Source ก็ไม่มี. แต่พอลองหน้าเดียวกันด้วย IE แล้วไม่มีปัญหา แสดงได้ทุกอย่าง
วิธีแก้ปัญหาคือ ตอน append ให้เติมช่องว่างแทรกไปด้วย “tok_N”,_ (_ ตัวสุดท้าย คือช่องว่างนะ) ก็จะได้สตริงแบบนี้:
"tok_1", "tok_2", "tok_3", ..., "tok_N"
คราวนี้พิมพ์ออกอ่ะ
เหมือนมันไม่ได้เกี่ยวกับ “ความยาว” แฮะ (เพราะเติมช่องว่างเข้าไป ก็ต้องยิ่งยาวขึ้นดิ: โค้ดที่ลองนี่ ก่อนเติมยาว 6551, หลังเติมยาว 7216) แต่เกี่ยวกะ “ไม่มี whitespace” ??
อะไรเนี่ย -_-“
PHP 5 + MySQL settings on Windows
ปวดหัวนิดหน่อย นั่งเซตให้ PHP 5 ใช้ MySQL ได้
ปัญหามันเริ่มมาจาก PHP 5 เค้าไม่ได้รวมฟังก์ชั่น MySQL เอาไว้ในตัว แต่แยกเป็นโมดูลต่างหาก แล้วก็ไม่ได้เปิดไว้เป็น default ก็เลยต้องเซตเจ้า php.ini กันหน่อย
ทีนี้ เหมือนไอ้ php.ini มันก็ต๊อง ๆ คือทำอะไรไม่ค่อยเหมือนที่คิดเท่าไหร่ มีคนบ่นกันเยอะ
หลังจากมั่ว ๆ อยู่พักนึง (เคยลองแล้วเมื่อหลายวันก่อนกับ Apache 2.0 แต่ไม่สำเร็จ วันนี้ลองใหม่กับ IIS ที่มากับ Win XP) ก็ได้ความ
ก่อนอื่นต้องไปดาวน์โหลด zip package ของ Windows มาลงก่อน. เน้นว่า zip package เพราะตัว installer ไม่มีโมดูล MySQL ให้นะ (หรือถ้าอยากลงง่าย ๆ ก็เอาตัว installer มาลงก่อน แล้วก็อปไฟล์จาก zip package ทับ ก็เร็วดี ได้เหมือนกัน)
จากนั้นก็ไปเซตไฟล์ php.ini, ปกติมันจะอยู่ใน C:\Windows
; โฟลเดอร์ที่เก็บไฟล์เว็บ, ถ้าใช้ IIS ไม่ต้องเซต ปล่อยว่างเลย doc_root = "D:\home\shared\wwwroot" ; โฟลเดอร์ที่เก็บโมดูลเสริม ของเดิมมันจะเป็น "./" extension_dir = "ext/" ; เอาคอมเมนต์ (;) ที่อยู่ข้างหน้าโมดูลด้านล่างพวกนี้ออก ; multi-byte string, ถ้าใช้ phpMyAdmin มันจะเตือน ถ้าไม่ยอมเปิดตัวนี้ extension=php_mbstring.dll ; MySQL extension=php_mysql.dll
จะเห็นว่า มีการใช้ทั้ง / และ \ สำหรับ path, แถมเจ้า extension_dir ที่เซตมาให้ ก็ไม่ตรงกับเลย์เอาต์จริง ๆ ในแพ็คเกจ -_-“
แถมนิด เซตเจ้านี่ด้วยก็ดี include_path โดยเฉพาะถ้าคิดจะใช้ PEAR
include_path = ".;c:\dev\php\PEAR"
ถ้าปล่อยว่างไว้ ค่าปริยายมันจะเป็น “.;C:\php5\pear” … แต่ค่าปริยายของตัว installer เวลาติดตั้ง PHP มันจะลงที่ C:\php นะ … ก๊า~ -_-“
สำหรับ phpMyAdmin ถ้าเปิดแล้วมีปัญหา ฟ้องว่า Access denied. #2003 – The server is not responding ทำนองนี้ ให้เปิดไฟล์ config.ini.php ของ phpMyAdmin ขึ้นมาแก้:
$cfg['Servers'][$i]['host'] = '.'; // MySQL hostname or IP address
ค่าเดิม ปกติมันจะเป็น ‘localhost’ แต่ผมใช้ไม่เคยได้เลย ‘127.0.0.1’ ก็ไม่ได้ แต่ใช้ ‘.’ แล้วได้แฮะ
อีกค่าที่ต้องแก้ตาม ไม่งั้น phpMyAdmin มันจะเตือนอีก ก็คือ
$cfg['PmaAbsoluteUri'] = 'http://localhost/phpmyadmin/';
ของเดิมมันจะว่าง ๆ โดย phpMyAdmin จะพยายามหาให้เอง แต่ไม่เวิร์กน่ะ ก็บอกมันไปหน่อย
ทั้งหมดที่ว่ามา เป็นการเซต PHP 5, MySQL, phpMyAdmin บน Windows XP สำหรับงานพัฒนาในเครื่องเดียวกันเท่านั้น ยังไม่เคยลองเซตให้มันเชื่อมไปเครื่องรีโมต อาจจะไม่เวิร์กก็ได้
ปัญหาอย่างนึงที่พบ ตอนที่ลองกับ Apache 2.0 คือ ถ้าเกิดเปลี่ยน extension_dir จากเดิม “./” ไปเป็นอย่างอื่นเนี่ย เจ้า PHP ก็จะหาพวกโมดูลที่อยู่ในโฟลเดอร์เดียวกะตัวมันเอง (.) ไม่เจอ -_-” แต่ IIS ไม่มีปัญหานี้แฮะ ก็งง ๆ ทั้ง ๆ ที่ใช้ตัว php-cgi.exe ตัวเดียวกันเลย
Information visualization
- book: The Visual Display of Quantitative Information
- book: Envisioning Information
- book: Visual Explanations: Images and Quantities, Evidence and Narrative
- php lib: Sparkline PHP Graphing Library
- java lib: prefuse, for interactive information visualization
TYPO3
A day with Web/DB application frameworks
Ruby on Rails is on the highlight (อ่าน pok’s blogger มีเรื่องนี้เยอะ พร้อมตัวอย่างจริง).
For Python, there’s Subway, Django, and SQLObject. —
For PHP, there’re Cake and PHP on Trax. —
Java, Trails. —
Groovy, Grails. —
.NET, MonoRail.
Most of them are Rails-inspired (well, Cake itself aggressively claims it’s actually a rip-off!)
นั่งเขียนเว็บบอร์ดใหม่เล่น ๆ ดีกว่า จะได้ลองหัดพวกนี้มั่ง
ยังไงก็ตาม ถ้าต้องเลือกใช้จริง ๆ คงไม่พ้นตัวที่ใช้ PHP, Python หรืออาจจะ Java.
ทำไม ? ก็เพราะ โฮสติ้งทั้งหลาย ส่วนใหญ่ที่เจอ ๆ มา มันใช้ได้แค่นี้น่ะ (PHP นี่เกือบทุกที่, Python ก็เยอะขึ้นละ Java นี่ก็เยอะ แต่แพงกว่าชาวบ้านหน่อย)
Ruby (and Rails) weakness: not very widespread in entry-level (cheap) hosting plans. [search “ruby hosting”]
จะเป็น web app แต่ไม่ค่อยมี web hosting มันก็แย่หน่อยอ่ะนะ
update: ลอง Cake 0.9.1 แล้ว ยังมีบั๊กอยู่, Tutorial ที่มีอยู่ก็สำหรับรุ่นเก่า ใช้กับ 0.9.1 ไม่ได้. รุ่น 0.9.2 กำลังจะออก อีกไม่นาน
Ajaxxxx
What’s new in the world of Ajax-based web developement ?
keep read:
Ajax toolkit
for calling a server-side function directly from a client side (JavaScript):