I am new to PHP and Zend Framework. I met the error:
Notice: Undefined index: itemid in C:\xampp\htdocs\blogshop\application\views\scripts\item\tops.phtml on line 58
I don’t get why this error shows up.
public function topsAction() //tops action
{
//$tops = new Application_Model_DbTable_Item();
//$tops->getTops();
$item = new Application_Model_DbTable_Item(); //create new Item object
$this->view->item = $item->getTops(); //$this->view->item is pass to index.phtml
}
This is my controller code.
public function getTops()
{
$row = $this->fetchAll('itemtype = "Tops"'); //find Row based on 'Tops'
if (!$row) { //if row can't be found
throw new Exception("Could not find Tops!"); //Catch exception where itemid is not found
}
return $row->toArray();
}
This is my getTops action in Model to get the rows with category ‘Tops’ in my database.
<?php foreach($this->item as $item) : ?>
<?php echo $this->escape($this->item['itemid']);?> // This is where the error happens
<img src="<?php echo $this->escape($item->image);?>" width="82" height="100">
<?php echo $this->escape($this->item['itemname']);?>
<?php echo $this->escape($this->item['description']);?>
<?php echo $this->escape($this->item['itemtype']);?>
<?php endforeach; ?>
This is my code to display all the rows I have in my database.
There is no index named
itemidin your$this->itemarray, this is why you get the error.Also, your code here seems to be a bit wrong:
Every
$this->iteminside theforeachstatement should be replaced with$itemfor the iteration to work. So it will be$item['itemid'],$item['itemname'], etc. You are missing to get a level deeper into the array, rendering the iterationforeachuseless.I guess
$this->itemlooks something like this:This is why
$this->item['itemid']returns nothing, as it does not exist.$this->item[1]['itemid']however does. What theforeachcycle helps you to do is that it walks (iterates) the whole$this->itemarray with each value represented as$iteminside the cycle. In the first run,$itemis$this->item[1], in the second,$itemis$this->item[2], and so on, and so forth.So, change
$this->itemto$iteminside theforeachconstruct.