问题
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;
}
回答1:
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;
}
}
回答2:
You should do it like this. When you first run the query set an array.
Now when you get the results( 4 rows) and display the records put the ids of these records in a javascript array. You must be using ajax to scrolldown and fetching more results.
Next time when you want to run query first send the javascript array, to php use json. PHP will get json encoded array .decode it. if its length is equal to 0 then run simple query.else run WHERE productID NOT IN ($array)
. this way you will be able to do it.
来源:https://stackoverflow.com/questions/14397772/codeigniter-duplicate-elements-from-the-database