-1

我不确定我的错误在哪里,但记录请求总是返回相同的。

首先我要列表(工作正常) - list.js

 $('#listPage').bind('pageinit', function(event) {
    getList();
});

function getList() {
    $.getJSON(serviceURL + 'getlist.php', function(data) {
        $('#list li').remove();
        list= data.items;
        //edit function below based on db table
        $.each(list, function(index, sites) {
            $('#list').append('<li><a href="details.html?id=' + sites.id + '">' +
                    '<h4>' + sites.title + '</h4>' +
                    '<p>' + sites.address + '</p></a></li>'
                    );
        });
        $('#list').listview('refresh');
    });
}

获取列表.php

include 'config.php';
//change FROM to ____
$sql = "SELECT id, title, address, picture1 FROM sites ORDER BY title";

try {
    $dbh = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);  
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $stmt = $dbh->query($sql);  
    $details = $stmt->fetchAll(PDO::FETCH_OBJ);
    $dbh = null;
    echo '{"items":'. json_encode($details) .'}'; 
} catch(PDOException $e) {
    echo '{"error":{"text":'. $e->getMessage() .'}}'; 
}

然后在下面的某个地方,记录被弄乱了。-details.js _

$('#detailsPage').live('pageshow', function(event) {
    var id = getUrlVars()["id"];
    $.getJSON(serviceURL + 'getdetails.php?id='+ id, displayDetail);
});

function displayDetail(data) {
    var sites = data.item;
    console.log(sites);
        $('#pic').attr('src', 'http://hh.lpbp.net/assets/uploads/files/' + sites.picture1);
    $('#title').text(sites.title);
    $('#address').text(sites.address);
    $('#pic2').attr('src', 'http://hh.lpbp.net/assets/uploads/files/' + sites.picture2);
    if (sites.phone) {
        $('#actionList').append('<li><h3>Phone #:</h3>' + '<p>' + '<a href="tel:' + sites.phone + '">'+ sites.phone + '</a></p></li>');
    }
    if (sites.website) {
        $('#actionList').append('<li><h3>Website</h3>' + '<p>' + '<a href="#" onClick="Ti.App.fireEvent(\'openURL\', { url:\'http://' +sites.website +'\'}); return false;">' + sites.website + '</a></p></li>');
    }
    if (sites.description) {
        $('#actionList').append('<li><h3>Description</h3>' + '<p>' + sites.description + '</p></li>');

    }
    $('#actionList').listview('refresh');

}

function getUrlVars() {
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}

获取详细信息.php

include 'config.php';
//Change FROM ___
$sql = "SELECT id, title, address, phone, website, description, picture1, picture2 FROM sites ORDER BY title";

try {
    $dbh = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);  
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $stmt = $dbh->prepare($sql);  
    $stmt->bindParam("id", $_GET[id]);
    $stmt->execute();
    $detail = $stmt->fetchObject();  
    $dbh = null;
    echo '{"item":'. json_encode($detail) .'}'; 
} catch(PDOException $e) {
    echo '{"error":{"text":'. $e->getMessage() .'}}'; 
}
4

1 回答 1

2

您从不使用id列表的值,因此您只需获取整个sites表,拉出第一行,然后返回。

那里的某个地方应该有一个WHERE子句。您正在绑定该id字段,但查询中没有:id参数,所以基本上您是在冒烟。

$sql = "SELECT blah,blah,blah FROM sites WHERE id = :id";
                                        ^^^^^^^^^^^^^^^

ORDER BY是毫无意义的,除非您期望返回多行。已经订购了单行结果。

于 2012-07-17T17:02:04.857 回答