1
votes

i want to show data from model using foreach loop in view, but after reaching 2nd cycle of looping from category table and category product its not working, its show blank result

enter image description here

Here my database

i have 2 tables with category and product

enter image description here

Here my controller

  • category_menu to fetch data from category table

  • product_category to fetch data from product join category where category_id=product_category_id

     // Category Menu
     $category_table                 = 'category';
     $result['category_menu']        = $this->Model_Data->fetch($category_table);
     // Show Product join Category
     $result['product_category']     = $this->Model_Data->product_per_category();
    

Here my view

        <!-- Category -->
    <section class="category-bg">
        <div class="container">
            <?php
                $i = 1;
                foreach ($category_menu as $category_row) {
            ?>

            <div class="landing-header">
                <div class="landing-header-wrapper">
                    <div class="landing-header-left">
                        <h1><?php echo $category_row->category_name ?></h1>
                    </div>
                    <div class="landing-header-right text-right">
                        <a href="<?php echo base_url('category/'.str_replace(' ', '-', strtolower($category_row->category_name))) ?>">Lihat semua &gt; </a>
                    </div>
                </div>
            </div>
            <div class="landing-body">
                <!-- Product Swiper -->
                <div class="swiper-container swiper-product">
                    <div class="swiper-wrapper">
                        <?php
                            foreach ($product_category as $product_category_row) if ($product_category_row->product_category_id === $category_row->category_id) {
                                echo ''.$i.',';
                                echo ''.$category_row->category_id.',';
                                echo ''.$product_category_row->product_category_id;
                        ?>

                        <!-- Product -->
                        <div class="swiper-slide">
                            <a href="<?php echo base_url('product/'.str_replace(' ', '-', strtolower($product_category_row->product_name))) ?>">
                                <div class="landing-card">
                                    <div class="landing-card-header">
                                        <img src="<?php echo base_url('assets/img/product/'.$product_category_row->product_image) ?>">
                                    </div>
                                    <div class="landing-card-body">
                                        <div class="landing-card-title">
                                            <h1><?php echo $product_category_row->product_name ?></h1>
                                        </div>
                                        <div class="landing-card-sold">
                                            <span>Terjual <?php echo $product_category_row->product_sold ?></span>
                                        </div>
                                        <div class="landing-card-old-price text-right">
                                            <span class="strike">Rp <?php echo number_format($product_category_row->product_sell_price, 0, ",", ".") ?></span>
                                        </div>
                                        <div class="landing-card-price">
                                            <span class="flex-price-discount text-center">- 20%</span>
                                            <span class="flex-price-display text-center">Rp <?php echo number_format($product_category_row->product_sell_price, 0, ",", ".") ?></span>
                                        </div>
                                    </div>
                                </div>
                            </a>
                        </div>
                        <?php
                            }
                        ?>
                        
                        <!-- All Product -->
                        <div class="swiper-slide">
                            <a href="<?php echo base_url('category/'.str_replace(' ', '-', strtolower($category_row->category_name))) ?>">
                                <div class="landing-card">
                                    <div class="landing-card-header">
                                        <img src="<?php echo base_url('assets/img/product/default.jpg') ?>">
                                    </div>
                                    <div class="landing-card-body">
                                        <strong>Lihat <?php echo count($category_row) ?>+</strong> Barang Lainnya di <?php echo $category_row->category_name ?>
                                    </div>
                                </div>
                            </a>
                        </div>
                    </div>
                    <!-- Add Pagination -->
                    <div class="swiper-pagination swiper-pagination-product"></div>
                </div>
            </div>
            <?php
                }
                
                $i++;
            ?>

        </div>
    </section>
1

1 Answers

0
votes

Why can't you make left join in query and simply loop the values?