使用jquery将结果加载到页面中

时间:2014-01-13 10:38:28

标签: javascript php jquery mysql pagination

我有一个php数据库查询,我试图用jquery处理加载页面,此时我尝试测试没有任何事情发生所以我觉得问题在于我的joutery脚本中的HTMLoutput我正在尝试执行一个php爆炸功能可以让任何人对此有所了解吗?

这是解析数据的PHP代码:

if (!$db_server){
    die("unable to Connect to MYSQL: " . mysqli_connect_error($db_server));
    $db_status = "not connected"; 
}else{  
    if(trim($_POST['submit']) =="submit"){
    }else{              
        if (isset($_POST['dropoption']) && ($_POST['dropoption'] != '')){
            if (isset($_POST['meal']) && ($_POST['meal'] != '')) {
            if(isset($_POST['pn'])){
    $rpp = preg_replace('#[^0-9#', '', $_POST['rpp']);
    $last = preg_replace('#[^0-9#', '', $_POST['last']);
    $pn = preg_replace('#[^0-9#', '', $_POST['pn']);
    if ($pn < 1) {
        $pn = 1;
    } else if ($pn > $last){
        $pn = $last;
    }
    include_once("db_connect.php");
    $limit = 'LIMIT ' .($pn - 1) * $rpp .',' .$rpp;
    $sql = "SELECT * FROM `recipename` WHERE `cuisine_type` ='$dropoption' AND `b_l_d` ='$meal' $limit";
    $query = mysqli_query($db_server, $sql);
    $datastring = '';
    while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
        $mealname = $row["mealname"];
        $mealpic = $row["imagepath"];
        $cookingtime = $row["minutes"]."minutes".$row["hours"]."hours";
        $ingredients = $row["ingredients"];
        $recipe = $row["recipe"];
        $datastring .= $mealname.'|'.$mealpic.'|'.$cookingtime.'|'.$ingredients.'|'.$recipe.'||';
    }
    echo $datastring;
    exit();
}   

$dropoption = clean_string($db_server, $_POST['dropoption']);
$meal = clean_string($db_server, $_POST['meal']);
$quer = "SELECT COUNT(recipeid) FROM `recipename` WHERE `cuisine_type` ='$dropoption' AND `b_l_d` ='$meal'";
mysqli_select_db($db_server, $db_database);
$querya= mysqli_query($db_server, $quer); 
if (!$querya) die("database access failed: " . mysqli_error($db_server));
        $row = mysqli_fetch_row($querya);    
        $total_rows = $row[0];
        $rpp = 1;
        $last = ceil($total_rows/$rpp);
        if(last < 1){
                $last = 1;
            }           
            }//if(meal)//
        }//if(cuisine)//    
    } //if(trim)//  
}
?>

这是jquery脚本:

<script type="text/javascript">
var rpp=<?php echo $rpp; ?>;
var last=<?php echo $last; ?>;
function request_page(pn) {
    var results_box = document.getElementById("results_box");
    var pagination_controls = document.getElementById("pagination_controls");
    results_box.innerHTML = "loading results";
    var hr = new XMLHttpRequest();
    hr.open("POST", "results.php", true);
    hr.setRequestHeader("Content-type", "application/x-www-form-urleconded");
    hr.onreadystatechange = function() {
        if(hr.readyState == 4 && hr.status == 200) {
            var dataArray = hr.responseText split("||");
            var html_output = "";
            for(i= 0; i< dataArray.length - 1; i++) {
                var itemArray = dataArray[i].split("|");
                html_output += "Recipe: "+itemArray[0]+"<img src='http://ml11maj.icsnewmedia.net/Workshops/Week%207/"+itemArray[1]+"'/><h2>Ingredients</h2><?php $ingredientchunks = (explode(",","+itemArray[2]+"));
for($i = 1; $i < count($ingredientchunks); $i++){
    echo "$i.$ingredientchunks[$i] <br/>";}?>"+itemArray[3]+"<h2>Recipe</h2>
<?php $recipechunks = (explode(",","+itemArray[4]+"));
for($i = 1; $i < count($recipechunks); $i++){
    echo "$i.$recipechunks[$i] </br>";} 
?>";
}
results_box.innerHTML = html_output;
        }
    }
    hr.send("rpp="+rpp+"&last="+last+"&pn="+pn);
    //change pagination controls//
    var paginationCtrls = "";
    if(last !=1) {
        if (pn > 1) {
            paginationCtrls += '<button onclick="request_page('+(pn-1)+')">&lt;</button>';
        }
        paginationCtrls += ' &nbsp; &nbsp; <b>Page '+pn+' of '+last+'</b> &nbsp; &nbsp; ';
        if (pn !=last) {
            paginationCtrls += '< <button onclick="request_page('+(pn+1)+')">&gt;</button>';    
    }
    }
    pagination_controls.innerHTML = paginationCtrls;
}
</script>

1 个答案:

答案 0 :(得分:0)

是的,我怀疑这是导致问题的explode(",","+itemArray[2]+")

爆炸用于将字符串拆分为数组,如下所示:

$string = "Apples,Oranges,Pears";
$array = explode(",",$string);
var_dump($array);

示例输出

array(2)
(
    [0] => string(5) "Apples"
    [1] => string(6) "Oranges"
    [2] => string(4) "Pears"
)