【问题标题】:Foreach loop with selection in codeigniter 3在codeigniter 3中进行选择的foreach循环
【发布时间】:2021-01-31 14:50:11
【问题描述】:

我想在视图中使用 foreach 循环显示模型中的数据,但是在达到从类别表和类别产品循环的第二个循环后它不起作用,它显示空白结果

这是我的数据库

我有 2 个包含类别和产品的表格

这是我的控制器

  • category_menu 从类别表中获取数据

  • product_category 从产品连接类别中获取数据,其中 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();
    

这是我的看法

        <!-- 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>

【问题讨论】:

    标签: php codeigniter codeigniter-3


    【解决方案1】:

    为什么不能在查询中进行左连接并简单地循环值?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-08
      • 2016-02-21
      • 2021-03-20
      • 2012-01-12
      • 2017-11-27
      • 1970-01-01
      相关资源
      最近更新 更多