开发者

Passing Javascript variable from one function to another function for use in a sqlite query

I am trying to write a Javascript function that obtains an array of 开发者_开发百科unique ids from an sqlite database & then passes them to be used in another function where the ids can be used in another sql query, they also form part of a dynamically created list.

I have managed to pass the ids row['id'] to the array variable window.symp[i]. But I have not been able to access them correctly in the second function below, the second function correctly uses the ids to create the dynamic html but the variable passed to the sqlite query either fails or is the same value in all the list items created. Any help would be greatly appreciated - I have included both functions below:

function showContent() {
    db.transaction(function (tx) {
        tx.executeSql("SELECT id, notes FROM webkit WHERE notes LIKE 'A%'", [], function (tx, result) {
            var notesanode = document.getElementById('notesa');
            notesanode.innerHTML = "";

            for (var i = 0; i < result.rows.length; ++i) {
                var row = result.rows.item(i);
                window.symp[i] = i;
                window.symp[i] = row['id'];
                var noteadiv = document.createElement('div');
                noteadiv.innerHTML = '<li class=\"arrow\"><a id=\"0\" onClick=\"showSymptoms()\" href=\"#symptoms\">' + row['notes'] + " " + row['id'] + '</a></li>';
                notesanode.appendChild(noteadiv);

            }

        }, function (tx, error) {
            alert('Failed to retrieve notes from database - ' + error.message);
            return;
        });
    });
}

function showSymptoms() {
    db.transaction(function (tx) {
        tx.executeSql("SELECT sid, symptom FROM slinks WHERE id LIKE ('" + symp + "')", [], function (tx, result) {
            var symptomnode = document.getElementById('symptomid');
            symptomnode.innerHTML = "";

            for (var i = 0; i < result.rows.length; ++i) {
                var row = result.rows.item(i);
                var symptomdiv = document.createElement('div');
                symptomdiv.innerHTML = '<p><label> <input type=checkbox>' + row['symptom'] + '</label></p>';
                symptomnode.appendChild(symptomdiv);
            }

        }, function (tx, error) {
            alert('Failed to retrieve notes from database - ' + error.message);
            return;
        });
    });
}


I see two things:

The main issue is that you need

"SELECT sid, symptom FROM slinks WHERE id LIKE ('" + window.symp.join(',') + "')"

instead of what you have up there.

The second issue is that you have

window.symp[i] = i;
window.symp[i] = row['id'];

you can just go ahead and remove window.symp[i] = i; because it gets immediately overwritten by the line that follows it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜