[英]How do I calculate the last iteration of a PHP while loop with jQuery/JavaScript?
In PHP I have a page that checks if there are more than 0 rows and if not then the user gets a message saying echo 'NO MATCHES';
在 PHP 我有一个页面检查是否有超过 0 行,如果没有,那么用户会收到一条消息,说echo 'NO MATCHES';
, then checks if the user $_POST
ed any filters to show all data that fits in said filtered parameters and if not shows all iterations. ,然后检查用户$_POST
是否编辑了任何过滤器以显示适合所述过滤参数的所有数据,如果没有则显示所有迭代。 Here's my pseudo code:这是我的伪代码:
PHP.php PHP.php
$rowcount = mysqli_num_rows($result); //Count iterations
if($rowcount > 0){
if(isset($_POST["filter1"]) && isset($_POST["filter2"]) && isset($_POST["filter3"])) {
//If there are filters selected
while($row = mysqli_fetch_array($result)) {
include('Listing.php');
}
} else {
//If no filters have been selected
while($row = mysqli_fetch_array($result)) {
include('Listing.php');
}
}
} else {
echo '<div class="NoMatch">NO MATCHES</div>';
}
Then in my jQuery I have an AJAX call that when an image with the #Loader
is inView (a third party function) it grabs my filters as data, directs to the PHP page (above) and appends the data inside the container.然后在我的 jQuery 中,我有一个 AJAX 调用,当带有#Loader
的图像在inView(第三方功能)时,它会抓取我的过滤器作为数据,指向PHP页面内部的数据(容器.bove) Here's that pseudo code:这是伪代码:
jQuery.js jQuery.js
$('#Loader').on('inview', function(event, isInView) {
if (isInView) {
// Filters
var filter1 = get_filter('filter1');
var filter2 = get_filter('filter2');
var filter3 = get_filter('filter3');
$.ajax({
url: 'PHP.php',
type: 'POST',
data: {
filter1: filter1,
filter2: filter3,
filter3: filter3
},
success:
function(data) {
if(data != '') {
$('#StoneContainer').append(data);
$('#pageno').val(nextPage);
} else {
$('#StoneContainer').html(data);
$('#Loader').hide(); // Hide infinite scroll
}
}
});
}
});
In the if
statement of my jQuery.AJAX i'm trying to hide the image ( #Loader
) when data
is empty but it's never empty because of my PHP's echo 'NO MATCHES';
在我的 jQuery.AJAX 的if
语句中,当data
为空但它永远不会为空时,我试图隐藏图像( #Loader
),因为我的 PHP echo 'NO MATCHES';
. . How do I calculate the last index of the PHP's while loop with jQuery to hide the image when there are no more PHP iterations?当没有更多的 PHP 迭代时,如何使用 jQuery 计算 PHP 的 while 循环的最后一个索引以隐藏图像?
ATTEMPTS :尝试:
if(data.lastIndexOf('class="Iteration"') == 0) {
$('#Container').append(data);
$('#pageno').val(nextPage);
} else {
$('#Container').html(data);
$('#Loader').hide(); // Hide infinite scroll
}
Didn't work because it would find the last class of that page but not the entire array and if(data.indexOf('class="Iteration"') > 0) {
didn't work because it only hides when the PHP $rowcount < 0
but not when the array is finished.不起作用,因为它会找到该页面的最后一个 class 但不是整个数组,并且if(data.indexOf('class="Iteration"') > 0) {
不起作用,因为它仅在 PHP $rowcount < 0
时隐藏$rowcount < 0
但不是在数组完成时。
$('#Container').append(data);
$('#pageno').val(nextPage);
$('.Iteration').each(function(element) {
if (element + 1 === count) {
$('#Loader').hide(); // Hide infinite scroll
}
});
I can't figure out why didn't work either.我也想不通为什么没有工作。
Sorry - I can't comment yet, but I think this is a reasonable answer -抱歉-我还不能发表评论,但我认为这是一个合理的答案-
Why not simply change your if() condition, to exclude whatever the 'no match' condition text looks like.为什么不简单地更改您的 if() 条件,以排除任何“不匹配”条件文本的样子。
Your comments seem to indicate that the 'no match' text looks like this:您的评论似乎表明“不匹配”文本如下所示:
<div class="NoMatch">NO MATCHES</div>
Let's say that the string 'NoMatch' is guaranteed to only show up when there are no matches, and will never show up when there were 1 or more matches.假设字符串 'NoMatch' 保证仅在没有匹配项时出现,并且在有 1 个或多个匹配项时永远不会出现。
Then you could use this:然后你可以使用这个:
if(data !== '' && !data.includes('NoMatch')) {
$('#StoneContainer').append(data);
$('#pageno').val(nextPage);
} else {
$('#StoneContainer').html(data);
$('#Loader').hide(); // Hide infinite scroll
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.