jQuery/RSS/HTML5 Database

Discussion in 'Web Design and Development' started by ezkimo, Oct 13, 2010.

  1. ezkimo macrumors regular

    Joined:
    Sep 12, 2002
    #1
    Hi,
    Javascript is not my forte, so excuse me if this is incredibly obvious:

    I'm working on some code that creates a local database and table, retrieves an RSS feed and parses it and adds each item to the database. All is fine for the most part, yay. Except, I'm trying to add information from each item in the feed to a local database using the following code:

    HTML:
    $.ajax({
    	type: "GET",
    	url: feedurl,
    	dataType: "xml",
    	success: parseXml
    });
    
    function parseXml(xml){
    	$(xml).find("item").each(function(){
    		title = $(this).find("title").text();
    		description = $(this).find("description").text();
    		postype = $(this).find("postype").text();
    		category = $(this).find("category").text();
    		guid = $(this).find("guid").text();
    		postid = $(this).find("postid").text();
    		url = $(this).find("enclosure").attr('url');
    		
    		db.transaction(function(tx) {
    			tx.executeSql('INSERT INTO Posts (title, description, postype, category, guid, postid) VALUES (?,?,?,?,?,?)', [title, description,postype,category,guid,postid]);
    		});
    		
    		return true;
    	});
    }
    
    Here's the issue. The insert query is working, but it uses the same data every iteration. If I add alert() right before the title variable, it all works without a problem! WTF. I don't get it.

    Help!
    Thanks!
     
  2. ezkimo thread starter macrumors regular

    Joined:
    Sep 12, 2002
    #2
    Figured it out..

    In case anyone runs into the problem in the future:

    HTML:
    $.ajax({
    	type: "GET",
    	url: feedurl,
    	dataType: "xml",
    	success: parseXml
    });
    
    function parseXml(xml){
    	$(xml).find("item").each(function(){
    		var $this = $(this),
    			title = $this.find("title").text(),
    			description = $this.find("description").text(),
    			postype = $this.find("postype").text(),
    			category = $this.find("category").text(),
    			guid = $this.find("guid").text(),
    			postid = $this.find("postid").text(),
    			url = $this.find("enclosure").attr('url');
    		
    		db.transaction(function(tx) {
    			tx.executeSql('INSERT INTO Posts (title, description, postype, category, guid, postid) VALUES (?,?,?,?,?,?)', [title, description,postype,category,guid,postid]);
    		});
    		
    		return true;
    	});
    }
    
     

Share This Page