Join the Stack Overflow Community
Stack Overflow is a community of 6.6 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

Although I have seen this question addressed in multiple posts not only here but on other forums, I am wanting some more basic information that pertains to the task I have at hand as each programmer has slightly different needs for this particular function.

For right now, I simply want to pull x-amount of data from my database (this is within my area of expertise as I have been working with these languages for a few years now...I also use mysqli extension) and then place that data in a JavaScript array.

At that point, I'll display the information in the JS array to suit a number of purposes I have in mind.

Writing standards-compliant code is important to me so I don't mind taking the time to learn the best way to accomplish these tasks.

I have seen mentions of JSON and other techniques. I'm not looking to anything dreadfully complex here, just populating the JS array from my DB.

Any and all help will be sincerely appreciated.

share|improve this question

Populating any JS variable from PHP usually involves this kind of construct:

$myvar = array('hello world');

?>
<script type="text/javascript">
var myvar = <?php echo json_encode($myvar); ?>;
</script>
share|improve this answer
    
Jack, many thanks for that. Very concise and to the point. I will give this a whirl and post any new questions I have. Thanks! - wordman – wordman Apr 30 '12 at 22:35
    
Glad to help :) – Ja͢ck Apr 30 '12 at 22:38
    
Jack, many thanks for the quick follow-up. One other thought: Most of the applications I have in mind for this technique will be pulling an array with anywhere from say, 6 to 50 items. I assume I build a loop inside the JS script around your 'var myvar' code to load the contents of the PHP array into the JS array? (I hope that made sense). Thanks! - wordman – wordman Apr 30 '12 at 22:48
    
Right, you could write a function that takes an array with the keys becoming the JS variable name and the values becoming the contents – Ja͢ck Apr 30 '12 at 23:15

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.