来自PHONEGAP的JSON AJAX请求

时间:2013-09-27 03:58:13

标签: php ajax json cordova

我正在为droid和iphone构建一个phonegap应用程序 我在phonegap html文件中创建了一个json ajax请求,以便从我的网站调用一个从我的数据库中提取信息的php脚本。

我有一个限制设置来限制带回多少信息,超过5的任何信息都不会通过。它几乎就像有多少数据或我可以请求多少?我把头发拉过来。

这是我的json脚本......

<script type="text/javascript">
<!--var myTimer; -->
function ajax_json_data(){
    var databox = document.getElementById("databox");
    var arbitrarybox = document.getElementById("arbitrarybox");
    var hr = new XMLHttpRequest();
    hr.open("POST", "http://www.metrodetroithappyhour.com/test/bar_data.php", true);
    hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    hr.onreadystatechange = function() {
        if(hr.readyState == 4 && hr.status == 200) {
            var d = JSON.parse(hr.responseText);
            arbitrarybox.innerHTML = d.arbitrary.returntime;
            databox.innerHTML = "";
            for(var o in d){
                if(d[o].name){
            databox.innerHTML = "<table width='100%' border='0' cellpadding='0' cellspacing='0' class='top_logo' height='14%'><tr><td height='3%'></td></tr><tr><td align='center' valign='middle' height='94%'><font color='#FFFFFF' style='font-weight:bolder'>All Specials For "+d[o].weekday+"</font></td></tr><tr><td height='3%'></td></tr></table>"
            for(var o in d){
                if(d[o].name){
                    databox.innerHTML += "<table width='100%' border='0' cellpadding='0' cellspacing='0' class='bottom_logo'><tr><td><table width='100%' border='0' cellpadding='0' height='2%'><tr><td></td></tr></table><table width='100%' border='0' cellpadding='0' cellspacing='0'><tr><td width='2%'></td><td width='20px'><img src='http://www.metrodetroithappyhour.com/northern_michigan/bar_pics/"+d[o].bar_pic+".png' width='75px'></td><td width='2%'></td><td valign='top' align='left'><table width='100%' height='100%' border='0' cellpadding='0' cellspacing='0'><tr valign='top' align='left' height='100%'><td align='left' valign='top'><font style='font-weight:bolder'>"+d[o].name+"</font><br /></td></tr>  <tr align='left' valign='top' height='100%'><td height='75' align='left' valign='middle'><font style='font-weight:lighter'>"+d[o].special1+"<br />"+d[o].special2+"<br />"+d[o].special3+"</font></td></tr></table></td></tr></table><table width='100%' border='0' cellpadding='0' height='2%'> <tr><td></td></tr></table></td></tr></table>";
                }}
                }
            }
        }
    }
    hr.send("limit=5");
    databox.innerHTML = "<table class='bottom_logo' width='100%' border='0'><tr><td align='center'>UPDATING SPECIALS...</td></tr></table>";
    <!--myTimer = setTimeout('ajax_json_data()',6000); -->
}
</script>

这是我的php脚本......

<?php
$jd=cal_to_jd(CAL_GREGORIAN,date("m"),date("d"),date("Y"));
$weekday = (jddayofweek($jd,1));
header("Content-Type: application/json");
if(isset($_POST['limit'])){
    $limit = preg_replace('#[^0-9]#', '', $_POST['limit']);
require_once("../northern_michigan/connect_to_mysql.php");
    $i = 0;
    $jsonData = '{';
    $sqlString = "SELECT * FROM bars WHERE zip='49713' AND subscribed='1' AND location='1' ORDER BY RAND() LIMIT $limit";
    $query = mysql_query($sqlString) or die (mysql_error()); 
    while ($row = mysql_fetch_array($query)) {
        $i++;
        $id = $row["id"];
        $name = $row["name"];
        $zipcode = $row["zipcode"];
//      $signupdate  = $row["signupdate"];
//      $signupdate = strftime("%B %d, %Y", strtotime($signupdate));

        $sqlString2 = "SELECT * FROM specials WHERE bar_id='$id' AND day_of_week='$weekday'";
        $query2 = mysql_query($sqlString2) or die (mysql_error()); 
        while ($row2 = mysql_fetch_array($query2)) {
                $special1 = $row2["special1"];
                $special2 = $row2["special2"];
                $special3 = $row2["special3"];

                $nameCut = substr($name, 0, 30);
                $nameBig = ucwords(strtolower($nameCut));

             $check_pic = "../northern_michigan/bar_pics/$id.png";
    if (file_exists($check_pic)) {
        $bar_pic = "$id";
    } else {
        $bar_pic = "128x128";
    }

        if($special1!="") // Two times Doule Quotation marks that is, and != means "not equal to". So we mean to say if $code is not equal to empty 
{ 
$jsonData .= '"article'.$i.'":{ "id":"'.$id.'","name":"'.$name.'", "special1":"'.$special1.'", "special2":"'.$special2.'", "special3":"'.$special3.'", "bar_pic":"'.$bar_pic.'", "weekday":"'.$weekday.'" },';
} 
else 
{ 
echo ""; 
} 

//      $jsonData .= '"article'.$i.'":{ "id":"'.$id.'","name":"'.$name.'", "special1":"'.$special1.'", "special2":"'.$special2.'", "special3":"'.$special3.'" },';
        }
    }
    $now = getdate();
    $timestamp = $now[0];
    $jsonData .= '"arbitrary":{"itemcount":'.$i.', "returntime":"'.$timestamp.'"}';
    $jsonData .= '}';
    echo $jsonData;
}
?>

我想立刻拉出很多物品.100或更多。

请帮忙!谢谢!

0 个答案:

没有答案