0

我有一个数据库,我在其中使用 php 按 ID 随机化信息并通过 xml 发送出去。我的问题是我只想获取 xml 一次并将其存储以用于至少 2 个函数...一个运行 onload 的函数以获取 xml 的第一行,另一个将在每次按下按钮时运行以访问下一行 xml 直到结束。我的 2 个函数是 loadfirst() 和 loadnext()。loadfirst() 完美运行,但我不确定如何将 xml 数据传递给 loadnext()。现在我只是在页面加载上使用 loadfirst() 并在按钮按下时使用 loadfirst(),但我最终每次都从数据库中创建新的 xml,这会导致随机化问题并且效率极低。任何帮助,将不胜感激。

var places;
var i = 0;

function loadXML(){

downloadUrl("places.php", function(data){
    places = data.responseXML;
        getFeatured(i);
});



}

function getFeatured(index){
   var id = places[index].getAttribute("id");
    var name = places[index].getAttribute("name");
    var location = places[index].getAttribute("location");
    var imgpath = places[index].getAttribute("imgpath");
    var tags = places[index].getAttribute("tags");

}


    function getPrev() {
    i--;
    getFeatured(i);
}

 function getNext() {
    i++;
         getFeatured(i);
    }


function downloadUrl(url, callback) {
     var request = window.ActiveXObject ?
        new ActiveXObject('Microsoft.XMLHTTP') :
        new XMLHttpRequest;
  request.onreadystatechange = function() {
        if (request.readyState == 4) {
            request.onreadystatechange = doNothing;
            callback(request, request.status);
        }
     };
     request.open('GET', url, true);
     request.send(null);
   }

    function doNothing() {}

loadnext() 将与 loadfirst() 非常相似,我只是遇到了传递 xml 数据的问题,这样我就可以使用它而无需再次访问数据库。谢谢。

4

3 回答 3

1

将您的xml 和设置i在公共范围内。然后你所要做的就是增加/减少 i 并从 XML 中重新读取数据。像这样的东西:

var xml;
var xml_idx = 0; // replaces your i counter

function loadXML() {
    downloadUrl ("places.php", function(data) {
        xml = data.responseXML;
    )};
}

function loadItem(index) {
    var id = xml[index].getAttribute("id");
    var name = xml[index].getAttribute("name");
    var location = xml[index].getAttribute("location");
    var imgpath = xml[index].getAttribute("imgpath");
    var tags = xml[index].getAttribute("tags");
    // do something with this data
}

function loadCurrentItem() {
    loadItem(xml_idx);
}

function loadNextItem() {
    xml_idx++;
    loadItem(xml_idx);
}

function loadPreviousItem() {
    xml_idx--;
    loadItem(xml_idx);
}

// usage
loadXML();  // do this first to populate xml variable
loadItem(xml_idx); // loads first item (i=0)
loadCurrentItem(); // loads i=0
loadNextItem(); // loads i=1
loadNextItem(); // loads i=2
loadPreviousItem(); // loads i=1

如果你真的想变得花哨(并保持全局命名空间更干净),你可以很容易地将它变成一个类。

于 2012-12-07T19:15:22.377 回答
0

使用全局变量(项目 - 项目数组,迭代器 - 计数器)来存储所有函数可用的数据。
尝试这样的事情:

items = false;
iterator = 0;

function loadfirst(){

downloadUrl ("places.php", function(data) {
    var i = 0;
    var xml = data.responseXML;
    var places = xml.documentElement.getElementsByTagName("place");
    var id = places[i].getAttribute("id");
    var name = places[i].getAttribute("name");
    var location = places[i].getAttribute("location");
    var imgpath = places[i].getAttribute("imgpath");
    var tags = places[i].getAttribute("tags");

    items = places;
    iterator++;
)};
}


function loadnext(){


    var i = iterator;
    var id = items[i].getAttribute("id");
    var name = items[i].getAttribute("name");
    var location = items[i].getAttribute("location");
    var imgpath = items[i].getAttribute("imgpath");
    var tags = items[i].getAttribute("tags");

    iterator++;

}
于 2012-12-07T19:12:04.083 回答
0

您应该将所有这些包装到一个对象中以控制范围和数据状态。(下面未经测试的代码,它应该只是说明一个可能的模式和使用的接口。)

function PlacesScroller(url, callback) {
    this.url = url;
    this.data = null;
    this._index = null;
    this.length = 0;
    var self = this;
    downloadURL(this.url, function(result, status) {
        if (Math.floor(status/100)===2) {
            self.setData(result);
        }
        if (callback) {
            callback(self, result);
        }
    });
}
PlacesScroller.prototype.setData(xmldom) {
    this._index = 0;
    // this may require changing; it depends on your xml structure
    this.data = [];
    var places = xmldom.getElementsByTagName('place');
    for (var i=0; i<places.length; i++) {
        this.data.push({
            id : places[i].getAttribute('id'),
            name : places[i].getAttribute('name')
            // etc
        });
    }
}
PlacesScroller.prototype.getPlaceByIndex = function(index) {
    if (this.data) {
        return this.data[index];
    } else {
        return null;
    }
}
PlacesScroller.prototype.getCurrentFeature = function() {
    return this.getPlaceByIndex(this._index);
}
PlacesScroller.prototype.addToIndex(i) {
   // This sets the index forward or back
   // being careful not to fall off the end of the data
   // You can change this to (e.g.) cycle instead
   if (this.data===null) {
        return null;
   }
   var newi = i+this._index;
   newi = Math.min(newi, this.data.length);
   newi = Math.max(0, newi);
   this._index = newi;
   return this._index;
}
PlacesScroller.prototype.getNextFeature = function() {
    this.addToIndex(1);
    return this.getCurrentFeature();
}
PlacesScroller.prototype.getPreviousFeature = function() {
    this.addToIndex(-1);
    return this.getCurrentFeature();
}

然后初始化它并像这样使用它:

var scroller = new PlacesScroller('places.php', function(scrollerobject, xmlresult){
   // put any initialization code for your HTML here, so it can build after
   // the scrollerobject gets its data.
   // You can also register event handlers here

   myNextButton.onclick = function(e){
        var placedata = scrollerobject.getNextFeature();
        myPictureDisplayingThing.update(placedata);
   }
   // etc
});
于 2012-12-07T21:05:16.333 回答