Jump to content

Sending PHP values into a Javascript var


rubahfgouveia

Recommended Posts

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

Link to comment
Share on other sites

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

 

 

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.