dpq755012465 2012-08-08 11:47
浏览 91
已采纳

注意:foreach构造中的未定义索引'itemid'

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.

  • 写回答

1条回答 默认 最新

  • douye9822 2012-08-08 11:57
    关注

    There is no index named itemid in your $this->item array, this is why you get the error.

    Also, your code here seems to be a bit wrong:

    <?php foreach($this->item as $item) : ?>
        <?php echo $this->escape($this->item['itemid']);?>
        <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; ?>
    

    Every $this->item inside the foreach statement should be replaced with $item for 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 iteration foreach useless.

    I guess $this->item looks something like this:

    array (
      1 => 
      array (
        'itemid' => 1,
        'itemname' => 'foobar',
      ),
      2 => 
      array (
        'itemid' => 2,
        'itemname' => 'bazqux',
      ),
    )
    

    This is why $this->item['itemid'] returns nothing, as it does not exist. $this->item[1]['itemid'] however does. What the foreach cycle helps you to do is that it walks (iterates) the whole $this->item array with each value represented as $item inside the cycle. In the first run, $item is $this->item[1], in the second, $item is $this->item[2], and so on, and so forth.

    So, change $this->item to $item inside the foreach construct.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多