#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Posts
    18
    Rep Power
    0

    Splice string into json array.


    I have a array that comes from PHP as a JSON array. I'd like to splice new items in the middle of the array. The code I have to do this is this one:

    Code:
                                    var k = 1;
      				if ( new_event_date > cur_event_date ) {
    					var splice_index = k - 1;
    					$(this).before(print_nav);
    					var current_event_id = $(this).attr('id');
    					current_event_id = current_event_id.substr(2);
    					alert ('event id '+event_id+' current id '+current_event_id);
    					for ( var n=0; n<events_data.event_id.length; n++ ) { 
    						if ( current_event_id == events_data.event_id[n]["0"] ) {
    							events_data.event_id.splice(splice_index,0,event_id);
    							alert('save event id '+event_id+' splice_index '+n+' data event id '+events_data.event_id[n]["0"]);
    							events_data.event_title.splice(splice_index,0,event_title);
    							alert('save event id '+event_id+' splice_index '+n+' data event title '+events_data.event_title[n]["0"]);
    							events_data.selected_source.splice(splice_index,0,selected_source);
    							events_data.channel_id.splice(splice_index,0,channel_id);
    							events_data.channel_name.splice(splice_index,0,channel_text);
    							events_data.event_site.splice(splice_index,0,event_site);
    							events_data.event_url.splice(splice_index,0,event_url);
    							events_data.start_date.splice(splice_index,0,start_date_string);
    							events_data.start_time.splice(splice_index,0,start_time_string);
    							events_data.end_date.splice(splice_index,0,end_date_string);
    							events_data.end_time.splice(splice_index,0,end_time_string);
    							events_data.event_notes.splice(splice_index,0,event_notes);
    							break;
    						}
    					}
    					return false;
    				}
    events.data is a json array of arrays from PHP. The splice method only seems to put the first char of the string within the new item in the array. For example, if for event_id e901, events_data.event_id[splice_index]["0"] only prints "e", similarly for the events_data.event_title. What must I fix in the script to put the entire string?
  2. #2
  3. JavaScript is not spelt java
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2011
    Location
    Landan, England
    Posts
    743
    Rep Power
    165
    A wild stab in the dark suggests

    Code:
    var tempsp = events_data.event_id[n];
    tempsp.splice(splice_index,0,event_id);

IMN logo majestic logo threadwatch logo seochat tools logo