Codeigniter从数据库重复元素 [英] Codeigniter duplicate elements from the database
问题描述
我有一个包含从数据库加载的4个产品的页面,当您向下滚动时,每次都会获得4个产品。
一个随机的方式,问题是重复的产品。
这是我在模型中使用的行:
$ this-> db-> order_by('productID','RANDOM');
没有这一行一切正常。
我不能使用 limit
设置为1,因为我有:
$ query = $ this-> db-> get('product',4,$ offset);
有一个简单的方法来解决这个问题?
UPDATE
这是脚本插入到index.php
< script type =text / javascript>
$(document).ready(function(){
var products =<?= $ get_products?> ;;
var loaded_products = 0;
b $ b $(。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:
success:function(){
$ .get(dati,function(data){
$(#mainContainerProductWelcome)append(data);
};
if(loaded_products> = products - 4){
$(loadMoreProducts)。hide();
} else {
/ / load more still visible
}
},
错误:function(){
//有错误的
}
});
//在ajax请求上显示微调启动
$(。loading-spinner)。ajaxStart(function(){
$(load-spinner)。show ;
$(。text-load)。hide();
});
// ajax请求完成隐藏微调器
$(。loading-spinner)。ajaxStop(function(){
$(。loading-spinner)。delay (5000).hide();
$(。text-load)。show();
});
return false;
});
//提交表单联系
$(window).scroll(function(){
if($(window).scrollTop()+ $(window).height )> = $(document).height()){
loaded_products + = 4;
var dati =welcome / get_products /+ loaded_products;
$ b b $ .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 {
//加载更多仍然可见
}
},
错误:function(){
//有错误的
}
});
//在ajax请求上显示微调启动
$(。loading-spinner)。ajaxStart(function(){
$(。loading-spinner)。
$(。text-load)。hide();
});
// ajax请求完成隐藏微调器
$(。loading-spinner)。ajaxStop(function(){
$(。loading-spinner)。delay (5000).hide();
$(。text-load)。show();
});
return false;
}
});
});
< / script>
控制器:
code> function index()
{
$ this-> load-> helper('url');
$ data ['description'] =说明;
$ data ['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 ;
$ 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;
}
制作一个接收的ProdustID数组
对于eg :,在第一次运行中,您有productIDs为(1,5,8,9)
执行以下操作。
$ exlude_ids = 1,5,8,9;
$ this-> db-> where_not_in('productID',$ exlude_ids);下一次将
添加到exclude_ids并继续循环。
我在一个网站上做过这个。
CONTROLLER:
获取前四个产品行后,将这些product_ids添加到数组中,并将其传递给模型,如下所示。
例如: $ exlcude_ids = array(1,5,7,3);
MODEL:
function getAllProductsLimit($ offset = 0,$ exlude_ids = array())
{
$ this-> db-> select(*);
if(count($ exclude_ids)> 0)
{
$ this-> db-> where_not_in('productID',$ exlude_ids);
}
$ this-> db-> order_by('productPosition','ASC');
$ query = $ this-> db-> get('product',4,$ offset);
if($ query-> num_rows()> 0)
{
return $ query-> result();
}
else
{
return 0;
}
}
I've got a page that contains 4 products loaded from the database, when you scroll down you get 4 more products every time.
This products are loaded in a random way, the problem are the duplicate products.
This is the line that I used into the model:
$this->db->order_by('productID', 'RANDOM');
Without this line everything works fine.
I can't use limit
set to 1 because I've got:
$query = $this->db->get('product', 4, $offset);
There's a simple way to solve this problem? I've to do an array that contains all the products?
UPDATE
This is the script into the 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>
controller:
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);
}
model:
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;
}
Make an array of received ProducstIDs
For eg:, In the first run, you got productIDs as (1,5,8,9)
Do as follows.
$exlude_ids= 1,5,8,9;
$this->db->where_not_in('productID',$exlude_ids);
in next time, add those ids to exclude_ids and continue looping.
I've done this on one website. So if you need more help, just comment on it.
EDIT
CONTROLLER :
After fetching the first four product rows, add those product_ids into an array and pass this to model as shown below.
eg : $exlcude_ids = array(1,5,7,3);
MODEL :
function getAllProductsLimit($offset=0,$exlude_ids=array())
{
$this->db->select(*);
if(count($exclude_ids)>0)
{
$this->db->where_not_in('productID',$exlude_ids);
}
$this->db->order_by('productPosition','ASC');
$query = $this->db->get('product', 4, $offset);
if($query->num_rows() > 0)
{
return $query->result();
}
else
{
return 0;
}
}
这篇关于Codeigniter从数据库重复元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!