cleary1981 Posted August 21, 2008 Share Posted August 21, 2008 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); } } Quote Link to comment Share on other sites More sharing options...
lemmin Posted August 21, 2008 Share Posted August 21, 2008 This should work: var returned = request.responseText; var phpArray = returned.split("$"); for (var i=0;i<phpArray.length;i++) phpArray[i] = phpArray[i].split("^"); Quote Link to comment Share on other sites More sharing options...
cleary1981 Posted August 21, 2008 Author Share Posted August 21, 2008 That does something but im not sure what. lol. I used an alert(phpArray) and it gace me a list of the values seperate by commas. How do I refer to each individual record from here? Quote Link to comment Share on other sites More sharing options...
haku Posted August 21, 2008 Share Posted August 21, 2008 You should be able to refer to to the elements using phparray[x][y], where you replace x and y with the indexes of the part of the array that you want. Quote Link to comment Share on other sites More sharing options...
lemmin Posted August 21, 2008 Share Posted August 21, 2008 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? Quote Link to comment Share on other sites More sharing options...
cleary1981 Posted August 22, 2008 Author Share Posted August 22, 2008 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. Quote Link to comment Share on other sites More sharing options...
cleary1981 Posted August 22, 2008 Author Share Posted August 22, 2008 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); Quote Link to comment Share on other sites More sharing options...
cleary1981 Posted August 22, 2008 Author Share Posted August 22, 2008 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); } } } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.