0

i stuck to attach counter-var (b) on text-variable (string) - not mathematical - Just to add counter(b) on data-var in JS...

Example:

<script type="text/javascript">    

<?php 
$i=0;
foreach ($sqldata as $data){
echo 'var data'.$i.' = 
Array("'.implode('", "', array_map('addslashes', $data)).'");';     
$i++;
}
echo 'var data_ges = '.$i.' ;';
?>

for (b=0; b<data_ges; b++){
document.writeln (data+b[1]); // ERROR LINE - How do i escape here ?
}

</script>

Thanks!

1
  • 1
    That there include the dreaded PHP code. Confusing, no? Commented Feb 19, 2012 at 23:53

3 Answers 3

1

You should just use array instead of trying to dynamically use a variable name :

<script type="text/javascript">    
  var data = [];
<?php 
$i=0;
foreach ($sqldata as $data){
echo 'data['.$i.'] = 
Array("'.implode('", "', array_map('addslashes', $data)).'");';     
$i++;
}
?>

for (b=0, l = data.length; b<l; b++){
document.writeln (data[b]); // ERROR LINE - How do i escape here ?
}

</script>
Sign up to request clarification or add additional context in comments.

Comments

1

You can use the json_encode function to create a javascript-object where you can easily iterate over. http://www.php.net/manual/en/function.json-encode.php

Comments

0

Your php is making a set of variables like data0, data1. One way to capture these back is to grab them off the this or global window object:

for (var i = 0; i < data_ges; ++i) {
  document.writeln(window['data' + i]);
}

It would probably be better to actually make an array called data in the php though:

var data = [];
<?php 
foreach ($sqldata as $data) {
    echo 'data.push(' … ');';
}    
?>

for (var i = 0; i < data.length; ++i) {
  document.writeln(data[i]);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.