Forum Moderators: coopster
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;
}
}
?>
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;?>;