0
어떻게 그렇게하는지 잘 모르겠습니다. 하지만 내 while 루프에 데이터 (1,2,3,4, ...)로 자바 스크립트 변수를 저장하고 싶습니다. 어떤 도움을 주셔서 감사합니다! 감사합니다while 루프 내 변수 저장
$id = 0;
while($rows = mysql_fetch_array($data)){
$id = $id + 1;
$data = $rows['data'];
?>
<script>
var id = '<?php echo $id; ?>'; //1,2,3,4,5,6,...
var data = '<?php echo $id; ?>'; // Example - Data 1 = Frog, Data 2 = Bird
// So here I want to sava variables so the name will be
// var "data+'id'";
// And the output is data for that row
</script>
<?php
}
?>
//And here outside I want to use the variables like this:
document.write(data1); writes out "Frog"
document.write(data2); writes out "Pig"
ID가 증가하는 경우 JS에서 Array를 사용하지 않는 이유는 무엇입니까? –
왜 여러 개의 '
WHILE LOOP 바로 앞에
$data = new Array();
을 선언하면 WHILE LOOP에서$data['id'][$id]= $id
으로 모든 IDS를 수집 할 수 있습니다.
이 작업을 수행 :$data['data'][$id]= $row['data']
을 아래에 추가하여 원하는 각data
도 수집하십시오. 스크립트 태그에var data = <?php echo '['.join(',', $data['data']).']' ?> //--> ['cat', 'dog', 'cow', 'sheep']
var id = <?php echo '['.join(',', $data['id']).']' ?> //will create --> [1,2,3,4,5,6,7]
지금 당신은 자바 스크립트 배열 생성 예에서 변수에 액세스 할 수 있습니다.$data[0] // 'cat'
출처
2013-10-21 08:26:19 Knights