rubahfgouveia Posted August 22, 2010 Share Posted August 22, 2010 Hey all, What I want to do is send an array of PHP values into a variable I created in Javascript. PHP code: <? $result = mysql_query("SELECT A,B FROM USS"); $num_rows = mysql_numrows($result); for($i = 0; $i < $num_rows; $i++) { $nome_utilizador = mysql_fetch_row($result); $user = $nome_utilizador[0] . " ". $nome_utilizador[1]; $teste[] = $user; } ?> Then, right after this PHP code, i have the following JS code: <script type="text/javascript"> var contacts = [ {name:"<? echo $teste[0]?>",email:"ccc@ddd.com"}, {name:"fff",email:"fff@ccc.com"}]; </script> I managed to get one value printed into the var, but what I want is to create a cycle, inside the JS code, that would get all the values from my $teste[] array, and print it into the var I created. Hope I made myself explicit Thanks, Ruben Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted August 22, 2010 Share Posted August 22, 2010 Here's one way: After your loop: $data = json_encode($teste); Then: <script type="text/javascript"> var contacts = eval('(' + '<?php echo $data; ?>' + ')'); </script> Quote Link to comment Share on other sites More sharing options...
rubahfgouveia Posted August 24, 2010 Author Share Posted August 24, 2010 Hey, The solution you sent me won't really work, because I need the var in the following format: var contacts = [{name:"<? echo $teste[0]?>",email:"ccc@ddd.com"}, {name:"<? echo $teste[1]?>",email:"fff@ccc.com"}, ]; // etc, until the cycle stops In other words, the solution you gave me would provide something like: var contacts = ["value_1","value_2"] //etc When what I need to recieve in the var is: var contacts = [ {name:"value_1",email:"whatever@whatever.com"},{name:"value_2",email:"whatever2@whatever2.com"}] //etc Thanks, Ruben Quote Link to comment Share on other sites More sharing options...
Psycho Posted August 24, 2010 Share Posted August 24, 2010 The solution you sent me won't really work, because I need the var in the following format: Did you even try the solution? json_encod returns the values in exactly the same format as you just stated: "{key1:value1, key2:value2, key3:value3, ...}". Although you would want to "add them up" within a loop that extracts the records. Your query is only selecting fields "A" and "B", but you state you need data for name and email. If those fields really are called "A" & "B" then just change the names of the columns returned. Off topis: I wonder why I see so many examples of people using a numeric counter to iterrate through database results instead of just using a while loop. Sample code: $query = "SELECT `A` as `name', `B` as 'email' FROM USS"; $result = mysql_query($query); $jsVarsAry = array; while($user = mysql_fetch_assoc($result)) { $jsVarsAry = json_encode($user); } $jsVarsStr = implode(",\n", $jsVarsAry); <script type="text/javascript"> var contacts = '[<?php echo $jsVarsStr; ?>]'; </script> Not tested, so there may be some minor tweaking needed. Quote Link to comment 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.