two dimensional array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cleary1981
    New Member
    • Jun 2008
    • 178

    two dimensional array

    Hi,

    I have a javascript function that reads in an array from php. Each record is split by a $ and each field is then split by a ^. I was wondering if anyone could help me get this data into a 2d array? Heres what I have so far.

    Code:
    function drawExisting() {
    	if (request.readyState ==4) {
    		var returned = request.responseText;
    		var recordSplit = returned.split("$");
    		var record = recordSplit[0];
    		alert(record);
    		var fieldSplit = record.split("^");
    		var object_name = fieldSplit[0];
    		var xpos = fieldSplit[1];
    		var ypos = fieldSplit[2];
    		var height = fieldSplit[3];
    		var width = fieldSplit[4];
    		// alert(object_name + " " + xpos + " " + ypos + " " + height + " " + width);
    		
    		}
    }
    This gives me the results for the first record
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Loop over recordSplit (an array) and set each fieldSplit (also an array) to an array variable.

    Comment

    • cleary1981
      New Member
      • Jun 2008
      • 178

      #3
      The problem that is throwing me is that you have to declare the size of the array. Can I declare an array without the size?

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        You don't need to declare the size of the array. You can declare it like this:
        [code=javascript]var arr = [];[/code]and that's it.

        Comment

        • cleary1981
          New Member
          • Jun 2008
          • 178

          #5
          Am I on the right track here?

          Code:
          function drawExisting() {
          	if (request.readyState ==4) {
          		var record = [];
          		var returned = request.responseText;
          		var recordSplit = returned.split("$");
          		for (i=0; i < record.length; i++) {
          			record = recordSplit[i];
          			var fieldSplit = record.split("^");
          			var object_name = fieldSplit[0];
          			var xpos = fieldSplit[1];
          			var ypos = fieldSplit[2];
          			var height = fieldSplit[3];
          			var width = fieldSplit[4];
          			alert(object_name + " " + xpos + " " + ypos + " " + height + " " + width);
          		}
          		
          		}
          }

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            Sort of. The loop should be on recordSplit, not record. Then use the push() method to add the fieldSplit array to record.

            Comment

            • cleary1981
              New Member
              • Jun 2008
              • 178

              #7
              I have cracked it. Thanks for your help all the same. Just took a while for the concept to snk in to my thick head. Heres my code.

              Code:
              function drawExisting() {
              	if (request.readyState ==4) {
              		cv = document.getElementById("canvas");
              		var returned = request.responseText;
              		var splitResult = returned.split("$");
              		for (var i=0;i<splitResult.length-1;i++) {
              			var record = splitResult[i];
              			// alert(record);
              			var splitRecord = record.split("^");
              				var newObject = document.createElement('div');
              				newObject.id=splitRecord[0];
              				newObject.innerHTML=splitRecord[0];
              				newObject.style.height=splitRecord[3]/10;
              				newObject.style.width=splitRecord[4]/10;
              				newObject.style.top=splitRecord[2];
              				newObject.style.left=splitRecord[1];
              				newObject.onmousedown=function(){grab(this);}
              				cv.appendChild(newObject);
              				
              		}
              		}
              }

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #8
                So you didn't need to use a two dimensional array in the end?

                Comment

                Working...