我正试图从常规表中获取常规表中的值来自ajax
中php php调用的mysql_query和mysql_fetch_arrayif ($comtype==3){
$getsteps = mysql_query("SELECT id FROM steps WHERE id = $id");
$row = mysql_fetch_array($getsteps);
if($row != false){
$steps_array = array();
while ($row = mysql_fetch_array($getsteps, MYSQL_ASSOC))
{
array_push($steps_array,$row);
}
$countsteps = count($steps_array);
error_log("count: ".$countsteps);
error_log(print_r($steps_array));
echo json_encode(array("comtype" => $comtype, "countsteps" => $countsteps, "steps" => $steps_array));
} else {
//error
}
}
<{3>}错误日志中的:
[2017年8月28日11:25:32 UTC]数:1 [28-Aug-2017 11:25:32 UTC] 1
虽然计数我知道它是2行!!!
和ajax
$.ajax({
type:'POST',
url:'app/check.php',
data: {'ID':ID},
dataType: 'json',
success: function(value){
if (value.comtype== "0") {
$("#newC").show();
$("#hadC").hide();
} else {
var method;
var com;
com = "<div class='clearfix'></div><br><h3>old.</h3>";
com += "By Stpes: <br>";
for (i = 0; i < value.countsteps; i++) {
com += "Step " + Number(i)+1 + ": Greater Than " + value.steps_array['calfrom'] + " AND Less Than or Equal " + value.steps_array['calto'] + " Apply " + value.steps_array['calapl'] + " % <br>";
}
}
$("#hadC").html(com);
$("#hadC").show();
$("#newC").hide();
}
}
});
问题是: 1.我不知道如何将数据推送到数组中? 2.我不知道如何在ajax中循环到这个数组,以便在html中查看它 DIV?
答案 0 :(得分:1)
问题是您在mysql_fetch_array()
循环之前调用了while
。这将获取第一行,循环将获取剩余的行。
如果要在循环之前测试是否返回了任何行,请调用mysql_num_rows()
。您也可以使用此代替count($steps_array)
。
if ($comtype==3){
$getsteps = mysql_query("SELECT id FROM steps WHERE id = $id");
$countsteps = mysql_num_rows($getsteps);
if($countsteps > 0) {
$steps_array = array();
while ($row = mysql_fetch_array($getsteps, MYSQL_ASSOC))
{
array_push($steps_array,$row);
}
error_log("count: ".$countsteps);
error_log(print_r($steps_array));
echo json_encode(array("comtype" => $comtype, "countsteps" => $countsteps, "steps" => $steps_array));
} else {
//error
}
}