0

This should be bone simple, and as usual, I'm probably overlooking something due to my less-than-thorough way of teaching myself code. Hopefully this makes sense...

In a php loop, I have this:

var key = <?php echo json_encode($key); ?>;
var id<?php echo $key;?> = <?php echo json_encode($value['Claim']['id']); ?>;
var point<?php echo $key;?> = <?php echo json_encode($value['Claim']['point']); ?>;

Which evaluates just as I want it. So id2 equals whatever id2 is supposed to be, etc.

Then later I need to call those variables, like this:

var takeurl = '/api/response.json?point='+ point + '&id=' + id;

Except I want "point" and "id" to be the value of "point2" or "point4" or, in shorthand, point+key.

If I write it as

var takeurl = '/api/response.json?point='+ point + key + '&id=' + id;

it returns the value of "point" plus the value of "key," not the value of point2 or point3.

Then I realise that at the very beginning, when declaring those values, I shouldn't have to hit php all the time. I should be able to do something like:

var key = <?php echo json_encode($key); ?>;
var id[key] = <?php echo json_encode($value['Claim']['id']); ?>;
var point{"key"} = <?php echo json_encode($value['Claim']['point']); ?>;

And then create the new variable using the same convention. I've read similar questions where the answer involves using arrays, but again, this is all in a larger php loop, so I don't see that fitting the situation here.

Sorry, I hope this was clear. I'm earning my username on this one. Ideas?

EDITED TO ADD: The problem is a little different than what I've explained above. My code is apparently pulling the values of the final variables that appear in the loop (as if I weren't defining them by a 'key' distinction at all, but just defining "point" five times). I fear I'm not making sense at all. Here's some code so you can see what's going on: codepad.org/ETX5dI6C

What I need is, for example, when clicking the fourth link on the page, for the values for id4 and point4 to be pulled. Instead, no matter what link I click, it passes the variables for id5 and point5, which were defined last. I don't get it?

5
  • What is your question? Commented Mar 23, 2013 at 1:48
  • 1
    Just json encode the entire structure at once and access it in js, what you are doing is almost like writing JSON by hand. Commented Mar 23, 2013 at 1:49
  • You can do var takeurl = '/api/response.json?point='+ point.toString() + key.toString() + '&id=' + id; or var takeurl = '/api/response.json?point='+ point + '' + key + '&id=' + id; Commented Mar 23, 2013 at 1:50
  • Ryan: the question is... how can I access the variables I established in my takeurl variable? Commented Mar 23, 2013 at 1:51
  • @dilettante - SO hint, prefix name with @ Commented Mar 23, 2013 at 1:53

2 Answers 2

1

It is a bit nasty but you can access them like this:

window['point' + key]

Since these are global variables, they are in fact properties of the window object, and as such they can be referred using that bracket syntax.

Though this is equivalent to window.point5 when key == 5, note that window.point+key would not work.

So full solution would become:

var takeurl = '/api/response.json?point='+ window['point' + key] + '&id=' + window['id' + key];
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks. It's not evaluating, though. It's returning the error: ReferenceError: val is not defined
It sounds to as if you are referring to a variable called "val" somewhere in your code and that that variable is undefined...
Can you create a fiddle at jsfiddle.net with the relevant js lines? Or include them in the question. I'm sure we can work it out that way.
Oh, you know. This is... weird. So, your answer was right. The problem is entirely different. It's apparently pulling the values of the last variables that appear in the loop (as if I weren't defining them by a 'key' distinction at all, but just defining "point" five times. God, I fear I'm not making sense at all. Here's some code so you can see what's going on: codepad.org/ETX5dI6C
Checked because the initial question was answered. Thank you.
|
0

Your problem is that you are creating a string point + key evaluates to the value of point plus the value of key. Put what you want is the value of "point" plus key.

so what you have to do is dynamically name the varaible that you assigned to the window object:

window[ "point" + key ]

this will (given the key is let's say 3) evaluate to window.point3 which is what you need.

1 Comment

Thank you... so I created a couple of new variables: var newpoint = window["point" + key]; then modified takeurl to read: var takeurl = '/api/response.json?point='+ newpoint + '&id=' + id; and am now getting a "val is not defined" error. And am confusing myself more...

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.