0

I have the following PHP code. It creates multiple variables using $a; for example: $numtoken1.

$sql ="SELECT token, dispositivo FROM dispositivos WHERE idcliente=1";
mysql_select_db('localiza');
$retval = mysql_query( $sql, $conn );
$num_rows = mysql_num_rows($retval);

while($row = mysql_fetch_array($retval, MYSQL_BOTH))
{
    $numtoken['$a']=$row['token'];
    $numdispositivo['$a']=$row['dispositivo'];
    $a=$a++;
}   

Using JavaScript, I want to call all the PHP variables using that code, but it only get the last $a number.

My question is: In a JavaScript loop, how can I dynamically insert the value for $a? Because in the above PHP, I have multiple values for $a.

var accessToken = "<?= $numtoken['$a']; ?>"a;
var deviceID = "<?= $numdispositivo['$a']; ?>";
4
  • remove the quotes from ['$a'] Commented Mar 6, 2015 at 15:13
  • your while loop keeps overwritting the data... do var_dump($numtoken) you will see... I think you'd want to remove the quotes from that $a inside the brakets Commented Mar 6, 2015 at 15:13
  • 1
    stop using mysql_ functions! they are deprecated! Commented Mar 6, 2015 at 15:15
  • k, but the problem is I have in php numtoken1,numtoken2,numtoken13... so I need if the javascript var accessToken = "<?= $numtoken['$a']; ?>"; insert automacally the $a value but that command "<?= $numtoken['$a']; ?>" is a php command, How can I change the $a in javascript and send it to php? – Commented Mar 6, 2015 at 16:03

2 Answers 2

2

I suggest JSON:

    var accesstoken = JSON.parse("<?= json_encode($numtoken); ?>");
    var deviceID = JSON.parse("<?= json_encode($numdispositivo); ?>");
Sign up to request clarification or add additional context in comments.

2 Comments

ok, but the problem is I have in php numtoken1,numtoken2,numtoken13... so I need if the javascript var accessToken = "<?= $numtoken['$a']; ?>"; insert automacally the $a value but that command "<?= $numtoken['$a']; ?>" is a php command, How can I change the $a in javascript and send it to php?
@RobertoBahia Using PHP to echo the JSON encoded sting into your javascript is a one way trick. If your issue is getting information from javascript back to PHP, you are going to need to look into making an AJAX call to a PHP processing script.
0

So the first thing I noticed when looking at this is that you are wrapping $a in single quotes (') instead of double quotes (") this means that instead of evaluating $a the key in the array will be the string "$a", so you'll be overwriting the value in each iteration of the while loop. See - http://php.net/manual/en/language.types.string.php#language.types.string.parsing for more info.

That being said the actual answer to your question varies, but the simplest method might be to use the json_encode function to convert your arrays into json objects.

var accessTokenArr = <?php print json_encode($numtoken); ?>
var deviceIdArr = <?php print json_encode($numdispositivo); ?>

Then you can iterate over the values in those arrays and do whatever you want to with the values. Fair warning - you may need to tweak that code a little as I haven't tested it to make sure it plays nicely.

2 Comments

So I bring hte information with that command: var deviceIdArr = <?php print json_encode($numdispositivo); ?>; I tried use subtr to get one value of array but is not possible. I tried too usinge var a = 0; var deviceID1=deviceIdArr[a]; but can't get the value. you know how can I get the fvalues from json_encode?
It's difficult to say as I don't know what the structure of your data is. What errors are you getting in the console? You can also see what the data looks like by doing console.log(accessTokenArr); or console.log(deviceIdArr);

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.