[thelist] PHP / javascript arrays problem

David Horn davidhorn at ticktockdesign.com
Fri Apr 16 10:40:46 CDT 2004


Hi all,

First time post to this list, so apologies if I'm a little longwinded!

I'm having problems creating javascript arrays from PHP.  I'm pretty  
new to both concepts, so please bear with me.

The PHP I'm using to generate the js looks like this:

<?php
$db = mysql_pconnect("localhost", "*****", "*****");
mysql_select_db('carsite');
$query1 = "SELECT modelName, makeID FROM makes GROUP BY makeID";
$result=mysql_query($query1);
echo "varStockModels = new Array(); \n";
echo "varStockModels[0] = new Array('');\n";

while ( $models=mysql_fetch_assoc($result) )
	{
	 echo "varStockModels[";
	 echo $models['makeID'];
	 echo "] =  new Array('','$models[modelName]'); \n";
	 }
	
?>

this generates the following array quite satisfactorily:

<script language="JavaScript">
varStockModels = new Array();
varStockModels[0] = new Array('');
varStockModels[1] =  new Array('','Xsara');
varStockModels[2] =  new Array('','Focus');
varStockModels[3] =  new Array('','MX5');
varStockModels[4] =  new Array('','midget');
varStockModels[5] =  new Array('','Clio');
varStockModels[6] =  new Array('','Octavia');
varStockModels[7] =  new Array('','SD40');
varStockModels[8] =  new Array('','XR20');
</script>

However, this array only seems to work on Windows browsers, and not on  
Mac IE / Safari / Opera.  When I hard code the array onto a page, it  
works satisfactorily in all browsers - which has really got me puzzled.

If anyone there can help me out with why, I'd really appreciate it.   
I've posted working (or not working) examples of this occurrence at the  
following pages:

http://www.ticktockdesign.com/cars/doesnotwork.php
http://www.ticktockdesign.com/cars/doeswork.php

I suspect the problem relies with my ham-fisted javascript at the top  
of each page, but I'm really not sure.

IF i get this cracked ... my next question is going to be how do I  
expand the PHP above to accommodate situations where there is more than  
one model per 'makeID', but one thing at a time I guess!

Anyway, I hope this is the right group to ask!  Many thanks in advance  
for your help,

David

------------------------------------------------------------------------ 
----
David Horn
Creative Director
www.TickTockDesign.com
Design.  On Time.
t. +44 (0) 795 876 9196
f. +44 (0) 870 429 2747


More information about the thelist mailing list