【问题标题】:Missing array data in codeigniter phpcodeigniter php中缺少数组数据
【发布时间】:2018-03-21 07:14:26
【问题描述】:

我有从数据库获取数据的菜单 这是我的代码

<nav id="mysidebarmenu" class="amazonmenu">
<ul>
    <?php
    //Get Category array
    $categories = json_decode($this->db->get_where('ui_settings', array('type' => 'home_category'))->row()->value);

    foreach ($categories as $row) {
        ?>
        <li>
            <a href="javascript:void(0);">
                <?php
                echo $this->crud_model->get_type_name_by_id('category', $row, 'category_name');
                ?>
            </a>
            <div>
                <div class="col-md-12">
                    <?php
                    //Get Sub Category array
                    $subs = $this->db->get_where('sub_category', array('category' => $row['category_id']))->result_array();

                    foreach ($subs as $row1) {
                        $this->db->limit(4);
                        $this->db->order_by('product_id', 'desc');
                        $products = $this->db->get_where('product', array('sub_category' => $row1['sub_category_id'], 'status' => 'ok'))->result_array();
                        ?>
                        <div class="col-md-12"><h3 class="text-center" style="background:#EAEAEA;"><?php echo $row1['sub_category_name']; ?></h3></div>
                        <?php
                        foreach ($products as $row2) {
                            if ($this->crud_model->is_publishable($row2['product_id'])) {
                                ?>
                                <div class="col-md-3">
                                    <div class="menu_box">
                                        <div class="img_menu_box" style="background:url('<?php echo $this->crud_model->file_view('product', $row2['product_id'], '', '', 'no', 'src', 'multi', 'one') ?>') no-repeat center center; background-size: 100% auto;">
                                        </div>

                                        <a href="<?php echo $this->crud_model->product_link($row2['product_id']); ?>">
                                            <?php echo $row2['title']; ?>
                                        </a>

                                    </div>
                                </div>
                                <?php
                            }
                        }
                        ?>
                        <?php
                    }
                    ?>
                </div>
            </div>
        </li>

        <?php
    }
    ?>


</ul>

这是我得到的结果

print_r($categories);
结果 = 数组 ( [0] => 5 [1] => 31 )
print_r($subs);
1- Array [0] => 5 = ( [0] => Array ( [sub_category_id] => 18 [sub_category_name] => A [category] ​​=> 5 ) [1] 结果 => 数组 ( [sub_category_id] => 19 [sub_category_name] => B [category] ​​=> 5 ))
数组 [1] 的 2- 结果 => 31 = 数组 ( )

问题在于第二个数组结果为空

 1-   **category table & data**
        category_id | category_name
            5              cat1
           31              cat2

2-   **Sub category table & data**
    sub_category_id | sub_category_name | category
          1              C                 31
          2              D                 31
          18             A                  5
          19             B                  5
3- ui_settings table & data
ui_settings_id  |     type      |         value
       10         home_category    ["5","31"]

预期结果
数组 [1] => 31 = ( [0] => 数组 ( [sub_category_id] => 1 [sub_category_name] => C [category] ​​=> 31 ) [1] => 数组 ([sub_category_id] => 2[sub_category_name] => D [category] ​​=> 31))

【问题讨论】:

  • 您的 SQL 或数据库中的某些内容不正确,但由于我们没有关于数据库或相关函数的任何信息,因此很难说出确切的原因。
  • @Alex 请检查我已经添加了整个数据库结构和数据这不是数据库问题数据库有数据
  • 如果您确定这不是数据库问题,则功能一定是原因。不幸的是,我们没有它们,即使我们这样做了,也有很多不可复制的代码需要筛选,因为我们没有相同的数据库和环境。我建议缩小问题范围,使其易于管理并符合堆栈最小且可验证的准则。
  • 我确定这段代码出了点问题,db->get_where('sub_category', array('category' => $row['category_id']))->result_array(); foreach ($subs as $row1) { $this->db->limit(4); $this->db->order_by('product_id', 'desc'); $products = $this->db->get_where('product', array('sub_category' => $row1['sub_category_id'], 'status' => 'ok'))->result_array(); ?>
  • 也许,但同样,除非出现任何印刷或语法错误,我无法推测问题可能是什么。我可以说代码没有任何危险信号。也许检查状态是否正常。否则,制作一个测试用例并将问题缩小到有问题的 db cat 值 31

标签: php arrays codeigniter


【解决方案1】:

先查表 ->sub_category 是否有 category = 31 的记录

【讨论】:

  • 它有请检查我编辑问题并添加了所有数据库结构及其数据
  • $categories = json_decode($this->db->get_where('ui_settings', array('type' => 'home_category'))->row()->value);跨度>
  • $categories = json_decode($this->db->get_where('ui_settings', array('type' => 'home_category'))->row()->value)。如果您在这种情况下使用 $query->row() 它只会在结果中返回第一条记录作为类对象您可以使用 $query->row_array() 和 $query->result_array() 来获取结果作为数组类对象。
  • 问题出在本节 db->get_where('sub_category',array('category'=>$row['category_id ']))->result_array(); foreach($subs as $row1){ $this->db->limit(4); $this->db->order_by('product_id','desc'); $products = $this->db->get_where('product',array('sub_category'=>$row1['sub_category_id'],'status' =>'ok'))->result_array(); ?>
  • 使用这个->print($this->db->last_query());找出查询是什么并在 db 中运行相同的 qurey 以找出 wat 出错了
猜你喜欢
  • 2011-02-25
  • 1970-01-01
  • 2013-05-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-26
相关资源
最近更新 更多