Your problem stems from the fact that you call exit(print_r($product_batches)); immediately after defining $product_batches, which stops the PHP script execution there, so your $product_batches_json and the rest of the HTML + JS code won’t work or run properly since execution stops.
User
After remove the error line, the problem still exit, please fix
The problem is productBatches always the same, for example if the total count of seller_product is 30, the productBatches will only return the first 6 product.
The correct way should be the productBatches will loop until all 30 items show and reloop from the first 6 item.
Because of this:
productBatches in JS gets JSON encoded from PHP but since exit() was called, your actual output stops early.
You see only the first batch (6 products) because only the first chunk exists in the truncated execution context.
How to fix this:
Remove or comment out the exit(print_r($product_batches)); line in your PHP script.
php
// exit(print_r($product_batches));
This line is stopping the page rendering prematurely and preventing the full product batches JSON from being output and assigned in JavaScript. Without this exit, the script completes, outputs the entire HTML page, and the product_batches_json will include all batches properly.
Once the above is fixed:
Your JavaScript logic for lazy loading batches looks correct and will:
Render a batch of 6 items.
On scroll near bottom, render next batch.
When runs out of batches, loops back to batch 0.
js
function renderBatch(index) {
if (index >= productBatches.length) {
currentBatchIndex = 0; // Loop back to first batch
index = 0;
}
...
}
So the main issue is PHP prematurely killing script execution before JSON is made available for JS.
Additional recommendations for your script to avoid similar problems:
You can print_r or var_dump for debugging only during development, but remove those before deploying.
Confirm that $seller_products['product'] actually contains all 30 products.
If you want to debug array structure without breaking the page, consider logging PHP variables before output or using in-browser debugging tools.
Summary:
The problem is caused by this line:
php
exit(print_r($product_batches));
Remove or comment it, and your product batches JSON will correctly pass to JS and your infinite scroll/load will work as expected.
Thanks for the update.
If after removing the exit(print_r($product_batches)) line you still see only the first 6 items in productBatches (instead of all 30), here are some possible reasons and step-by-step checks with fixes:
1. Confirm $seller_products['product'] actually contains all 30 products
In PHP, before chunking, add a debug to confirm total products count:
You call renderBatch first with currentBatchIndex=0
Then you scroll, it calls with currentBatchIndex=1, etc.
But your onScroll function increments currentBatchIndexevery scroll near bottom, which can trigger multiple batch renderings quickly and possibly cause weird behavior.
Also check:
Do you initialize productBatches in JS properly?
Are batches distinct?
6. Problem in the JS batching logic (possible)
Issue: When currentBatchIndex is incremented past the last batch index, you reset currentBatchIndex=0but do not reset your productListElem.innerHTML, so you start appending batch 0 again, causing duplicated batches but your UI may not reflect expected behavior.
Improve batch render logic to clear old batches when looping back to beginning, e.g.:
js
function renderBatch(index) {
if (index >= productBatches.length) {
// Loop back to first batch:
currentBatchIndex = 0;
index = 0;
// Clear the container to avoid duplicates:
productListElem.innerHTML = '';
}
const batch = productBatches[index];
let html = '';
batch.forEach(item => {
html += buildProductHTML(item);
});
productListElem.insertAdjacentHTML('beforeend', html);
// Keep DOM size in check:
const maxBatchesInDOM = 5; // Max 30 products
while (productListElem.children.length > maxBatchesInDOM * batchSize) {
productListElem.removeChild(productListElem.firstChild);
}
}
Throttle or debounce your scroll event handler to prevent multiple rapid calls:
js
let isLoading = false;
function onScroll() {
if (isLoading) return; // prevent multiple trigger
if ((window.innerHeight + window.scrollY) >= document.body.offsetHeight - 400) {
isLoading = true;
currentBatchIndex++;
renderBatch(currentBatchIndex);
setTimeout(() => {
isLoading = false;
}, 300); // 300ms debounce - adjust as needed
}
}
Initial load:
js
renderBatch(currentBatchIndex);
Add event:
js
window.addEventListener('scroll', onScroll);
Explanation:
Clearing .innerHTML on loop back avoids duplicating batches.
Throttling scroll avoids multiple increments of currentBatchIndex in a short time.
Confirming the batches contain all products ensures PHP side is correct.
Summary
Most likely your JS re-looping logic causes product batches to show only first 6 because when it loops back you append and append but never clear old products.
Fix renderBatch() to clear container on looping.
Add throttling/debouncing on scroll listener.
Confirm PHP is outputting full product batch JSON.
Confirm in the browser console productBatches.length === 5 (for 30 items with batchSize=6).
If you'd like, I can provide a full fixed JS snippet as well. Let me know!