[英]how to fix undefined variable in php?
I have an index.php
page.我有一个index.php
页面。 The function of this page is infinite scrolling using AJAX, PHP and MySQL.该页面的功能是使用AJAX、PHP和MySQL无限滚动。 The top portion contains PHP MySQL codes and bottom contains JavaScript.顶部包含 PHP MySQL 代码,底部包含 JavaScript。
I want to print the total number of rows in center of the page, but every time I try it shows "undefined variable" error.我想在页面中央打印总行数,但每次尝试时都会显示“未定义变量”错误。
I think when loading the page, the total number of variable tries to print first and then the PHP query takes place, so it shows "undefined variable", but when I put the total number of variable inside the PHP codings, there is no problem.我认为在加载页面时,变量总数尝试先打印,然后进行PHP查询,因此显示“未定义变量”,但是当我将变量总数放入PHP编码中时,没有问题.
How can I prevent this?我怎样才能防止这种情况?
My index.php
is我的index.php
是
//my php part here
<?php
if(isset($_POST["anotherID"])){
require_once("config.php");
$limit = (intval($_POST['limit']) != 0 ) ? $_POST['limit'] : 10;
$offset = (intval($_POST['offset']) != 0 ) ? $_POST['offset'] : 0;
$id = $_POST["anotherID"];
$query = $id;
$sql = "SELECT SQL_CALC_FOUND_ROWS * FROM x where title like '%xx%' ORDER BY rand() LIMIT $limit OFFSET $offset";
try {
$stmt = $DB->prepare($sql);
$stmt->execute();
$results = $stmt->fetchAll();
$row_object = $DB->prepare("Select Found_Rows() as rowcount");
$row_object->execute();
$roww_object =$row_object->fetchobject();
$actual_row_count = $roww_object->rowcount;
} catch (Exception $ex) {
echo $ex->getMessage();
}
if (count($results) > 0) {
foreach ($results as $res) {
echo'something';
}
}
$count = $actual_row_count;
exit;
}
?>
//my html part here
<html>
//some html codes
<?php echo $count; ?>
//some html codes here
//my java scripts here
<script type="text/javascript">
var busy = false;
var limit = 6
var offset = 0;
var anotherID = 5
function displayRecords(lim, off) {
$.ajax({
type: "POST",
async: false,
data: "limit=" + lim + "&offset="+ off+"&anotherID="+anotherID,
cache: false,
beforeSend: function() {
$("#loader_message").html("").hide();
$('#loader_image').show();
},
success: function(html) {
$("#results").append(html);
$('#loader_image').hide();
if (html == "") {
$("#loader_message").html('<button class="btn btn-default btn-block" type="button">No more records.</button>').show()
} else {
$("#loader_message").html('<button class="btn btn-default btn-block" type="button"><div id="loader_image"><img src="loader.gif" alt="" width="24" height="24">Loading please wait...</button>').show();
}
window.busy = false;
}
});
}
$(document).ready(function() {
// start to load the first set of data
if (busy == false) {
busy = true;
// start to load the first set of data
displayRecords(limit, offset);
}
$(window).scroll(function() {
// make sure u give the container id of the data to be loaded in.
if ($(window).scrollTop() + $(window).height() > $("#results").height() && !busy) {
busy = true;
offset = limit + offset;
// this is optional just to delay the loading of data
setTimeout(function() { displayRecords(limit, offset); }, 500);
// you can remove the above code and can use directly this function
// displayRecords(limit, offset);
}
});
});
</script>
//some html codes her
</html>
I know when a page is loading, the HTML parts are first loaded and then my jQuery stimulates the PHP part and then my results appear.我知道当页面加载时,首先加载 HTML 部分,然后我的 jQuery 刺激 PHP 部分,然后我的结果出现。
How can I fix this?我怎样才能解决这个问题?
Why does $count
always show "undefined variable"?为什么$count
总是显示“未定义的变量”?
Thanks in advance.提前致谢。
You get an error of undefined $count
because $count
is defined only inside the if
statement.您会收到 undefined $count
错误,因为$count
仅在if
语句中定义。
When the if
clause doesn't apply $count
is not defined.当if
子句不适用时, $count
未定义。
Add an else
clause to the if
and initialize $count=0;
在if
添加else
子句并初始化$count=0;
and it will solve your problem.它会解决你的问题。
Example:例子:
....
$count = $actual_row_count;
exit;
}
else $count = 0;
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.