我得到一个Warning: Illegal string offset 'customitem_id' in admin/controller/sale/customer.php
和/admin/controller/sale/customer.php on line 941Notice: Uninitialized string offset: 0
我正在尝试在列表中显示所有相关的自定义产品,我不确定这个错误意味着什么。这是代码,如果我没有foreach循环,但只显示一个产品,它就会工作。我需要其他5个指定的产品也出现。
`//自定义项目分配给客户
if (isset($this->request->post['customitem_id'])) {
$data['customitem_id'] = $this->request->post['customitem_id'];
} elseif (!empty($customer_info)) {
$data['customitem_id'] = $customer_info['customitem_id'];
} else {
$data['customitem_id'] = 0;
}
$data['product_relateds'] = array();
if(isset($data['customitem_id'])) {
$related_infos = $this->model_sale_customer->getProduct($data['customitem_id']);
foreach($related_infos as $related_info) {
$data['product_relateds'][] = array(
'customitem_id' => $related_info['customitem_id'],
'name' => $related_info['name']
);
}
}
任何帮助都将不胜感激。
tpl文件中的其他代码看起来像这个
$('input[name=''related'']').autocomplete({
'source': function(request, response) {
$.ajax({
url: 'index.php?route=catalog/customitems/autocomplete&token=<?php echo $token; ?>&filter_name=' + encodeURIComponent(request),
dataType: 'json',
success: function(json) {
response($.map(json, function(item) {
return {
label: item['name'],
value: item['customitem_id']
}
}));
}
});
},
'select': function(item) {
$('input[name=''related'']').val('');
$('#product-related' + item['value']).remove();
$('#product-related').append('<div id="product-related' + item['value'] + '"><i class="fa fa-minus-circle"></i> ' + item['label'] + '<input type="hidden" name="customitem_id[]" value="' + item['value'] + '" /></div>')`
和
<div class="form-group">
<label class="col-sm-2 control-label" for="input-related"><span data-toggle="tooltip" title="<?php echo $help_related; ?>"><?php echo $entry_addcustomitem; ?></span></label>
<div class="col-sm-10">
<input type="text" name="related" value="" placeholder="<?php echo $entry_addcustomitem; ?>" id="input-related" class="form-control" />
<div id="product-related" class="well well-sm" style="height: 150px; overflow: auto;">
<?php foreach ($product_relateds as $product_related) { ?>
<div id="product-related<?php echo $product_related['customitem_id']; ?>"><i class="fa fa-minus-circle"></i> <?php echo $product_related['name']; ?>
<input type="hidden" name="customitem_id[]" value="<?php echo $product_related['customitem_id']; ?>" />
</div>
<?php } ?>
</div>
</div>
</div>
当有问题的变量是字符串而不是数组时,会引发错误。在这种情况下为CCD_ 5。$related_info
的var_dump
可以提供帮助。
您的第一个错误"admin/controller/sale/customer.php中的非法字符串偏移量'customitem_id'"很可能您的变量不是数组,而是字符串。
您的第二个错误"注意:未初始化的字符串偏移量:0"表示变量是一个数组,但索引0是相同的问题,变量是字符串,但它是空的
下面的简单测试对每个错误进行注释和取消注释:
$dummyVar="你好";
//错误=>未初始化的字符串偏移量:5//echo$dummyVar[5];
//错误=>非法的字符串偏移量"key name"//echo$dummyVar[关键字名称];
//错误=>未定义的索引:密钥名称$dummyVar2=array();$dummyVar2[0]="某物";//echo$dummyVar2[关键字名称];
//错误=>未初始化的字符串偏移量:0$emptyString='';echo$emptyString[0];