I am running through a MySQL database using PHP and extracting all of the makes of cars in that database and trying to create a JavaScript array named after each make. Then I am trying to fill each array will all of the models for that make. When I just try to display each Make and Model in an alert box it seems to come up right. And my arrays seem to be getting created but they won’t populate with the models (even though my alert msg shows the loops and everything is working).
[code=php]
while ($row = mysql_fetch_array($result)) {
$i=0;
//Used so each model is added to the array starting at 0 and ++
$make = $row[‘make’];//PHP var used to extract data from MySQL
print(“<script language = ‘javascript’>var $make=new Array();</script>”);
//Create array
$sql1 = “SELECT DISTINCT (model) FROM $table_name WHERE make=’$make’ ORDER BY model”;
//Extract models from particular make
$result1 = @mysql_query($sql1,$connection) or die(mysql_error());
while ($row1 = mysql_fetch_array($result1)) {
$model = $row1[‘model’];
//PHP var used to extract data from MySQL
//print(“<script language = ‘javascript’> alert(‘$make ‘+’$model’+’ $i’);</script>”);
//If uncommented shows correct data in alert msg
print(“<script language = ‘javascript’>$make[$i]=’$model’;</script>”);
//The key, trying to add $model to array named $make at index $i
//print(“<script language = ‘javascript’>myCars[$i]=’$model’+’ $make’;</script>”);
/*If uncommented shows that adding to array works if it is a statically named array instead of dynamically named based on variable*/
$i++;
}
Haven’t used JavaScript since a college class a couple of years ago and haven’t used it in the real world for even longer. Never with PHP like this. But I am working on this marketing site and need to be able to list products by type according to each brand and as they can be added to a MySQL database it needs to be dynamic. I have the whole thing working by stepping through separate pages using PHP to populate select fields with the data but I want to be able to dynamically add the content to select field using JavaScript so it can all be done on one page and thought that if I could create these arrays on page load then the right information could be selected client side.
Thanks