I have the following code to generate the content with the data from Server Side: <?php…
- User
I have the following code to generate the content with the data from Server Side:
<div class="col-6 product-grid" data-aos="fade-up" data-aos-duration="200"> <div class="product-card h-100 rounded-16 position-relative transition-2" style="padding: 0px !important;"> <div class="product-card__thumb rounded-8 position-relative"> <!-- "<?=base_url()?><?= 'products/details/' . $product_row['slug'] ?>" --> <a href="<?=base_url()?><?= 'products/details/' . $product_row['slug'] ?>" class="w-100 h-100 flex-center"> <img src="<?= $product_row['image_sm'] ?>" alt="" class="w-auto rounded-8"> </a> <!-- <div class="position-absolute inset-block-start-0 inset-inline-start-0 mt-16 ms-16 z-1 d-flex flex-column gap-8"> <span class="text-main-two-600 w-40 h-40 d-flex justify-content-center align-items-center bg-white rounded-circle shadow-sm text-xs fw-semibold">-29%</span> <span class="text-neutral-600 w-40 h-40 d-flex justify-content-center align-items-center bg-white rounded-circle shadow-sm text-xs fw-semibold">HOT</span> </div> --> <div class="expand-btn-container bg-white p-2 rounded-pill z-1 position-absolute inset-inline-end-0 inset-block-start-0 me-16 mt-16 shadow-sm"> <button type="button" class="expand-btn w-40 h-40 text-md d-flex justify-content-center align-items-center rounded-circle hover-bg-main-two-600 hover-text-white"> <i class="ph ph-plus"></i> </button> <div class="expand-icons gap-20 my-20"> <button type="button" class="text-neutral-600 text-xl flex-center hover-text-main-two-600 wishlist-btn"> <i class="ph ph-heart"></i> </button> <button type="button" class="text-neutral-600 text-xl flex-center hover-text-main-two-600"> <i class="ph ph-eye"></i> </button> <button type="button" class="text-neutral-600 text-xl flex-center hover-text-main-two-600"> <i class="ph ph-shuffle"></i> </button> </div> </div> <!-- <div class="countdown position-absolute start-50 inset-block-end-0 mb-20 translate-middle-x w-100" id="countdown12"> <ul class="countdown-list style-four flex-center flex-wrap gap-8"> <li class="countdown-list__item flex-align flex-column text-sm fw-medium text-white rounded-lg bg-neutral-600"> <span class="days text-2xl text-main-two-600 fw-medium"></span>Days </li> <li class="countdown-list__item flex-align flex-column text-sm fw-medium text-white rounded-lg bg-neutral-600"> <span class="hours text-2xl text-main-two-600 fw-medium"></span>Hour </li> <li class="countdown-list__item flex-align flex-column text-sm fw-medium text-white rounded-lg bg-neutral-600"> <span class="minutes text-2xl text-main-two-600 fw-medium"></span>Min </li> <li class="countdown-list__item flex-align flex-column text-sm fw-medium text-white rounded-lg bg-neutral-600"> <span class="seconds text-2xl text-main-two-600 fw-medium"></span>Sec </li> </ul> </div> --> </div> <div class="product-card__content w-100"> <div class="product-card-tags"> <?php foreach($product_row['tags'] as $tag){?> <span class="py-2 px-8 text-xs rounded-pill text-main-two-600 bg-main-two-50"><a href="<?=base_url("products/tags/".$tag)?>"><?=$tag?></a></span> <?php } ?> </div> <h6 class="title text-lg fw-semibold" style="margin-bottom: 0px !important;"> <a href="<?=base_url()?><?= 'products/details/' . $product_row['slug'] ?>" class="link text-line-2" tabindex="0"><?= short_description_word_limit(output_escaping(str_replace('\r\n', ' ', strip_tags($product_row['name'])))) ?></a> </h6> <!-- <div class="flex-align gap-6"> <div class="flex-align gap-8"> <span class="text-15 fw-medium text-warning-600 d-flex"><i class="ph-fill ph-star"></i></span> <span class="text-15 fw-medium text-warning-600 d-flex"><i class="ph-fill ph-star"></i></span> <span class="text-15 fw-medium text-warning-600 d-flex"><i class="ph-fill ph-star"></i></span> <span class="text-15 fw-medium text-warning-600 d-flex"><i class="ph-fill ph-star"></i></span> <span class="text-15 fw-medium text-warning-600 d-flex"><i class="ph-fill ph-star"></i></span> </div> <span class="text-xs fw-medium text-gray-500">4.8</span> <span class="text-xs fw-medium text-gray-500">(12K)</span> </div> --> <span class="py-2 px-8 text-xs rounded-pill text-main-two-600 bg-main-two-50 mt-4"><a href="<?=base_url("products/category/".$product_row['category_name'])?>"><?=$product_row['category_name']?></a></span> <!-- <?php foreach($product_row['sub_categories'] as $cat){?> <span class="py-2 px-8 text-xs rounded-pill text-main-two-600 bg-main-two-50 mt-16"><a href="<?=base_url("products/category/".$cat)?>"><?=$cat?></a></span> <?php } ?> --> <span class="py-2 px-8 text-xs rounded-pill text-main-two-600 bg-main-two-50 mt-4"><a href="<?=base_url('sellers/seller_details/'.$product_row['seller_slug'])?>"><?='由'.$product_row['store_name'].'提供'?></a></span> <div class="product-card__price mt-4 mb-4"> <?php global $hkdNBF; if($variant_price != $product_row['variants'][0]['price']) { ?> <span class="text-gray-400 text-md fw-semibold text-decoration-line-through"><?=$hkdNBF->formatCurrency($product_row['variants'][0]['price'], "HKD")?></span> <?php } ?> <span class="text-heading text-md fw-semibold "><?=$hkdNBF->formatCurrency($variant_price,"HKD")?> <span class="text-gray-500 fw-normal"></span></span> </div> <!-- <a href="#" class="add_to_cart product-card__cart btn bg-gray-50 text-heading hover-bg-main-600 hover-text-white py-11 px-24 rounded-8 flex-center gap-8 fw-medium" data-product-id="<?= $product_row['id'] ?>" data-product-variant-id="<?= $variant_id ?>" data-product-title="<?= $product_row['name'] ?>" data-product-image="<?= $product_row['image']; ?>" data-product-price="<?= $variant_price; ?>" data-min="<?= $data_min; ?>" data-step="<?= $data_step; ?>" data-product-description="<?= short_description_word_limit(output_escaping(str_replace('\r\n', ' ', $sh_desc))); ?>" data-izimodal-open="<?= $modal ?>"> 加至購物車 <i class="ph ph-shopping-cart"></i> </a> --> </div> </div> </div> <? } ?> - Assistant
- User
Can you provide the full code of each part you mentioned with my reference?
- Assistant
- User
Lets says I have the array to contain all products in PHP like the following $this->data['products'] = fetch_product(null, null, null, null, null, null, "pv.date_added", "desc", NULL, NULL, null);
and I want to split the array by the batch size like: this->data['products']; this->data['products']; this->data['products']; this->data['products'] until to the last item;
- Assistant
You can split your big products array into chunks of size 6 by using PHP’s built-in
array_chunk()function.Example for your case:
// Assuming $this->data['products'] is a flat array (list) of all products $batch_size = 6; // Split it into chunks of 6 items each $product_batches = array_chunk($this->data['products'], $batch_size); // Now: $this->data['products'] = $product_batches; // Access: $this->data['products'][0] // first 6 products (array) $this->data['products'][1] // next 6 products (array) $this->data['products'][2] // next 6 products (array) // ... and so forthNote:
array_chunk()returns a multidimensional array: an array of arrays.- The last chunk might contain fewer than 6 products if total count is not divisible by 6.
If your
$this->data['products']includes nested structure like your earlier example where key holds product list, then do: - User
Please help me to integrate chucking with infinite scroll effect without using the fetch function
- Assistant
- User
Since I dont have lot of products to testing the infinite scroll. Maybe I need to duplicate the products few time for demo, how can?
- Assistant
To demo infinite scroll with a small number of products, you can simply duplicate your existing products array multiple times in PHP to simulate a larger dataset.
Here's how you can do it easily:
PHP Example: Duplicate products N times
<?php // Original product list $original_products = $this->data['products']['product']; // Number of times to duplicate (e.g. 5 times) $duplicate_times = 5; $all_products = []; for ($i = 0; $i < $duplicate_times; $i++) { // Optionally, you can modify the duplicates to differentiate them (e.g. add a suffix to name or id) foreach ($original_products as $product) { // Clone with some small change to ID or name so JS keys won't get confused $copy = $product; // Append i index to ID to make unique — adjust based on your actual product structure $copy['id'] = $product['id'] . '_' . $i; // Also modify slug or name if needed to avoid duplicates causing issues $copy['slug'] = $product['slug'] . '-copy' . $i; $copy['name'] = $product['name'] . ' (Copy ' . ($i+1) . ')'; $all_products[] = $copy; } } // Replace your data products with duplicated list for demo $this->data['products']['product'] = $all_products; // Now proceed with chunking or rendering as usual... - User
I think it is not efficiency because the loading speed is slow. Can we make it display again the first batch items when user scroll at the end of the product list? and loop infinitly?
- Assistant
- User
- Assistant