【发布时间】: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