hockey97 Posted August 5, 2011 Share Posted August 5, 2011 Hi, I want to know how to make multidimensional arrays and toss it to javascript. here is my example. Lets say our website sells fruit. I store fruit names and prices in a database. I then have html code and javascript code to make a select html tag. I need to take the names from the database and prices from the database. I then need to populate the select tag. yet with javascript I need to generate more then 1 select tags with a use of a button. that way the customer at check out can buy more then 1 fruit. So they are not limited to just 1 fruit to be bought at a time. how can one do this? Quote Link to comment https://forums.phpfreaks.com/topic/243875-how-to-make-multidimensional-arrays-toss-to-javascript/ Share on other sites More sharing options...
phpSensei Posted August 5, 2011 Share Posted August 5, 2011 Do you want to learn how to make the arrays themselves or how to process them in Javascript? Quote Link to comment https://forums.phpfreaks.com/topic/243875-how-to-make-multidimensional-arrays-toss-to-javascript/#findComment-1252266 Share on other sites More sharing options...
hockey97 Posted August 5, 2011 Author Share Posted August 5, 2011 I know how to make static arrays and multi-dimensional arrays. I don't know exactly how I can grab data from a database to generate the array to make a list. then toss it from php to javascript. because I need to use javascript to generate more then 1 select tag. based from a button click. so when you click this button it will make more select tags. Yet, have the same list. like my example is a list of fruits... each select tag that been created will have these fruit names so the customer can select more then 1 fruit and can be different fruits as well. I would say both. like I never made an array using variables inside the array. I only made a static array before where you do things by hand. but I want to populate the array with what is stored in the database. I hope this makes any sense. Quote Link to comment https://forums.phpfreaks.com/topic/243875-how-to-make-multidimensional-arrays-toss-to-javascript/#findComment-1252271 Share on other sites More sharing options...
phpSensei Posted August 5, 2011 Share Posted August 5, 2011 You can pass PHP variables to Javascript. <?php $array = array('1','2','3','4'); ?> <script> testVar = '<?php print_r($array); ?>'; alert(testVar); </script> You won't be able to use a javascript loop to break down that array since its just a string, but what you can do is create a javascript ARRAY and put it inside a PHP LOOP and pass the database variables to it. Quote Link to comment https://forums.phpfreaks.com/topic/243875-how-to-make-multidimensional-arrays-toss-to-javascript/#findComment-1252278 Share on other sites More sharing options...
hockey97 Posted August 5, 2011 Author Share Posted August 5, 2011 how would you do that? I tried to do this just now but it's not working. I am getting a empty select tag. how would I use variables inside the array instead of hand writting the content? Quote Link to comment https://forums.phpfreaks.com/topic/243875-how-to-make-multidimensional-arrays-toss-to-javascript/#findComment-1252297 Share on other sites More sharing options...
phpSensei Posted August 5, 2011 Share Posted August 5, 2011 Post your code and what you tried to do so far. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/243875-how-to-make-multidimensional-arrays-toss-to-javascript/#findComment-1252300 Share on other sites More sharing options...
trq Posted August 5, 2011 Share Posted August 5, 2011 It's no different to outputting any html markup. <?php $a = array('foo' => 'bar', 'bob' => 'boo'); echo '<script>'; echo 'var a = [];'; foreach ($a as $k => $v) { echo " a[$k] = $v;"; } echo '</script>'; Quote Link to comment https://forums.phpfreaks.com/topic/243875-how-to-make-multidimensional-arrays-toss-to-javascript/#findComment-1252302 Share on other sites More sharing options...
hockey97 Posted August 5, 2011 Author Share Posted August 5, 2011 here is my code: error it's javascript inside a php file. so you will see echo it's just because it's inside php. I used a while loop. It generates the code. Quote Link to comment https://forums.phpfreaks.com/topic/243875-how-to-make-multidimensional-arrays-toss-to-javascript/#findComment-1252305 Share on other sites More sharing options...
phpSensei Posted August 5, 2011 Share Posted August 5, 2011 edit: Refer to Thorpe's example. Quote Link to comment https://forums.phpfreaks.com/topic/243875-how-to-make-multidimensional-arrays-toss-to-javascript/#findComment-1252307 Share on other sites More sharing options...
trq Posted August 5, 2011 Share Posted August 5, 2011 Your code will do nothing but produce an error Quote Link to comment https://forums.phpfreaks.com/topic/243875-how-to-make-multidimensional-arrays-toss-to-javascript/#findComment-1252308 Share on other sites More sharing options...
phpSensei Posted August 5, 2011 Share Posted August 5, 2011 Heres what my code printed <script> testVar = 'Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 ) '; </script> Just to show you OP how the PHP variable implemented inside Javascript. You just need to modify the quotations correctly, and the \n. Again Thorpe's example is the best one see ... Alerts 'Array([0] => 1[1] => 2[2] => 3[3] => 4)' <script> testVar = " Array([0] => 1[1] => 2[2] => 3[3] => 4)"; </script> Quote Link to comment https://forums.phpfreaks.com/topic/243875-how-to-make-multidimensional-arrays-toss-to-javascript/#findComment-1252313 Share on other sites More sharing options...
hockey97 Posted August 5, 2011 Author Share Posted August 5, 2011 but then how do you use variables inside the arrays? like all examples you showed where static arrays.. meaning you had to hand code it yourself. how can you populate the array via variables? Quote Link to comment https://forums.phpfreaks.com/topic/243875-how-to-make-multidimensional-arrays-toss-to-javascript/#findComment-1252335 Share on other sites More sharing options...
phpSensei Posted August 5, 2011 Share Posted August 5, 2011 Thorpe's example populates the var a with a supplied array from PHP. <?php $a = array('foo' => 'bar', 'bob' => 'boo'); echo '<script>'; echo 'var a = [];'; foreach ($a as $k => $v) { echo " a[$k] = $v;"; } echo '</script>'; Quote Link to comment https://forums.phpfreaks.com/topic/243875-how-to-make-multidimensional-arrays-toss-to-javascript/#findComment-1252336 Share on other sites More sharing options...
hockey97 Posted August 5, 2011 Author Share Posted August 5, 2011 no, I am saying in that $a variable... if inside that array.. that bob and boo if you replace them with variables... how can you instead of directly writting the stuff... how can you populate the array.... like if I got fruit names in my database... how would you grab them from the database and then put thing inside an array? Quote Link to comment https://forums.phpfreaks.com/topic/243875-how-to-make-multidimensional-arrays-toss-to-javascript/#findComment-1252342 Share on other sites More sharing options...
phpSensei Posted August 5, 2011 Share Posted August 5, 2011 Your not paying attention to the examples man. Just create a sql query, fetch the data and populate "a". then in Javascript you have an array full of values from your database. Quote Link to comment https://forums.phpfreaks.com/topic/243875-how-to-make-multidimensional-arrays-toss-to-javascript/#findComment-1252343 Share on other sites More sharing options...
hockey97 Posted August 5, 2011 Author Share Posted August 5, 2011 so if be like this? $result ---> this be what mysql spit out. $a = array('foo' => $result); that be how I assign or populate the array from a database? Quote Link to comment https://forums.phpfreaks.com/topic/243875-how-to-make-multidimensional-arrays-toss-to-javascript/#findComment-1252344 Share on other sites More sharing options...
trq Posted August 5, 2011 Share Posted August 5, 2011 Assuming $result is the result of a call to mysql_query. No. You would need to loop through that result. <?php $sql = "SELECT a, b FROM table;"; if ($result = mysql_query($sql)) { if (mysql_num_rows($result)) { echo '<script>'; echo 'var a = [];'; while ($row = mysql_fetch_assoc($result)) { echo " a[{$row['a']}] = '{$row['b']}';" } echo '</script>'; }; } Quote Link to comment https://forums.phpfreaks.com/topic/243875-how-to-make-multidimensional-arrays-toss-to-javascript/#findComment-1252346 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.