...

View Full Version : Stupid ? - Arrays



ruggeddesign
10-26-2006, 11:59 PM
I am trying to build a JavaScript array out of a PHP query. Here is a sample of the code that is being generated:



<script type="text/javascript">
var quoteData=new Array(3);
quoteData[0]["quoteId"]="1160164423";
quoteData[0]["revisionNum"]="1";
quoteData[0]["pubTitle"]="Test Job 1";
quoteData[0]["briefJobDescrip"]="This is just a sample of text to demonstrate the general shape of a brief description. This isn't too brief,";
quoteData[0]["estNeeded"]="0000-00-00";
</script>


Firefox gives me the error: "quoteData[0] has no properties" - and points my attention to the line:



var quoteData=new Array(3);


Is there something extremely obvious that I am doing wrong here? JS embarrasses me everyday... I think that I'm getting a grip on the more complicated aspects of the language and then all of a sudden I can't even generate an array properly.

Sorry for the stupid question.

DougMck
10-27-2006, 12:51 AM
I think you have to declare the 2nd dimention of the quoteData array as an array.

e.g.



var quoteData=new Array(3);
quoteData[0] = new Array();


Now you should be able to assign the values to quoteData[0]["etc"]

1212jtraceur
10-27-2006, 04:22 AM
I'm not very familiar with JS Arrays, but that sounds right...

Beagle
10-27-2006, 03:21 PM
Well, you don't necessarily have to define it as an array, but you need to define it as something.

If you're going to use named properties instead of numeric indexes, I would recommend making it a generic object:



var x = new Array(3);
x[0] = {};
x[0].newProperty = newValue;
x[0].secondProperty = secondValue;


The reason your code is failing is because quoteData[0] is either undefined or null. Try alerting it and you'll see.

ruggeddesign
10-27-2006, 04:52 PM
Thank each of you for your help. In the end, Beagle's advice seemed to work best in this situation. This site is very useful.

Kor
10-27-2006, 05:20 PM
Auxilliary advice. Most of the time, there is no use to set from the begining the lenght of your array. If a dynamic one, you might have problems. Let the array have it's own length, whichever might be:

var x = [];
.......

ruggeddesign
10-27-2006, 05:35 PM
Auxilliary advice. Most of the time, there is no use to set from the begining the lenght of your array. If a dynamic one, you might have problems. Let the array have it's own length, whichever might be:

var x = [];
.......

Thanks, you're right. I probably would have been ok, I was generating the array out of PHP and was using a mysql_num_rows() to determine my array length - but when I was doing some other reading I decided to take that out in case it was causing difficulties.

I do have a bit of a problem with this new array though. I basically have this setup (the following code is what PHP echos onto the page in JS tags):



<script type="text/javascript">
quoteData = new Array();
quoteData[0] = {};
quoteData[0].quoteId="1160164423";
quoteData[0].revisionNum="1";
quoteData[0].pubTitle="Test Job 1";
quoteData[0].briefJobDescrip="This is just a sample of text to demonstrate the general shape of a brief description. This isn't too brief,";
quoteData[0].estNeeded="0000-00-00";
quoteData[0].salesRep="nunya";
quoteData[1] = {};
quoteData[1].quoteId="1161961764";
quoteData[1].revisionNum="1";
quoteData[1].pubTitle="";
quoteData[1].briefJobDescrip=" ";
quoteData[1].estNeeded="0000-00-00";
quoteData[1].salesRep="nunya";
quoteData[2] = {};
quoteData[2].quoteId="1161961793";
quoteData[2].revisionNum="1";
quoteData[2].pubTitle="";
quoteData[2].briefJobDescrip="Job Description is fun";
quoteData[2].estNeeded="0000-00-00";
quoteData[2].salesRep="nunya";
</script>


So, I would think that quoteData has a length of 3 (0, 1, and 2) - but when I alert(quoteData.length) I get a value of 16. Why?

*edit*
Alright, I reworked it so that my array would be accessed like this: quoteData[0]['salesRep'] and it works okay now, thank you everyone.

Kor
10-27-2006, 05:45 PM
because you have mixted orderd arrays with un-ordered arrays (objects)

There might be different ways to manage that (JSON, new Object(), prototypes...). Anyway, you should create the object outside the ordered array.

Kor
10-27-2006, 05:47 PM
why not create a global object? Do you need the length by all means?
See:
www.json.org

ruggeddesign
10-27-2006, 05:52 PM
because you have mixted orderd arrays with un-ordered arrays (objects)

There might be different ways to manage that (JSON, new Object(), prototypes...). Anyway, you should create the object outside the ordered array.

Ok, sorry, I'm going to post the actual source . . .


<?php

$userName = $_SESSION['username'];
$bSQL = "SELECT * FROM quoteForm WHERE salesRep='".$userName."'";
$bQRY = mysql_query($bSQL) or die(mysql_error());
$bNUM = mysql_num_rows($bQRY);

$i = 0;

echo "quoteData = new Array(); \n";

while($bRES = mysql_fetch_array($bQRY))
{
echo "quoteData[$i] = {}; \n";

while(list($key,$value)=each($bRES))
{
if(!is_numeric($key))
{
// echo "x[$i] = {}; \n";
echo "insideData = new Array(); \n";
echo "insideData[\"$key\"]=\"".$value."\"; \n";
echo "quoteData[$i]=insideData; \n";

//echo "quoteData[$i][\"$key\"]=\"".$value."\"; \n";
/*
echo "quoteData[$i] = new Array(); \n";
echo "quoteData[$i][\"".$key."\"]=\"".$value."\"; \n";
*/
}
}

$i++;
}

?>


The problem being, that now, even though I declare the second array separately, I am still getting a length of 16. I am cool with this array, it seems to be working, but if I cannot access the array length properly it makes it hard to loop through the array and process the data efficiently. I suppose I could declare quoteDataArrayLength = $bNUM; but if there is an actual problem with my array, I'd be interested in knowing how to fix it so that the length property reflects the value I'm looking for... which would be 3.

Thanks again.

ruggeddesign
10-27-2006, 05:58 PM
OK, I'm a bit of an idiot sometimes. Or maybe I thought it was april 20th this morning, because what happened is that while I was fixing the initial problem, I accidentally inserted another 13 entries into my database. And, believe it or not, the reason it was alerting the value of 16 as the length was because the array's length is now 16, not because there was a problem.

Which, is a good thing, because nobody likes being confused in the morning.

Thanks again everybody for your help, each solution worked perfectly.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum