Archive for กันยายน, 2008

Array in smarty template

ในไฟล์ index.php


<?php
include "init.php";
$smarty = new arrSmarty;

$branch = array(
$fuji =array(
'Ngamwongwan' => array(
'name' => 'Mana'
,'position' => 'Manager'
),
'Ladprao' => array(
'name' => 'Manee'
,'position' => 'Super visor'
)
)
);

$smarty->assign('branch', $branch);
$smarty->display("index2.html");

?>

ในไฟล์ index.html


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "<a href="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd</a>">
<html xmlns="<a href="http://www.w3.org/1999/xhtml">http://www.w3.org/1999/xhtml</a>">
<head>
<title>Array</title>
</head>

<body>
{foreach from=$branch key=branch item=branchArr}
Fuji branches ::<hr/>
{foreach from=$branchArr key=fuji item=brachItemArr}
Brach::{$fuji}<br/>
Name ::
{$brachItemArr.name}<br/>
Position::
{$brachItemArr.position}<br/>
<hr/>
{/foreach}

{/foreach}
</body>
</html>

ผลลัพธ์ที่ได้คือ

Fuji branches ::


Brach::Ngamwongwan
Name :: Mana
Position:: Manager


Brach::Ladprao
Name :: Manee
Position:: Super visor

:)

  • Comments Off

ในการนำข้อมูลไปแสดงในหน้าเว็บ มักจะต้องแสดงเป็น row-column อยู่เสมอๆ

ส่วนของข้อมูล

$myArr = array();
     $myArr[0] = array(‘code’ => ‘0001′, ‘name’ => ‘name1′);
     $myArr[1] = array(‘code’ => ‘0002′, ‘name’ => ‘name2′);
     $myArr[2] = array(‘code’ => ‘0003′, ‘name’ => ‘name3′);
     $myArr[3] = array(‘code’ => ‘0004′, ‘name’ => ‘name4′);
     $myArr[4] = array(‘code’ => ‘0005′, ‘name’ => ‘name5′);
     $myArr[5] = array(‘code’ => ‘0006′, ‘name’ => ‘name6′);
     $myArr[6] = array(‘code’ => ‘0007′, ‘name’ => ‘name7′);
     $myArr[7] = array(‘code’ => ‘0008′, ‘name’ => ‘name8′);
     $myArr[8] = array(‘code’ => ‘0009′, ‘name’ => ‘name9′);

และ อยากจะแสดงเป็นแถว แถวละ 3 คอลั่ม จัดข้อมูลใหม่ ให้มีโครงสร้างเหมือนกับที่ต้องการแสดงผล คือมองเป็นแถว และคอลั่ม เช่น

$myNewArr = array();
     $myNewArr[0] = array(
          array(‘code’ => ‘0001′, ‘name’ => ‘name1′)
         ,array(‘code’ => ‘0002′, ‘name’ => ‘name2′)
         ,array(‘code’ => ‘0003′, ‘name’ => ‘name3′)
     );
     $myNewArr[1] = array(
          array(‘code’ => ‘0004′, ‘name’ => ‘name4′)
         ,array(‘code’ => ‘0005′, ‘name’ => ‘name5′)
         ,array(‘code’ => ‘0006′, ‘name’ => ‘name6′)
     );
     $myNewArr[2] = array(
          array(‘code’ => ‘0007′, ‘name’ => ‘name7′)
         ,array(‘code’ => ‘0008′, ‘name’ => ‘name8′)
         ,array(‘code’ => ‘0009′, ‘name’ => ‘name9′)
     );

ซึ่งถ้าลองพิจารณาดูโครงสร้างของข้อมูลแล้ว มันก็จะมีลักษณะเหมือนกับการแสดงผลที่เราต้องการแล้ว คือ

แถวที่ 1: มี 3 คอลั่ม

$myNewArr[0] = array(
array(’code’ => ‘0001′, ‘name’ => ‘name1′)
,array(’code’ => ‘0002′, ‘name’ => ‘name2′)
,array(’code’ => ‘0003′, ‘name’ => ‘name3′)
);

แถวที่ 2: ก็มี 3 คอลั่ม

$myNewArr[1] = array(
array(’code’ => ‘0004′, ‘name’ => ‘name4′)
,array(’code’ => ‘0005′, ‘name’ => ‘name5′)
,array(’code’ => ‘0006′, ‘name’ => ‘name6′)
);

และเช่นเดียวกัน แถวที่ 3 ก็มี 3 คอลั่มเช่นกัน

แล้วจะจัดจาก $myArr เป็น $myNewArr ได้อย่างไร?

ในการจัด $myArr ให้เป็น $myNewArr ให้มีโครงสร้างเหมือนกับที่เราจะแสดงผล ก็ทำได้ง่ายๆ โดยใช้ for เช่น

$perRow = 3;
     $totalData = sizeof($myArr);
     $myNewArr = array();

     $n = 0;
     for($i=0; $i<$totalData; $i+=$perRow)
     {
         for($j=0; $j<$perRow; $j++)
         {
             $myNewArr[$n][$j] = $myArr[$i+$j];
         }
         $n++;
     }
     print_r($myNewArr);

ตรงที่กำหนดค่าตัวแปล $perRow = 3 นั้น ถ้าเราต้องการให้มีจำนวน colum ต่างจากนี้ ก็แค่แก้ตัวเลข ส่วน loop-for ก็ไม่ต้องแก้อะไร

การแสดงผลในฝั่ง smarty template

ตัวอย่างการเขียน code เพื่อแสดงผลข้อมูลเป็นแถวๆ แถวละ 3 คอลั่ม ในฝั่ง smarty template ก็ใช้ foreach 2 รอบก็พอแล้ว เช่น

<table>
 {foreach from=$myNewArr item=rowArr}
 <tr>
     {foreach from=$rowArr item=item}
     <td>
         {if $item.code}
             <span class=”someClass”>{$item.code} :: {$item.name}</span>
         {else}
             <i>(empty column)</i>
         {/if}
     </td>
     {/foreach}
 </tr>
 {/foreach}
 </table>
  • Comments Off

Eclipse

จากบทความเดิม “การตั้งค่าสำหรับการพัฒนาโปรแกรมด้วย PHP” ของคุณ overbid เขียนไว้ละเอียดดีทีเดียว โดยส่วนตัวผมได้ลองใช้ Eclipse PDT แล้วชักติดใจ โดยเฉพาะ ถ้าเขียนโปรแกรมแบบ OO แล้วช่วยได้เยอะมากๆ เลยอยากจะมาชวนให้มาลองใช้กัน (ใช้แล้วอาจจะลืม EditPlus ไปเลยก็ได้น้ะ :-)

มันดียังงัย ทำไมต้องใช้ EditPlus ก็ไวดีอยู่แล้ว?

โดยส่วนตัว ผมชอบตรง auto complete คือเราใส่ชื่อ function แล้ว ( อันนึง ตัว PDT ก็จะโชว์ขึ้นมาว่ามันต้องส่ง argument อะไรเข้าไปบ้าง และจะ return อะไรออกมาให้ และในลักษณะเดียวกัน ถ้าเราเขียนโปรแกรมแบบ OO แล้ว จะพบว่ามันสะดวกมากๆๆ เพราะ เพียงแค่พิมพ์ $yourObject-> มันก็จะ list attributes และ methods ทั้งหมดที่ใช้ได้ขึ้นมาให้ พร้อมบอก argument(s) และ return value มาให้เสร็จสรรพ ทำให้ประหยัดเวลาการเปิด document ได้เยอะทีเดียว

Installation

ก่อนอื่น ให้ไปดาวน์โหลดมาก่อน คุณจะดาวน์โหลด Eclipse มาก่อนแล้วโหลด PDT มาติดตั้ง หรือจะโหลด version ที่เขารวม Eclipse + PDT ไว้ให้แล้วก็ไม่ว่ากัน แล้วแต่ชอบ ไปดาวน์โหลดได้ที่ http://download.eclipse.org/tools/pdt/downloads/release.php?release=R200… สำหรับมือใหม่คนที่ต้องการความสะดวกสบาย ขอแนะนำ PDT All-in-One (เช่น pdt-all-in-one-R20080603-win32.zip ขนาดไฟล์ 122.46MฺB) น้ะครับ จะได้ไม่ยุ่งยาก แบบว่า ดาวน์โหลดมา แล้วแตก ZIP ไฟล์ ก็ใช้ได้เลย!

เริ่มใช้งาน

ผมขอกล่าวถึงตัว PDT All-in-One น้ะครับ หลังจากดาวน์โหลดมาแล้ว (เช่น pdt-all-in-one-R20080603-win32.zip) ก็แตก ZIP ไฟล์ แล้วเข้าไปใน folder ชื่อ eclipse ที่เราแตก ZIP ไฟล์ แล้วดับเบิ้ลคลิกที่ eclipse.exe ได้เลย หรือจริง อาจจะ move folder ชื่อ eclipse นี้ไปไว้ยังที่ ที่เหมาะสมก็ได้น้ะครับ เช่น ผมเอามันไปไว้ใน “Program Files” แล้วค่อยทำ Shortcut ออกมาไว้ที่ Start Menu หรือท่านจะทำอย่างไรก็สุดแล้วแต่

เมื่อดับเบิ้ลคลิก eclipse.exe จะเห็นตัว Program Dialog ของ Eclipse ขึ้นมา

และโปรแกรมก็จะให้เราเลือกตั้ง folder ที่จะใช้เป็นพื้นที่ทำงาน (Workspace) ผมเลือกให้ไปที่ Document Root ที่ตั้งไว้ใน Apache (ที่ติดมากับ AppServ) เวลาเรียกรันจะได้เรียกง่ายๆ :)

เข้าโปรแกรมแล้ว จะเจอหน้า welcome

ปิดหน้า welcome แล้วมาลองสร้าง project ใหม่กันเลย

เลือกที่เมนู File / New / Project จะเห็นหน้านี้

ตั้งชื่อโปรเจ็คตามใจชอบ แล้วกด Finish ไปเลย

ได้มาแล้ว โปรเจ็ค PHP ของฉัน (my-php-project) จากนั้น ให้คลิกขวาที่ชื่อโปรเจ็คของเรา และเลือก Properties เพื่อตั้งค่าบางอย่างซักเล็กน้อย

เลือก Text File Encoding เผื่อจะเขียน code ด้วย UTF-8 หรืออาจจะใช้ default ที่เป็น MS874 หรือ Windows-874 ที่คุ้นเคยก็ไม่ว่ากัน แต่ผมใช้ UTF-8 เพราะว่า DB ก็ใช้ UTF-8 จะได้เป็นแบบเดียวกันทั้งหมด จะได้ไม่งงเอง :D

หมายเหตุ: Text File Encoding นี้จะเป็น MS874 หรือ UTF-8 ก็พิมพ์ไทยได้น้ะครับ

และ New Text File line delimiter ผมมักเลือกเป็นแบบ Unix เพราะว่าท้ายที่สุดผมรัน App. บน Linux Server

และอีกอย่างที่ควรเซตคือ PHP Include Path/ Libraries / Add External Folder… เพื่อเพิ่ม path ที่เราเก็บ libraries ไว้ เวลาเรียกใช้งาน PDT จะได้ list attributes และ methods หรือ รายละเอียดของการใช้งาน function ต่างๆที่เกี่ยวข้องขึ้นให้

เมื่อเพิ่ม path เสร็จแล้วกด OK ตัว PDT จะเซต Include Path ให้โดยใช้เวลานิดหน่อย

ลองสร้างไฟล์ PHP มาซัก 1 ไฟล์ โดยคลิกขวาที่ชื่อโปรเจ็คของเรา แล้วเลือก New / PHP File แล้วพิมพ์ code ง่ายๆ เหมือนในรูป แต่ตอนพิมพ์ $smarty-> นี้ให้พิมพ์ถึง -> แล้วหยุดรอแป็บเดียว PDT ก็จะ list method ทั้งหมดที่ใช้งานได้ของ object $smarty มาให้

  • Comments Off

Sending HTTP POST with php cURL

You can see that the form will submit the query using HTTP POST to ?target.php?. Now let?s say you want to write a php script (bot.php) that will automatically send the query bypassing the html form, this is one way to do it (with php libcurl extension)


< ?php
//bot.php
$url = "http://localhost/wtf/target.php";
$ch = curl_init();

// set the target url
curl_setopt($ch, CURLOPT_URL,$url);

// howmany parameter to post
curl_setopt($ch, CURLOPT_POST, 1);

// the parameter 'username' with its value 'johndoe'
curl_setopt($ch, CURLOPT_POSTFIELDS,"username=johndoe");

$result= curl_exec ($ch);
curl_close ($ch);
print $result;

?>

This script will send a HTTP POST request to ?target.php? pretending to be a real person sending the ?username? parameter as ?john doe?.

However this is not entirely convincing since the server side will automatically know that you are using a http script to send the HTTP POST request by analyzing the browser ?user-agent? string. The default script will send ?(HTTPRetriever/1.0)? as its user-agent.

With a little add-on, you can spoof the user-agent string inside your script just like this :


< ?php
//
// test HTTP POST submitter, using libcurl
//

// the target url which contains scripts that accepts post request
$url = "http://localhost/wtf/target.php";

// we are spoofing Yahoo Seeker bot >:)
$useragent="YahooSeeker-Testing/v3.9 (compatible; Mozilla 4.0; MSIE 5.5; http://search.yahoo.com/)";

$ch = curl_init();

// set user agent
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);

// set the target url
curl_setopt($ch, CURLOPT_URL,$url);

// howmany parameter to post
curl_setopt($ch, CURLOPT_POST, 1);

// the parameter 'username' with its value 'johndoe'
curl_setopt($ch, CURLOPT_POSTFIELDS,"username=johndoe");

// execute curl,fetch the result and close curl connection
$result= curl_exec ($ch);
curl_close ($ch); 

// display result
print $result;

?>

so when your ?bot.php? sends the request, the server logs will record that the query was sent by a ?Yahoo Seeker bot? instead of a crudely coded php script.

You can spoof other browser as long as you know their user-agent string, refer to my previous post for a collection of browser user-agent strings.
No PHP cURL support?

In this case, you have a few options

1. Use a server that support php cURL extension
2. Compile/Install php cURL extension
3. Use libcurlemu - php cURL extension written in pure php

Well that should cover the short crash course on how to use php cURL extension.

p/s : Although I won?t tell you how to write one directly, this is the basic of building spam bots and auto-submitter. So use your imagination (and the dark side of the force) to write the rest of the code. *evil*

  • Comments Off

- ในการติดตั้ง Easy site install ในส่วนของ Oscommerce หรือ สคริ๊บทั่วไป ที่ต้องการค่าของ register_globals ให้เป็น ON โดยอาจมีข้อความฟ้อง

” Server Requirement Error: register_globals is disabled in your PHP configuration. This can be enabled in your php.ini configuration file or in the .htaccess file in your catalog directory. ”

สามารถเปิดการใช้งานได้โดยการสร้างไฟล์ .htaccess ไว้ในที่เดียวกับไฟล์ต่างๆที่เกี่ยวข้อง และพิมพ์คำสั่ง
php_flag register_globals on ก็จะสามารถใช้งานเว็บไซต์ได้ตามปรกติ

- สามารถเพิ่มค่า การ Upload สูงสุดของ php ได้โดยใช่คำสั่ง

php_value post_max_size 16M
php_value upload_max_size 16M

เพิ่มไปในไฟล์ของ .htaccess ได้ (16 M เป็นค่าที่ต้องการให้สามารถ Upload และ Post ได้สูงสุด ใส่ค่าได้ตามต้องการ)

สคริปบางตัวที่ ใช้งานกัน php4 ถ้านำมาใช้กับ php5 ไม่ได้ก็แก้ไข ตามที่บอกครับ

  • Comments Off

Recaptcha install

process from submit

?
require_once('recaptchalib.php');
?$privatekey = "Your Private key";
?$resp = recaptcha_check_answer ($privatekey,
?$_SERVER["REMOTE_ADDR"],
?$_POST["recaptcha_challenge_field"],
?$_POST["recaptcha_response_field"]);
?if (!$resp->is_valid) {
?die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." .
?"(reCAPTCHA said: " . $resp->error . ")");
?}

from page


<script type='text/javascript'>
	var RecaptchaOptions = { theme : 'red', lang : 'en' , tabindex : 5 };
</script>
<script type="text/javascript" src="http://api.recaptcha.net/challenge?k=plublic key "></script>

<noscript>
<iframe src="http://api.recaptcha.net/noscript?k=plublic key" height="300" width="500" frameborder="0"></iframe><br/>
<textarea name="recaptcha_challenge_field" rows="3" cols="40"></textarea>
<input type="hidden" name="recaptcha_response_field" value="manual_challenge"/>
</noscript>		

Time

Mp3

Msn status

  • manon2029@hotmail.com is

Chat with Meeh

Donate

    If you find an article useful, then please make a donation.

หมวดหมู่

UserOnline

Counter

  • Visitors today: 187
  • Visitors yesterday: 535
  • Visitors per day: 470
  • Max. visitors per day: 665
  • Total page views: 749,967
  • Page views of this page: 470
  • Currently online: 11
  • Max. online: 192
  • Total visitors: 157,604
  • counterStatistics