Jump to content

splitting a string


ricmetal

Recommended Posts

hi all

i need some help figuring out how to create something.

 

i have a database table with 4 columns. id, photo, thumbnail, description.

 

i am querying the table and fetching all rows with the id == 1 (theres many id numbers, but i only want to fetch all rows where id = 1).

this from an AJAX request.

in the PHP file which is called from the AJAX request, i have a script i used for another site which yields a variable with the content fo a single table row, and with in between each column fetched i added a unique text to use to do an array split, as following.

 

//JavaScript
var resultArray = resultVar.split(" uniqueDeviderLineForPhp "); // resultVar in the varibale sent from php to javascript
$(':input#exhibNameEditField').val(resultArray[0]);
$(':input#exhibDescEditField').val(resultArray[1]);
$(':input#dayEditDD').selectOptions(resultArray[2]);
$(':input#monthEditDD').selectOptions(resultArray[3]);
$(':input#yearEditDD').selectOptions(resultArray[4]);

now, this is for one single row fetch.

 

how could do the same thing for a resultVar variable with multiple rows. i already devide each columns content with the unique deivder line for php. how could i go about dividing the string not only by column, but also id?

 

the resultVar string i get is something link:

 

resultVar = id8 uniqueDeviderLineForPhp photo.jpg uniqueDeviderLineForPhp thumbnail.jpg uniqueDeviderLineForPhp description uniqueDeviderLineForPhp id16 uniqueDeviderLineForPhp photo.jpg uniqueDeviderLineForPhp thumbnail.jpg uniqueDeviderLineForPhp description uniqueDeviderLineForPhp  id34 uniqueDeviderLineForPhp photo.jpg etc

 

the id is only a number, i just added the id  text in front of the number for ease of understanding.

also, the ids are not sequencial. regards

Link to comment
https://forums.phpfreaks.com/topic/239727-splitting-a-string/
Share on other sites

mutli dimensional arrays?

 

aka

 

Array[index1][index2]

 

maybe i'm reading it wrong but it seems to me that your concatenating your results into a single string and then placing that string into an array of others..

 

when in reality you should just be using multi dimensional arrays.

 

ResultArray [sTRING1] = array('VARNAME' => 'VALUE');

ResultArray [sTRING2] = array('VARNAME' => 'VALUE');

Link to comment
https://forums.phpfreaks.com/topic/239727-splitting-a-string/#findComment-1231468
Share on other sites

"maybe i'm reading it wrong but it seems to me that your concatenating your results into a single string and then placing that string into an array of others.."

 

well that is what multidimensional arrays is, right?

 

im not very fluent in programming so ill have a hard time to explain any further details but this is what i can use:

 


// the concatenated string
var resultString = id1,photo1,thumb1,description1,id2,photo2,thumb2,description2,id3,photo3,thumb3,description3;

resultArray[0][0] = "id1";
resultArray[0][2] = "photo1";
resultArray[0][3] = "thumbnail1";
resultArray[0][4] = "description1";
resultArray[1][0] = "id2";
resultArray[1][2] = "photo2";
resultArray[1][3] = "thumbnail2";
resultArray[1][4] = "description2";

 

ive got this so far, in order to create the multidimensional arrays

 

var resultVar = "3,1a_2a_3a-1b_2b_3b-1c_2c_3c";
var charAtPos = resultVar.indexOf(',');
var x = resultVar.substring(0, charAtPos);

var parentArray= new Array(x);
for (var i=0; i<x; i++) {
parentArray[i]=new Array(3); 
}

 

but i am stil trying to find out how to populate them.

Link to comment
https://forums.phpfreaks.com/topic/239727-splitting-a-string/#findComment-1231480
Share on other sites

"maybe i'm reading it wrong but it seems to me that your concatenating your results into a single string and then placing that string into an array of others.."

 

well that is what multidimensional arrays is, right?

 

 

no that is not at all what a multidimensional array is..

 

concatenating them is just that.. a single string containing values seperated by a special character

 

as far as the rest of the bit i can't help with atm i'm not that great with javascript myself.

you seem to be on the right track tho sry i can't be of more help

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/239727-splitting-a-string/#findComment-1231573
Share on other sites

yeah but "placing that string into an array of others" can mean a multidimension array. "placing that string into an array of others arrays" :)

 

anyho, ive figured this out.

 

var resultRaw = "3,1a_2a_3a_4a-1b_2b_3b_4b-1c_2c_3c_4c";
var charAtPos = resultRaw.indexOf(',');
var items = resultRaw.substring(0, charAtPos);
var resultVar = resultRaw.substr(charAtPos + 1);

var simpleArray = resultVar.split("-");
var array = new Array(items);
for (var i = 0; i < items; i++) {
array[i] = new Array(4);
var row = simpleArray[i].split("_");
for (var j = 0; j < 4; j++) {
	array[i][j] = row[j];
}
}

alert(array[2][3]);

Link to comment
https://forums.phpfreaks.com/topic/239727-splitting-a-string/#findComment-1231709
Share on other sites

yeah but "placing that string into an array of others" can mean a multidimension array. "placing that string into an array of others arrays" :)

 

anyho, ive figured this out.

 

var resultRaw = "3,1a_2a_3a_4a-1b_2b_3b_4b-1c_2c_3c_4c";
var charAtPos = resultRaw.indexOf(',');
var items = resultRaw.substring(0, charAtPos);
var resultVar = resultRaw.substr(charAtPos + 1);

var simpleArray = resultVar.split("-");
var array = new Array(items);
for (var i = 0; i < items; i++) {
array[i] = new Array(4);
var row = simpleArray[i].split("_");
for (var j = 0; j < 4; j++) {
	array[i][j] = row[j];
}
}

alert(array[2][3]);

 

well i'm happy for you that you got it working grats! but still no a bunch of concatenated strings in an array is not a multi dimensional array .. its a single array. What you have just made with that script above is a multidimensional array. (more than 1 index)

Link to comment
https://forums.phpfreaks.com/topic/239727-splitting-a-string/#findComment-1231824
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.