[英]Load Data on scroll jQuery / PHP from MySQL data
Afternoon all. 下午全部。 I am trying the achieve the above and have been following the above guide see @ http://www.asif18.com/4/php/window-on-scroll-load-contents-in-php-mysql-using-jquery-bootstrap/
我正在尝试实现上述目标,并且一直在遵循上述指南,请参见@ http://www.asif18.com/4/php/window-on-scroll-load-contents-in-php-mysql-using-jquery-引导程序/
The test page I have created can be seen here. 我创建的测试页可以在这里看到。 http://coolnique.com/products_autoscroll.php
http://coolnique.com/products_autoscroll.php
It doesn't seem to be sitting doing nothing. 它似乎并没有坐着。 The 'loadmore' Page doesn't error when I access it so maybe it the javascript not working?
当我访问'loadmore'页面时不会出错,因此javascript无法正常工作?
Here is my js file 这是我的js文件
$(document).ready(function(){
$(window).scroll(function(){ /* window on scroll run the function using jquery and ajax */
var WindowHeight = $(window).height(); /* get the window height */
if($(window).scrollTop() +1 >= $(document).height() - WindowHeight){ /* check is that user scrolls down to the bottom of the page */
$("#loader").html("<img src='img/loading_icon.gif' alt='loading'/>"); /* displa the loading content */
var LastDiv = $(".project small:last"); /* get the last div of the dynamic content using ":last" */
var LastId = $(".project small:last").attr("id"); /* get the id of the last div */
var ValueToPass = "lastid="+LastId; /* create a variable that containing the url parameters which want to post to getdata.php file */
$.ajax({ /* post the values using AJAX */
type: "POST",
url: "_loadmore.php",
data: ValueToPass,
cache: false,
success: function(html){
$("#loader").html("");
LastDiv.after(html); /* get the out put of the getdata.php file and append it after the last div using after(), for each scroll this function will execute and display the results */
}
});
}
});
});
Here is my load more file... 这是我加载的更多文件...
<?php
//include location select
include'_locationselect.php';
// Connect to database
// HAVE REMOVE DATABASE VARIABLES FROM HERE
//Find last record
if(isset($_POST["lastid"]) && $_POST["lastid"] != "0"){
$lastid = $_POST["lastid"]; // save the posted value in a variable
$query="SELECT * FROM products WHERE product_price_$setLocation IS NOT NULL and product_id < '$lastid' Order By product_id DESC LIMIT 10";
$result=mysql_query($query) or die('Invalid query: ' .mysql_error());
// Store number of products as variable
$num=mysql_num_rows($result);
// Start loop to display products
$i=0;
while ($i < $num) {
$f1=mysql_result($result,$i,"product_name");
$f2=mysql_result($result,$i,"product_price_$setLocation");
$f3=mysql_result($result,$i,"product_link_$setLocation");
$f4=mysql_result($result,$i,"product_image");
// $f5=mysql_result($result,$i,"category_id");
// $f6=mysql_result($result,$i,"product_desc");
$f7=mysql_result($result,$i,"product_id");
$f1spacesremoved = str_replace(' ', '_', $f1);
if ($setLocation=="us")
{
$currencysymbol = "$";
}
else
{
$currencysymbol = "£";
};
//Write each product
//loop the text below
echo '<div class="project small" id="'.$f7.'">
<div class="inside">
<a href="/product/' .rawurlencode($f1spacesremoved). '/' .$f7. '">
<img width="300" height="175" src="/img/products/'.$f4.'" class="thumb wp-post-image" />
<span class="title"><span>'.$f1.'</span><span>'.$currencysymbol.' '.$f2.'</span></span>
</a>
</div> </div>
';
// Repeat loop until finished
$i++;
}
}
?>
And then on my main page have this bit of code where it should load 然后在我的主页上有应加载的这段代码
$query="SELECT * FROM products WHERE product_price_$setLocation IS NOT NULL Order By product_id DESC LIMIT 20";
$result=mysql_query($query) or die('Invalid query: ' .mysql_error());
// Store number of products as variable
$num=mysql_num_rows($result);
// Start loop to display products
$i=0;
while ($i < $num) {
$f1=mysql_result($result,$i,"product_name");
$f2=mysql_result($result,$i,"product_price_$setLocation");
$f3=mysql_result($result,$i,"product_link_$setLocation");
$f4=mysql_result($result,$i,"product_image");
// $f5=mysql_result($result,$i,"category_id");
// $f6=mysql_result($result,$i,"product_desc");
$f7=mysql_result($result,$i,"product_id");
$f1spacesremoved = str_replace(' ', '_', $f1);
if ($setLocation=="us")
{
$currencysymbol = "$";
}
else
{
$currencysymbol = "£";
};
//Write each product
//loop the text below
echo '<div class="project small" id="'.$f7.'">
<div class="inside">
<a href="/product/' .rawurlencode($f1spacesremoved). '/' .$f7. '">
<img width="300" height="175" src="/img/products/'.$f4.'" class="thumb wp-post-image" />
<span class="title"><span>'.$f1.'</span><span>'.$currencysymbol.' '.$f2.'</span></span>
</a>
</div> </div>
';
// Repeat loop until finished
$i++;
}
?>
<div id="loader"></div>
<div id="divResult"></div> <!-- here the rest of contents will display dynamically -->
</div>
</div>
</div>
</div>
</div>
Any pointers would be a great help as I just cannot seem to get this thing to work! 任何指针都将是一个很大的帮助,因为我似乎无法使这个东西正常工作! And im a bit of a newb with PHP!
我用PHP有点新手!
Try to change loadmore.js : 1 尝试更改loadmore.js :1
$(document).ready(function(){
to 至
jQuery(function($) {
$(window).scroll(function () {
if ($(window).height() + $(window).scrollTop() == $(document).height()) {
//do your stuff here
}
});
I have followed this approach. 我遵循了这种方法。 Try it.
试试吧。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.