Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何修复php中未定义的变量?_Javascript_Php_Jquery_Html_Mysql - Fatal编程技术网

Javascript 如何修复php中未定义的变量?

Javascript 如何修复php中未定义的变量?,javascript,php,jquery,html,mysql,Javascript,Php,Jquery,Html,Mysql,我有一个index.php页面。这个页面的功能是使用AJAX、PHP和MySQL无限滚动。顶部包含PHP MySQL代码,底部包含JavaScript。 我想打印页面中心的总行数,但每次尝试时都会显示“undefined variable”错误。 我认为在加载页面时,变量总数首先尝试打印,然后进行PHP查询,因此显示“undefined variable”,但当我将变量总数放入PHP编码中时,没有问题。 我怎样才能防止这种情况 我的index.php是 //my php part here &l

我有一个
index.php
页面。这个页面的功能是使用AJAX、PHP和MySQL无限滚动。顶部包含PHP MySQL代码,底部包含JavaScript。
我想打印页面中心的总行数,但每次尝试时都会显示“undefined variable”错误。
我认为在加载页面时,变量总数首先尝试打印,然后进行PHP查询,因此显示“undefined variable”,但当我将变量总数放入PHP编码中时,没有问题。
我怎样才能防止这种情况

我的
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>
//这里是我的php部分
//我的html部分在这里
//一些html代码
//这里有一些html代码
//这里是我的java脚本
var busy=false;
风险限额=6
var偏移=0;
var anotherID=5
功能显示记录(lim、off){
$.ajax({
类型:“POST”,
async:false,
数据:“limit=“+lim+”&offset=“+off+”&anotherID=“+anotherID,
cache:false,
beforeSend:function(){
$(“#装入器_消息”).html(“”.hide();
$(“#加载程序_图像”).show();
},
成功:函数(html){
$(“#结果”).append(html);
$(“#加载程序_图像”).hide();
如果(html==“”){
$(“#loader_message”).html('不再有记录').show()
}否则{
$(“#loader_message”).html('正在加载,请稍候…).show();
}
window.busy=false;
}
});
}
$(文档).ready(函数(){
//开始加载第一组数据
如果(忙==假){
忙=真;
//开始加载第一组数据
显示记录(限制、偏移);
}
$(窗口)。滚动(函数(){
//确保您提供了要加载的数据的容器id。
if($(窗口).scrollTop()+$(窗口).height()>$(“#结果”).height()&&&!busy){
忙=真;
偏移=限制+偏移;
//这是可选的,只是为了延迟数据加载
setTimeout(函数(){displayRecords(limit,offset);},500);
//您可以删除上述代码并直接使用此函数
//显示记录(限制、偏移);
}
});
});
//一些html代码可以帮助她
我知道在加载页面时,首先加载HTML部分,然后我的jQuery刺激PHP部分,然后显示结果。
我如何解决这个问题?
为什么
$count
总是显示“未定义变量”


提前感谢。

您会收到一个未定义的
$count
错误,因为
$count
仅在
if
语句中定义。
如果
if
子句不适用,则未定义
$count

else
子句添加到
if
并初始化
$count=0它将解决您的问题

例如:

....
$count = $actual_row_count;
exit;
}
else $count = 0;
?>

如果
If(isset($\u POST[“anotherID”]){
为false,$count将未定义。如果
中有任何语句,请重试{
block生成异常,$count将未定义,因为$actual\u row\u count将未定义。请尝试找出它发生的位置。但我必须需要$countvalue@ahgh但是什么的确切值呢?你没有得到一个ID或任何东西,那么你想要的确切计数是多少呢?在结果中找到的行数的确切值。因为我打印类似“$count items found on this page”的内容,例如:“152个items found on this page”bcz我正在使用这段代码来购物网站。如果你总是初始化所有变量,你就不会有这种问题……但我必须需要确切的$count值,但如果你从未初始化$count的值,那么它一开始不应该是零吗?@Amarnasan nope,它应该是未定义的,但我认为OP请求中的根本问题是不仅仅是初始化,我的意思是他应该强制初始化它的值为零,以防代码出现异常并且没有设置任何结果的变量。