我有一个页面,其中包含从数据库加载的 4 种产品,当您向下滚动时,您每次都会获得 4 种以上的产品。
此产品以随机方式加载,问题是重复产品。
这是我在模型中使用的线:
$this->db->order_by('productID', 'RANDOM');
没有这条线一切正常。我不能使用limit
set to 1 因为我有:
$query = $this->db->get('product', 4, $offset);
有一种简单的方法可以解决这个问题吗?我要做一个包含所有产品的数组吗?
更新
这是 index.php 中的脚本
<script type="text/javascript">
$(document).ready(function(){
var products = <?= $get_products ?>;
var loaded_products = 0;
$(".loadMoreProducts").click(function(){
loaded_products += 4;
var dati = "welcome/get_products/" + loaded_products;
$.ajax({
url:'welcome/get_products/' + loaded_products,
type: 'get',
data: dati,
cache: false,
success: function() {
$.get(dati, function(data){
$("#mainContainerProductWelcome").append(data);
});
if(loaded_products >= products - 4) {
$(".loadMoreProducts").hide();
} else {
// load more still visible
}
},
error: function() {
// there's something wrong
}
});
// show spinner on ajax request starts
$(".loading-spinner").ajaxStart(function(){
$(".loading-spinner").show();
$(".text-load").hide();
});
// ajax request complets hide spinner
$(".loading-spinner").ajaxStop(function(){
$(".loading-spinner").delay(5000).hide();
$(".text-load").show();
});
return false;
});
// submit form contact
$(window).scroll(function() {
if($(window).scrollTop() + $(window).height() >= $(document).height()) {
loaded_products += 4;
var dati = "welcome/get_products/" + loaded_products;
$.ajax({
url:'welcome/get_products/' + loaded_products,
type: 'get',
data: dati,
cache: false,
success: function() {
$.get(dati, function(data){
$("#mainContainerProductWelcome").append(data);
});
if(loaded_products >= products - 4) {
$(".loadMoreProducts").hide();
} else {
// load more still visible
}
},
error: function() {
// there's something wrong
}
});
// show spinner on ajax request starts
$(".loading-spinner").ajaxStart(function(){
$(".loading-spinner").show();
$(".text-load").hide();
});
// ajax request complets hide spinner
$(".loading-spinner").ajaxStop(function(){
$(".loading-spinner").delay(5000).hide();
$(".text-load").show();
});
return false;
}
});
});
</script>
控制器:
function index()
{
$this->load->helper('url');
$data['description'] = "Description";
$data['keywords'] = "Keywords";
$data['products'] = $this->abitainterni->getAllProductsLimit();
$data['get_products'] = $this->abitainterni->get_products();
//load view
$this->load->view('welcome', $data);
}
function get_products($offset)
{
$data['products'] = $this->abitainterni->getAllProductsLimit($offset);
$this->load->view('get_products', $data);
}
模型:
function getAllProductsLimit($offset=0)
{
$sql = "SELECT * FROM product P";
$this->db->order_by('productPosition','ASC');
$query = $this->db->get('product', 4, $offset);
if($query->num_rows() > 0){
return $query->result();
} else {
return 0;
}
}
function get_products()
{
$query = $this->db->count_all_results('product');
return $query;
}