Forum Moderators: coopster

Message Too Old, No Replies

Dynamic Arrays

         

malcolmcroucher

9:17 am on Sep 8, 2008 (gmt 0)

10+ Year Member



Hi there

Okay im trying to create an array that populates dynamically .

Ive searched around but the tutorials don't go into such depth .

What i have :

Database with a list of cities , continents and countries with citid , countryid and continent

e.g

Cape town 001 south africa 001 africa 4

what im trying to do is

create an array like the following :

$cities[1][1] : [south africa ] [ Cape Town]
$cities[1][2] : [south africa ] [ durban]
$cities[1][3] : [south africa ] [ JHB]

I have tried usuing for loops but i get problems with the looping and it loops forever.

here is what ive done : It is wrong i know.

my problem is what do i do when i know i have finshed with a city , i.e there are only 3 cities ?

<?php
$sql1="SELECT * FROM city ORDER BY Countryid";
$query = mysql_query($sql1);

while ($row1 = mysql_fetch_array($query))
{
for ($i=1;$i<=210;$i++)
{
$cities[$i][0]=$row['country'];

for ($j=1;$j<=20;$j++)
{$cities[$i][$j]=$row1['city'];
Echo $cities[$i][$j] ;

}

Echo $i;
}

}

?>

malcolmcroucher

1:12 pm on Sep 8, 2008 (gmt 0)

10+ Year Member



okay i got it working to a certain extent in php but i have one question :

how do you pass variables between javascript and php ?

omoutop

2:21 pm on Sep 8, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



based on your first post, i assume you are trying to create a combo-box where php populates 2,3 or more drop-down lists and javascript just filters them for user?

If that is the case:
create in php your java array, as it would be used in the javascript
example:

$my_java_array = "CityArray[CityCounter] = new Array(".$counrry_id.",".$city_id.",'".$city_name."')\n";

Now inside javascript echo this variable $my_java_array.
So you would have something like:
<script type="text/JavaScript">
<? echo $my_java_array?>
// some more javascript code
</script>

This should work.

Other than that you can pass php variables easily to javascript
One way:
in PHP: $a = 12;
and in javascript you do: var a=<? echo $a;?>;

malcolmcroucher

4:57 pm on Sep 8, 2008 (gmt 0)

10+ Year Member



Thanks that helps a ton.