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

· 12년 전 · 1685

<?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년 전 조회 3,931
12년 전 조회 8,809
12년 전 조회 4,388
12년 전 조회 3,983
12년 전 조회 1,586
12년 전 조회 1,649
12년 전 조회 3,375
12년 전 조회 4,217
12년 전 조회 4,611
12년 전 조회 1,831
12년 전 조회 1,519
12년 전 조회 1,106
12년 전 조회 1,201
12년 전 조회 2,953
12년 전 조회 2,954
12년 전 조회 1,478
12년 전 조회 3,024
12년 전 조회 1,509
12년 전 조회 8,681
12년 전 조회 1,749
12년 전 조회 3,999
12년 전 조회 6,623
12년 전 조회 1,791
12년 전 조회 3,059
12년 전 조회 818
12년 전 조회 1,519
12년 전 조회 1,373
12년 전 조회 1,533
12년 전 조회 3,535
12년 전 조회 1,315
12년 전 조회 2,045
12년 전 조회 1,485
12년 전 조회 1,137
12년 전 조회 1,610
12년 전 조회 2,334
12년 전 조회 1,606
12년 전 조회 1,511
12년 전 조회 2,027
12년 전 조회 1.5만
12년 전 조회 1,290
12년 전 조회 1,106
12년 전 조회 2,947
12년 전 조회 9,174
12년 전 조회 1,491
12년 전 조회 1,402
12년 전 조회 1,853
12년 전 조회 1,367
12년 전 조회 1,503
12년 전 조회 3,778
12년 전 조회 1,686
12년 전 조회 2,363
12년 전 조회 1,389
12년 전 조회 1,540
12년 전 조회 3,248
12년 전 조회 1,100
12년 전 조회 1,571
12년 전 조회 1,827
12년 전 조회 2,260
12년 전 조회 2,372
12년 전 조회 2,792
12년 전 조회 1,273
12년 전 조회 1,295
12년 전 조회 4,678
12년 전 조회 1,720
12년 전 조회 1,871
12년 전 조회 5,731
12년 전 조회 3,465
12년 전 조회 2,371
12년 전 조회 1,525
12년 전 조회 3,712
12년 전 조회 2,733
12년 전 조회 2,444
12년 전 조회 2,074
12년 전 조회 1,763
12년 전 조회 1,759
12년 전 조회 1,508
12년 전 조회 2,631
12년 전 조회 1,909
12년 전 조회 1,583
12년 전 조회 2,670
12년 전 조회 1,842
12년 전 조회 2,458
12년 전 조회 3,681
12년 전 조회 1,466
12년 전 조회 2,426
12년 전 조회 1,275
12년 전 조회 1,764
12년 전 조회 2,339
12년 전 조회 1,977
12년 전 조회 4,789
12년 전 조회 2,890
12년 전 조회 3,070
12년 전 조회 1,434
12년 전 조회 8,753
12년 전 조회 3,580
12년 전 조회 1,654
12년 전 조회 1,523
12년 전 조회 2,909
12년 전 조회 5,582
12년 전 조회 3,637