0

I need some help! Im retrieving some values from mySQL database using an external 'grabber'.

<?php 
    $datapiechart = file_get_contents("url which retrieves the values from MySQL"); 
?>

Which results in:

[{ "Name1": 62, "Name2": 42, "Name3": 19, "Name4": 7, "Name5": 6, "Name6": 4, "Name7": 1, "Name8": 4, "Name9": 3, "Name10": 1, "Name11": 1, "Name12": 0 }]

Then I want to select the values in this array.

<SCRIPT>
       dataObjectdatapiechart = <?php echo $datapiechart; ?>
</SCRIPT>

<script>  dataObjectdatapiechart.Name1</script>

I don't get whats going wrong here.

2
  • Please provide the error you are getting, we can't know what's wrong if you don't state it. Commented Jan 16, 2014 at 11:12
  • use array dataObjectdatapiechart[0],dataObjectdatapiechart[1] etc... Commented Jan 16, 2014 at 11:12

2 Answers 2

1

dataObjectdatapiechart is an array (with only one element), so you need to access it's contents using an indexer:

var item = dataObjectdatapiechart[0]; // Retrieve the object from the array
var name1 = item.Name1;
var name2 = item.Name2;
var name3 = item.Name3;
//etc.
Sign up to request clarification or add additional context in comments.

Comments

0

Use

dataObjectdatapiechart[0].Name1

The Object { "Name1": 62, "Name2": 42, "Name3": 19, "Name4": 7, "Name5": 6, "Name6": 4, "Name7": 1, "Name8": 4, "Name9": 3, "Name10": 1, "Name11": 1, "Name12": 0 }

is at 0th position of the array.

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.