Jump to content

[SOLVED] two dimensional array


cleary1981

Recommended Posts

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.

 

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);
        
        }
}

Link to comment
Share on other sites

Each record is a number in the outer array (phpArray[n]) and each field for that record is a number in the inner array (phpArray[n][m]).

 

This would show the way the array is tiered:

var strHTML = "";
for (var i=0;i<phpArray.length;i++)
{
	strHTML += phpArray[i] + "<div style=\"margin-left:20px\">";
	for (var j=0;j<phpArray[i].length;j++)
		strHTML += phpArray[i][j] + "<br>";
	strHTML += "</div>";
}
document.body.innerHTML = strHTML;

 

Although, I'm thinking now that the way I setup the array might not work with the way your array string is setup. Can you give me an example of the array string? And maybe an example of how you want the array to look?

Link to comment
Share on other sites

This is a sample of the data that is coming from php

 

e6^640^120^400^600$e5^640^80^400^600$e4^580^200^400^600$e3^580^160^400^600$e2^580^120^400^600$e1^580^80^400^600$w3^520^80^1400^600$w2^460^80^1400^600$w1^400^80^1400^600$q8^320^200^600^800$q7^320^140^600^800$q6^320^80^600^800$q5^240^200^600^800$q4^240^140^600^800$q3^240^80^600^800$q2^140^80^1000^1000$q1^140^180^1000^1000$e7^640^160^400^600$e8^640^200^400^600$v1^300^180^1000^1000$v2^200^180^1000^1000$v3^200^80^1000^1000$v4^300^80^1000^1000$v5^400^80^1000^1000$v6^400^180^1000^1000$v7^100^80^1000^1000$v8^100^180^1000^1000$huy66^220^100^400^600$ju444^160^120^600^800$nj776^240^120^600^800$

 

The format of the records being {object_name, xpos, ypos, height, width}. I will be using this function to generate a div with these properties.

 

 

Link to comment
Share on other sites

I will explain what I am doing with the data a little further. Each recor seperated by $ has 5 fields. These are {object_name, xpos, ypos, height, width}. For each record I wish to create a div and assign the five attributes as follows. Canvas is the Div I am using to ontain all these new divs.

 

	cv = document.getElementById("canvas");
var newObject = document.createElement('div');
newObject.id=object_name;
newObject.innerHTML=object_name;
newObject.style.height=height;
newObject.style.width=width;
newObject.style.top=xpos;
newObject.style.left=ypos;
cv.appendChild(newObject);

Link to comment
Share on other sites

Whoo hoo... sorted now. thanks guys you must have sparked sumthing in my old noggin. Heres my 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);

	}
	}
}	

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.