Jump to content

how to make multidimensional arrays toss to javascript?


hockey97

Recommended Posts

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?

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.

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.

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>

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?

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>';
    };  
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.