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

· 12년 전 · 2251

<?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,580
12년 전 조회 9,466
12년 전 조회 5,052
12년 전 조회 4,659
12년 전 조회 2,240
12년 전 조회 2,275
12년 전 조회 4,025
12년 전 조회 4,868
12년 전 조회 5,259
12년 전 조회 2,498
12년 전 조회 2,178
12년 전 조회 1,749
12년 전 조회 1,827
12년 전 조회 3,626
12년 전 조회 3,614
12년 전 조회 2,135
12년 전 조회 3,668
12년 전 조회 2,168
12년 전 조회 9,317
12년 전 조회 2,400
12년 전 조회 4,642
12년 전 조회 7,286
12년 전 조회 2,446
12년 전 조회 3,705
12년 전 조회 1,427
12년 전 조회 2,137
12년 전 조회 2,026
12년 전 조회 2,187
12년 전 조회 4,181
12년 전 조회 1,946
12년 전 조회 2,703
12년 전 조회 2,137
12년 전 조회 1,778
12년 전 조회 2,211
12년 전 조회 2,942
12년 전 조회 2,220
12년 전 조회 2,058
12년 전 조회 2,588
12년 전 조회 1.6만
12년 전 조회 1,857
12년 전 조회 1,626
12년 전 조회 3,477
12년 전 조회 9,715
12년 전 조회 2,027
12년 전 조회 1,970
12년 전 조회 2,420
12년 전 조회 1,897
12년 전 조회 2,073
12년 전 조회 4,337
12년 전 조회 2,252
12년 전 조회 2,916
12년 전 조회 1,907
12년 전 조회 2,090
12년 전 조회 3,797
12년 전 조회 1,625
12년 전 조회 2,116
12년 전 조회 2,433
12년 전 조회 2,876
12년 전 조회 2,978
12년 전 조회 3,428
12년 전 조회 1,881
12년 전 조회 1,940
12년 전 조회 5,293
12년 전 조회 2,350
12년 전 조회 2,539
12년 전 조회 6,357
12년 전 조회 4,063
12년 전 조회 3,028
12년 전 조회 2,175
12년 전 조회 4,356
12년 전 조회 3,353
12년 전 조회 3,051
12년 전 조회 2,711
12년 전 조회 2,403
12년 전 조회 2,418
12년 전 조회 2,156
12년 전 조회 3,230
12년 전 조회 2,545
12년 전 조회 2,241
12년 전 조회 3,307
12년 전 조회 2,491
12년 전 조회 3,086
12년 전 조회 4,294
12년 전 조회 2,045
12년 전 조회 3,048
12년 전 조회 1,895
12년 전 조회 2,371
12년 전 조회 3,007
12년 전 조회 2,628
12년 전 조회 5,432
12년 전 조회 3,555
12년 전 조회 3,732
13년 전 조회 2,112
13년 전 조회 9,420
13년 전 조회 4,234
13년 전 조회 2,297
13년 전 조회 2,158
13년 전 조회 3,590
13년 전 조회 6,280
13년 전 조회 4,298