Register    Login    Forum    FAQ

Board index » F.A.Q. » PHP




Post new topic Reply to topic  [ 8 posts ] 
Author Message
 Post subject: คลาสจำลองตัวแปรแบบ application scope
 Post Posted: 18 Mar 2009, 14:51 
Offline

Joined: 28 Jan 2009, 11:28
Posts: 304
ใน jsp ก็มีใน asp ก็มีแต่ php ไม่มี เลยเขียนเองซะเลย
Code:
<?php class AppVar { var $fn='appobj.txt'; function write($key,$data){ $fn = $this->fn; $var=$this->read(); $var[$key]=$data; $f=fopen($fn,'w'); flock($f,LOCK_EX); fwrite($f,serialize($var)); flock($f,LOCK_UN); fclose($f); } function read($key=null,$default=null){ $fn = $this->fn; if(!file_exists($fn)) return is_null($key)?array():$default; $f=fopen($fn,'r'); flock($f,LOCK_SH); $d=''; while(!feof($f)) $d.=fread($f,512); flock($f,LOCK_UN); fclose($f); $var=unserialize($d); return is_null($key) ?(is_null($var)?array():$var) :(isset($var[$key])?$var[$key]:$default); } } /* ทดลองสร้าง counter จากการตัวแปรแบบ application scope */ $app = new AppVar(); $c = $app->read('c',0)+1; $app->write('c',$c); echo "counter=$c"; ?>


เวอร์ชั่นใหม่แบบย้ายไฟล์ไปมาได้ทั้ง *nix และ windows :yoyo03:
Code:
<?php class AppVar { var $fn='appobj.txt'; function write($key,$data){ $fn = $this->fn; $var=$this->read(); $var[$key]=$data; $f=fopen($fn,'w'); flock($f,LOCK_EX); fwrite($f,(base64_encode(serialize($var)))); flock($f,LOCK_UN); fclose($f); } function read($key=null,$default=null){ $fn = $this->fn; if(!file_exists($fn)) return is_null($key)?array():$default; $f=fopen($fn,'r'); flock($f,LOCK_SH); $d=''; while(!feof($f)) $d.=fread($f,512); flock($f,LOCK_UN); fclose($f); $var=unserialize(base64_decode($d)); return is_null($key) ?(is_null($var)?array():$var) :(isset($var[$key])?$var[$key]:$default); } } $app = new AppVar(); $c = $app->read('c',0)+1; $app->write('c',$c); echo "counter=$c"; ?>


แต่ดูอีกทีคล้ายๆ กับ class ของ Dr.Yes เลยแหะ :yoyo15:
http://php.deeserver.net/dryes/articles ... 529079580/

_________________
In your life, There's no such a thing as you can do it or you can't!! but, it's a matter of 'doing it or not doing it!!
the talent of hard work
the first step to the nex level, i think is courageously stepping into the unknown


Last edited by num on 18 Mar 2009, 17:43, edited 2 times in total.

   The following user would like to thank num for this post:
Top 
 Profile  
 
 Post subject: Re: คลาสจำลองตัวแปรแบบ application scope
 Post Posted: 18 Mar 2009, 16:12 
Offline
Site Admin
User avatar

Joined: 19 Jan 2009, 12:33
Posts: 324
Location: สะพานสูง, กรุงเทพฯ
ที่ฟังก์ชั่น AppVar::write ก่อนจะเขียนลงไป ควรจะเพิ่ม

$data = str_replace(array("\r\n", "\r"), "\n", $data);

ไปก่อนครับ เพราะว่าเคยเจอปัญหา เกี่ยวกับ EOL นี่แหละ ใช้บน *nix ได้ แต่โหลดกลับมาเครื่อง windows
แล้วใช้ไม่ได้ :yoyo09: มัน unserialize ไม่ออก

ถ้าทำเพิ่มอีกหน่อยก็คือ ตอนอ่านก็เพิ่ม

$data = str_replace("\n", PHP_EOL, $data);

ก่อน return ค่ากลับ


ป.ล. ไม่แน่ใจว่าวิธีแก้อันนี้ถูกหรือเปล่า แต่จำได้ว่ามันมีปัญหา ต้องมานั่ง convert file จาก unix->windows ก่อน แล้วค่อย unserialize @__@

_________________
"I'm not a doctor", said Dr.Yes
"What we have done for ourselves alone, dies with us;
What we have done for others and the world, remains and is immortal"
, Albert Pike
1 2 3


   The following user would like to thank Dr.Yes for this post:
Top 
 Profile  
 
 Post subject: Re: คลาสจำลองตัวแปรแบบ application scope
 Post Posted: 18 Mar 2009, 16:55 
Offline

Joined: 28 Jan 2009, 11:28
Posts: 304
ขอบคุณครับ :yoyo16:

ที่มัน unserialize ไม่ได้เป็นเพราะอะไรหนอ
php น่าจะเก็บแบบตรงไปตรงมานะ \n ก็ \n \r\n ก็ \r\n นะ หรือว่า เป็นปัญหาตอน save Image

...
ไปทดลองดูแล้วครับ

serial 92 byte ใช้ ftp download มาได้ 94 byte
แต่ถ้า save จากหน้าเว็บได้ serial 92 byte คงเดิม ต้องเป็นตัว ftp มันไปแปลงข้อมูลแน่ๆ เลย :yoyo40:

_________________
In your life, There's no such a thing as you can do it or you can't!! but, it's a matter of 'doing it or not doing it!!
the talent of hard work
the first step to the nex level, i think is courageously stepping into the unknown


   The following user would like to thank num for this post:
Top 
 Profile  
 
 Post subject: Re: คลาสจำลองตัวแปรแบบ application scope
 Post Posted: 20 Mar 2009, 09:33 
Offline
User avatar

Joined: 19 Jan 2009, 13:04
Posts: 402
Location: อยู่ที่ไหนก็ได้...โตแล้ว
ว่าแต่ อยากรูด้วยว่า มันคือใยอะ :yoyo09:

_________________
ขอบคุณซักคำ มันยากเกินไปไช่ไหม!!!...


   The following user would like to thank danya for this post:
Top 
 Profile  
 
 Post subject: Re: คลาสจำลองตัวแปรแบบ application scope
 Post Posted: 20 Mar 2009, 09:51 
Offline
Site Admin
User avatar

Joined: 19 Jan 2009, 12:33
Posts: 324
Location: สะพานสูง, กรุงเทพฯ
num wrote:
ไปทดลองดูแล้วครับ

serial 92 byte ใช้ ftp download มาได้ 94 byte
แต่ถ้า save จากหน้าเว็บได้ serial 92 byte คงเดิม ต้องเป็นตัว ftp มันไปแปลงข้อมูลแน่ๆ เลย :yoyo40:


เป็นเพราะ FTP เหรอเนี่ย :yoyo40: ว้ากกกก

_________________
"I'm not a doctor", said Dr.Yes
"What we have done for ourselves alone, dies with us;
What we have done for others and the world, remains and is immortal"
, Albert Pike
1 2 3


   The following user would like to thank Dr.Yes for this post:
Top 
 Profile  
 
 Post subject: Re: คลาสจำลองตัวแปรแบบ application scope
 Post Posted: 10 Jul 2009, 21:20 
Offline
User avatar

Joined: 19 Jan 2009, 13:04
Posts: 402
Location: อยู่ที่ไหนก็ได้...โตแล้ว
จากโค้ดตัวนี้

function fetchBanner($position)
{
global $db;

$ROW = array();

if ( $position == '' ) return $ROW;

if ( !isset($_SESSION['mm_fetchbanner']) ) $_SESSION['mm_fetchbanner'] = array();

if ( !in_array( $position, $_SESSION['mm_fetchbanner'] ))
{
$sql = $db->query_fetch("banner AS b
LEFT JOIN shop AS s ON ( s.guru_id = b.guru_id)
LEFT JOIN product AS p ON (p.product_id = b.product_id )",'',"banner_position = '{$position}'",'', "0,20");

if ( $db->numrows($sql) > 0 )
{
while ( $row = $db->fetch_array($sql) )
{
$ROW[] = $row;
$_SESSION['mm_fetchbanner'][$position][] = $row;
}
}
}
else
{
$ROW = $_SESSION['mm_fetchbanner'][$position];
}
return $ROW;
}


ผมใช้ $_SESSION อะ

ถ้าจะเปลียนมาใช้ AppVar จงยกตัวอย่างมา เอยนะ อิอิอิ

_________________
ขอบคุณซักคำ มันยากเกินไปไช่ไหม!!!...


   The following user would like to thank danya for this post:
Top 
 Profile  
 
 Post subject: Re: คลาสจำลองตัวแปรแบบ application scope
 Post Posted: 10 Jul 2009, 21:37 
Offline

Joined: 28 Jan 2009, 11:28
Posts: 304
น่าจะแบบนี้นะ
<?php
function fetchBanner($position)
{
global $db;


$app = new AppVar(); //####
$banner = $app->read('banner',array()); //####

$ROW = array();

if ( $position == '' ) return $ROW;

if ( !isset($banner['mm_fetchbanner']) ) $banner['mm_fetchbanner'] = array();



if ( !in_array( $position, $banner['mm_fetchbanner'] ))
{
$sql = $db->query_fetch("banner AS b
LEFT JOIN shop AS s ON ( s.guru_id = b.guru_id)
LEFT JOIN product AS p ON (p.product_id = b.product_id )",'',"banner_position = '{$position}'",'', "0,20");

if ( $db->numrows($sql) > 0 )
{
while ( $row = $db->fetch_array($sql) )
{
$ROW[] = $row;
$banner['mm_fetchbanner'][$position][] = $row;
}
}
}
else
{
$ROW = $banner['mm_fetchbanner'][$position];
}
$app->write('banner',$banner); //###

return $ROW;
}
?>

_________________
In your life, There's no such a thing as you can do it or you can't!! but, it's a matter of 'doing it or not doing it!!
the talent of hard work
the first step to the nex level, i think is courageously stepping into the unknown


   The following user would like to thank num for this post:
Top 
 Profile  
 
 Post subject: Re: คลาสจำลองตัวแปรแบบ application scope
 Post Posted: 11 Jul 2009, 01:20 
Offline
User avatar

Joined: 22 Jan 2009, 23:36
Posts: 360
:yoyo09:

_________________
May the force be with you...


   The following user would like to thank bAnk for this post:
Top 
 Profile  
 
Display posts from previous:  Sort by  
 
Post new topic Reply to topic  [ 8 posts ] 

Board index » F.A.Q. » PHP


Who is online

Users browsing this forum: No registered users and 1 guest

 
 

 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Jump to:  
refresh
· 30 กระทู้อัพเดตล่าสุด ·