Airsea Airsea - 1 month ago 18
PHP Question

Warning: Invalid argument supplied for foreach()in /app/design/adminhtml/default/default/template/canonical/canonical/edit/tab/header.phtml on line 79

what should i do to fix this error ?


ERR (3): Warning: Invalid argument supplied for foreach() in /app/design/adminhtml/default/default/template/canonical/canonical/edit/tab/header.phtml on line 79


<?php if ( Mage::getSingleton('admin/session')->isAllowed('admin/global_search') ): ?>
<fieldset>
<legend>Search</legend>
<span id="global_search_indicator" class="autocomplete-indicator" style="display: none">
<img src="<?php echo $this->getSkinUrl('images/ajax-loader.gif') ?>" alt="<?php echo $this->__('Loading...') ?>" class="v-middle"/>
</span>
<?php $defSearch = $this->__('Global Record Search') ?>
<input id="global_search" name="query" type="text" class="input-text" value="<?php if(!empty($query)): ?><?php echo $query ?><?php else: ?><?php echo $defSearch ?><?php endif ?>" onfocus="if(this.value=='<?php echo $defSearch ?>')this.value=''; " onblur="if(this.value=='')this.value='<?php echo $defSearch ?>';" />
<div id="global_search_autocomplete" class="autocomplete"></div>
<script type="text/javascript">
new Ajax.Autocompleter(
'global_search',
'global_search_autocomplete',
'<?php echo $this->getUrl('adminhtml/index/globalSearch') ?>',


{
paramName:"query",
minChars:2,
indicator:"global_search_indicator",
updateElement:getSelectionId,
evalJSON:'force'
}
);
function getSelectionId(li) {
location.href = li.getAttribute('url');
}


</script>
</fieldset>
<?php endif; ?>
</div>
</div>
<?php if(Mage::getStoreConfig('advanced/modules_disable_output/FME_Canonical')==0) : ?>
<?php


$product_id = Mage::app()->getRequest()->getParam('id');
$_product = Mage::getModel('catalog/product')->load($product_id);
$selected = $_product->getData('fme_canonicalurl');
$catIds = $_product->getCategoryIds();
foreach ($catIds as $key )
{
$catCollection = Mage::getResourceModel('catalog/category_collection')
->addAttributeToFilter('entity_id', $key);
foreach($catCollection as $cat)
{
$dat[] = $cat->getId();
}
}


?>



<script type="text/javascript">

window.onload = function () {
if(categories = document.getElementById("fme_canonicalurl"))


{
var categories = document.getElementById("fme_canonicalurl");
var newOption = document.createElement('option');
newOption.text = "<?php echo $_product->getUrlPath() ?>";
newOption.setAttribute('value', "<?php echo $_product->getUrlPath() ?>");
categories.appendChild(newOption);
<?php

foreach ($dat as $value ) {
echo "var newOption = document.createElement('option');";
$_category = Mage::getModel('catalog/category')->load($value);
$url = $_product->getUrlPath($_category); ?>
newOption.text = "<?php echo $url ?>" ;
newOption.setAttribute('value', '<?php echo $url ?>');
categories.appendChild(newOption);
<?php
}
?>
function setSelectedIndex(s, v) {
for ( var i = 0; i < s.options.length; i++ ) {
if ( s.options[i].value == v ) {
s.selectedIndex = i;
return;
}
}
}
setSelectedIndex(categories,"<?php echo $selected?>");
}

}

Answer

Your problem is that you're pushing data into the $dat array from within a loop, but you didn't initialize it first. In the case where no results are looped and no data is pushed to $dat, it is then undefined when you try and loop over $dat later on.

Initialize it first:

$catIds = $_product->getCategoryIds();
$dat = array();
foreach ($catIds as $key) {  
    $catCollection = Mage::getResourceModel('catalog/category_collection')
        ->addAttributeToFilter('entity_id', $key);

    foreach ($catCollection as $cat) {
        $dat[] = $cat->getId();
    }
}