0

I have an AJAX request on a php file, which returns some data, like this:

21-12-12:::visits;45;;pageviews;344---22-10-10:::visits;71;;pageviews;34---15-01-11:::visits;4;;pageviews;30

Notice the pattern^^ It's like a multidimensional array, but only not an array, just a string.

I need a way to separate that string, and use the value of visits and pageviews for each date.

I already know how to split the string up in JQuery, but i have no idea how to get the value of visits for a specific date from that string, example, get the number of visits for the date 15-01-11.

Any suggestions or better alternatives would be great. Please don't mention JSON though, I've just given up with that.

Thanks

3
  • 1
    Here on StackOverflow we don't use the ^^ smilie. Especially not when it doesn't fit at all. Commented Dec 27, 2011 at 1:50
  • @ThiefMaster it wasn't supposed to be a 'smilie', it was supposed to be an arrow pointing up to the string that i talk about in that sentence. But okay, I wont do it again. Commented Dec 27, 2011 at 1:59
  • Ah, I think most people just use as single ^ instead. At least here in Germany almost everyone uses the ^^ smilie way too often (including me sometimes) :p Commented Dec 27, 2011 at 11:47

3 Answers 3

3

JSON is the alternative and extremely easy since you are using PHP and jQuery. Actually, everything else would be either a huge mess (parsing a string like you want to do) or much more complicated (using XML instead of JSON).

In your PHP code you simply echo json_encode($arrayWithYourData); and the response argument of your AJAX success callback will contain the same object.

Since you want to get data for a certain date, you might want to use the data as the array key - then you can just use e.g. response['01-01-01'] to access the corresponding array element.

Sign up to request clarification or add additional context in comments.

6 Comments

+1 Well, JSON indeed would be a better (and way cleaner) option by far.
I just tried document.write(response['some_date']); and it returned nothing. Also tried just document.write(response);, still nothing. the multidimensional array is setup properly (after double checking). I've been trying to get JSON working all day and eventually gave up. But if its as easy as your saying it is, I'm up for another shot.
For debugging, use your browser's developer tools (in firefox, install firebug) and then console.log(response); to see the structure of the object. alert and document.write are not good for debugging - especially the latter which erases the whole document when used after the DOM is fully loaded (which is likely to be the case when your asynchronous ajax request finishes). You might want to create a new question showing your JS and how you output the JSON and I'm sure you'll get help quickly.
@ThiefMaster I've debugged it in firebug and i see the request is returning a 200 status and after checking the response of the AJAX call, it seems to be returning the right data. But its still not working. But when i try response[3] instead of response["date"] it seems to print out the third char of the response, so if the response was "{my_name}", it prints out "_" as its the third char in the response. I don't think its picking it up as an array. ANy ideas? Thanks
Ah, I got it working now. I wasn't using the $.getjson to get the data, i was using the standard AJAX GET call. But its all working now. Thanks for your help.
|
2

Assuming you have the above string available in a variable named string, you can do:

var parts = string.split('---'), visits = 0, pageviews = 0;
for(var i=0; i<parts.length; i++) {
  if(parts[i].indexOf('01-01-01')!=-1) { // ex: looking up 01-01-01
    parts = parts[i].split(':::')[1];
    parts = parts.split(';;');
    visits = parts[0].split(';')[1]; // visits for 01-01-01
    pageviews = parts[1].split(';')[1]; // pageviews for 01-01-01
  }
}

Comments

0

Do you have control over the output format? If so switch to JSON, otherwise split the data with some RegEx.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.