2015-11-11 57 views
0

偏移'我得到一個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>

+0

此代碼不會導致此類錯誤。您必須顯示錯誤發生的行號 – Croll

+0

錯誤行是942行9​​41警告:非法字符串偏移'名稱'在'和'在線942警告:非法字符串偏移'customitem_id'' –

+0

增加了額外的代碼原問題 –

回答

1

錯誤被拋出時,所涉及的變量是一個字符串,而不是一個陣列。在這種情況下爲$related_info。 A var_dump$related_info可以提供幫助。

+0

這**可能是問題 - '$ related_info'是一個字符串。然而,這是作爲評論的措辭,而不是一個答案。請編輯重新解釋您的答案,或者刪除它並發表評論 – Steve

0

您的第一個錯誤「admin/controller/sale/customer.php中的非法字符串偏移'customitem_id'」很可能不是您的變量,而是一個字符串。

你的第二個錯誤「注意:未初始化字符串偏移量:0」表示變量是一個數組,但該指數0是同一個問題的變量是一個字符串,但它是空的

簡單的測試,下面的評論並取消每個錯誤:

$dummyVar = "Hello";

// Error => Uninitialized string offset: 5 //echo $dummyVar[5];

// Error => Illegal string offset 'key-name' //echo $dummyVar['key-name'];

//Error => Undefined index: key-name $dummyVar2 = array(); $dummyVar2[0] = 'something'; //echo $dummyVar2['key-name'];

// Error => Uninitialized string offset: 0 $emptyString = ''; echo $emptyString[0];

相關問題