Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    New Coder
    Join Date
    Aug 2006
    Posts
    66
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Stupid ? - Arrays

    I am trying to build a JavaScript array out of a PHP query. Here is a sample of the code that is being generated:

    Code:
    <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:

    Code:
    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.

  • #2
    New Coder
    Join Date
    May 2005
    Location
    New Zealand
    Posts
    76
    Thanks
    0
    Thanked 6 Times in 6 Posts
    I think you have to declare the 2nd dimention of the quoteData array as an array.

    e.g.

    Code:
    var quoteData=new Array(3);
    quoteData[0] = new Array();
    Now you should be able to assign the values to quoteData[0]["etc"]

  • #3
    Regular Coder
    Join Date
    Oct 2006
    Posts
    206
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I'm not very familiar with JS Arrays, but that sounds right...

  • #4
    Senior Coder
    Join Date
    Jul 2005
    Location
    New York, NY
    Posts
    1,084
    Thanks
    4
    Thanked 19 Times in 19 Posts
    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:

    Code:
    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.

  • #5
    New Coder
    Join Date
    Aug 2006
    Posts
    66
    Thanks
    0
    Thanked 2 Times in 2 Posts
    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.

  • #6
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    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 = [];
    .......
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #7
    New Coder
    Join Date
    Aug 2006
    Posts
    66
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by Kor View Post
    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):

    Code:
    <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.
    Last edited by ruggeddesign; 10-27-2006 at 05:46 PM.

  • #8
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    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
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #9
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    why not create a global object? Do you need the length by all means?
    See:
    www.json.org
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #10
    New Coder
    Join Date
    Aug 2006
    Posts
    66
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by Kor View Post
    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 . . .
    Code:
    <?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.

  • #11
    New Coder
    Join Date
    Aug 2006
    Posts
    66
    Thanks
    0
    Thanked 2 Times in 2 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •