Forum Moderators: coopster

Message Too Old, No Replies

creating javascript array error

         

jackvull

5:34 pm on Dec 20, 2007 (gmt 0)

10+ Year Member



I am trying to create a javascript through php code but the javascript error comes up with array.o is null:


<script language = "javascript" type="text/javascript">
var structure = new Array();
<?
$i = 0;
while ($row = mssql_fetch_assoc($resultStruc )) {
?>
structure[<?echo $i;?>][0] = <?echo $row['DivisionID'];?>;
structure[<?echo $i;?>][1] = <?echo $row['Symonds Div Level'];?>;
structure[<?echo $i;?>][2] = <?echo $row['Symonds Bus Unit Level'];?>;
structure[<?echo $i;?>][3] = <?echo $row['Symonds Regional Level'];?>;
structure[<?echo $i;?>][4] = <?echo $row['Cost Centre'];?>;
<?
$i++;
}

?>
</script>

mooger35

6:35 pm on Dec 20, 2007 (gmt 0)

10+ Year Member



I would echo the whole script rather than switching in and out.

<?php
echo "<script language = \"javascript\" type=\"text/javascript\">\r\n";
echo "var structure = new Array();\r\n";

$i = 0;
while ($row = mssql_fetch_assoc($resultStruc )) {
echo "structure[$i][0] = '".$row['DivisionID']."';\r\n";
echo "structure[$i][1] = '".$row['Symonds Div Level']."';\r\n";
echo "structure[$i][2] = '".$row['Symonds Bus Unit Level']."';\r\n";
echo "structure[$i][3] = '".$row['Symonds Regional Level']."';\r\n";
echo "structure[$i][4] = '".$row['Cost Centre']."';\r\n";
$i++;
}

echo "</script>\r\n";
?>

jackvull

9:09 pm on Dec 20, 2007 (gmt 0)

10+ Year Member



turns out the arrays in javascript are different.
php
[0][1]

javascript
[0,1]

Birdman

7:51 am on Dec 22, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi,

Actually, I think it may because you didn't specify Array() for the nested array.

<script language = "javascript" type="text/javascript">
var structure = new Array();
<?
$i = 0;
while ($row = mssql_fetch_assoc($resultStruc )) {
?>
structure[<?=$i?>] = new Array(
<?=$row['DivisionID']=?>,
<?=$row['Symonds Div Level']?>,
<?=$row['Symonds Bus Unit Level']?>,
<?=$row['Symonds Regional Level']?>,
<?=$row['Cost Centre']?>
);

<?
$i++;
}

?>
</script>