2015-08-28 30 views
-1

我有一個index.php頁面。這個頁面的功能是使用AJAX,PHP和MySQL進行無限滾動。頂部包含PHP MySQL代碼,底部包含JavaScript。
我想打印頁面中心的總行數,但每次嘗試時都會顯示「未定義的變量」錯誤。
我想在加載頁面時,首先打印變量的嘗試總數,然後發生PHP查詢,所以它顯示「未定義的變量」,但是當我把變量的總數放在PHP編碼中時,沒問題。
我該如何預防?如何修復php中的undefined變量?

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> 

我知道,當網頁加載時,HTML部分首先加載,然後我的jQuery的刺激PHP的一部分,然後我的結果出現。
我該如何解決這個問題?
爲什麼$count總是顯示「未定義變量」?

在此先感謝。

+2

如果'如果(isset($ _ POST [ 「anotherID」])){'是假的,$計數將是不確定的。如果'try {'塊中的任何語句產生一個異常,那麼$ count將是未定義的,因爲$ actual_row_count將是未定義的。試着找出它發生的地方。 – raduation

+0

但我必須需要$計數值 – ahgh

+0

@ahgh但什麼的確切價值?你沒有得到一個ID或任何東西,所以你想要什麼確切的數字? –

回答

0

由於$count僅在if語句中定義,所以會出現未定義的$count錯誤。
if子句不適用$count未定義。

將一個else子句添加到if並初始化$count=0;它將解決您的問題。

實施例:

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

但我必須確切的$計數值 – ahgh

+0

但是,如果你從未初始化$計數的價值,它不應該是零作爲一個開始? – Amarnasan

+0

@Amarnasan nope,它應該是未定義的,但我認爲OP的請求中的根本問題不僅僅是初始化。 –

相關問題