PHP 변수를 스크립트로 사용하는 방법입니다.

· 12년 전 · 2121

<?php
// PHP변수 스크립트에서 쉽게 사용하기
function setScriptVar($name, $obj) {
global $script;
$script = "<script language='javascript'>";

if(is_object($obj)) {
$script .= "var $name = new Array();";
factory($name, $obj,'object');
} elseif(is_array($obj)) {
$script .= "var $name = new Array();";
factory($name, $obj,'array');
} else {
if(is_numeric($obj)) $script .= "var $name = $obj;";
else $script .= "var $name = '$obj';";
}
$script .= "</script>";
echo $script;
}

function factory($name, $data, $type = NULL) {
global $script;
foreach($data as $key => $val) {
if(is_object($val)){
factory($name.".".$key, $val, 'object');
} elseif(is_array($val)){
if($type == 'array') {
$script .= $name."[".$key."] = new Array();";
factory($name."[".$key."]", $val, 'array');
} else {
$script .= $name.".".$key." = new Array();";
factory($name.".".$key, $val, 'array');
}
} elseif(preg_match("/[a-zA-Z_][a-zA-Z0-9_]*/",$key) && $type == 'object') {
if(is_numeric($val)) $script .= $name.".".$key."=".$val.";";
else $script .= $name.".".$key."='".$val."';";
} elseif($type == 'array') {
if(!is_numeric($key)) $key = "'".$key."'";
if(is_numeric($val)) $script .= $name."[".$key."]=".$val.";";
else $script .= $name."[".$key."]='".$val."';";
}
}
}

// 사용법 setScriptVar(변수명지정,PHP변수);

// 일반변수
$market = "I Like Fruit!";
setScriptVar("market", $market);

// 배열
$fruit = array('Apple','Pineapple',array('Tomato','Banana'));
setScriptVar("fruit", $fruit);

// 오브젝트와 배열
$list->like = 'Pineapple';
$list->hate = 'Tomato';
$list->buy = array('Apple','Pineapple');
setScriptVar("list", $list);

// 변수타입
$num = array(100,'200');
setScriptVar("num",$num);
?>

<script>
//alert(market); // I Like Fruit!
//alert(fruit[0]+"/"+fruit[2][0]); // Apple/Tomato
//alert(list.like+"/"+list.buy[0]); // Pineapple/Apple
//alert(num[0] + 10); // 110
//alert(num[1] + 10); // 20010
</script>

첨부파일

php변수.txt (2 KB) 11회 2013-03-20 13:27
|
댓글을 작성하시려면 로그인이 필요합니다.

프로그램

+
제목 글쓴이 날짜 조회
12년 전 조회 4,457
12년 전 조회 9,349
12년 전 조회 4,889
12년 전 조회 4,519
12년 전 조회 2,105
12년 전 조회 2,151
12년 전 조회 3,907
12년 전 조회 4,745
12년 전 조회 5,128
12년 전 조회 2,378
12년 전 조회 2,042
12년 전 조회 1,617
12년 전 조회 1,692
12년 전 조회 3,491
12년 전 조회 3,470
12년 전 조회 2,002
12년 전 조회 3,531
12년 전 조회 2,020
12년 전 조회 9,301
12년 전 조회 2,271
12년 전 조회 4,491
12년 전 조회 7,150
12년 전 조회 2,301
12년 전 조회 3,570
12년 전 조회 1,305
12년 전 조회 2,004
12년 전 조회 1,886
12년 전 조회 2,061
12년 전 조회 4,046
12년 전 조회 1,816
12년 전 조회 2,567
12년 전 조회 2,006
12년 전 조회 1,643
12년 전 조회 2,066
12년 전 조회 2,820
12년 전 조회 2,105
12년 전 조회 1,937
12년 전 조회 2,460
12년 전 조회 1.5만
12년 전 조회 1,720
12년 전 조회 1,500
12년 전 조회 3,350
12년 전 조회 9,583
12년 전 조회 1,899
12년 전 조회 1,850
12년 전 조회 2,290
12년 전 조회 1,764
12년 전 조회 1,949
12년 전 조회 4,222
12년 전 조회 2,122
12년 전 조회 2,787
12년 전 조회 1,784
12년 전 조회 1,962
12년 전 조회 3,663
12년 전 조회 1,504
12년 전 조회 1,990
12년 전 조회 2,316
12년 전 조회 2,745
12년 전 조회 2,852
12년 전 조회 3,292
12년 전 조회 1,747
12년 전 조회 1,797
12년 전 조회 5,172
12년 전 조회 2,221
12년 전 조회 2,406
12년 전 조회 6,241
12년 전 조회 3,926
12년 전 조회 2,899
12년 전 조회 2,047
12년 전 조회 4,212
12년 전 조회 3,237
12년 전 조회 2,900
12년 전 조회 2,582
12년 전 조회 2,269
12년 전 조회 2,284
12년 전 조회 2,022
12년 전 조회 3,100
12년 전 조회 2,424
12년 전 조회 2,117
12년 전 조회 3,182
12년 전 조회 2,375
12년 전 조회 2,954
12년 전 조회 4,177
12년 전 조회 1,935
12년 전 조회 2,911
12년 전 조회 1,771
12년 전 조회 2,256
12년 전 조회 2,895
12년 전 조회 2,499
12년 전 조회 5,304
12년 전 조회 3,425
12년 전 조회 3,614
12년 전 조회 1,980
12년 전 조회 9,302
12년 전 조회 4,117
12년 전 조회 2,176
12년 전 조회 2,037
12년 전 조회 3,478
12년 전 조회 6,159
12년 전 조회 4,173