0

PHP와 젠드 프레임 워크를 처음 사용합니다. 오류를 만났습니다 :Notice : foreach 구문에서 정의되지 않은 인덱스 'itemid'

Notice: Undefined index: itemid in C:\xampp\htdocs\blogshop\application\views\scripts\item\tops.phtml on line 58

나는이 오류가 나타나는 이유를 모르겠습니다.

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 
} 

이것은 내 컨트롤러 코드입니다.

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(); 
} 

내 데이터베이스에서 카테고리 '탑'이있는 행을 가져 오기위한 모델의 내 getTops 작업입니다.

<?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; ?> 

내가 내 데이터베이스에있는 모든 행을 표시하려면 코드입니다.

답변

2

$this->item 배열에 itemid이라는 색인이 없기 때문에 오류가 발생합니다.

<?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; ?> 

foreach 문 내부의 모든 $this->item이 작동 할 수있는 반복에 대해 $item로 대체해야합니다

또한, 코드는 여기에 조금 잘못된 것 같다. 따라서 $item['itemid'], $item['itemname'] 등입니다. 배열에 더 깊이 들어가려면 누락되었습니다. 반복을 렌더링하면 foreach은 쓸모 없게됩니다.

나는 $this->item이 같은 형태의 추측 : 존재하지 않는

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

이, 왜 $this->item['itemid'] 반환에 불과하다. $this->item[1]['itemid'] 그러나 입니다. foreach주기가 도움이되는 것은 사이클 내에서 $item으로 표시된 각 값을 사용하여 전체 $this->item 배열을 걷는 (반복하는) 것입니다. 첫 번째 실행에서는 $item$this->item[1]이고 두 번째 경우는 $item$this->item[2] 인 식으로 나타납니다.

따라서 foreach 구조 안에 $this->item$item으로 변경하십시오.

+0

좋아, 이제 작동하지. 고마워요 :) –

+0

@SwapTest 도와 드릴 수있어서 기쁩니다. 그러나 제발, 당신이 사용하고있는 것을 이해하고 시간을내어주십시오. ([foreach] (http://php.net/manual/en/control-structures.foreach.php) 관련 매뉴얼 페이지) 내가해야할 일을 말했고 내 _fix_를 파일에 병합 한 것이 좋지만 왜 그것이 효과가 있는지 이해하지 못한다면, 그것을 할 때 생산 가치가 있습니다. 또한 이것이 좋은 대답이라면 옆에 투명한 _tick_으로 답을 표시하여 독자들이이 대답이 효과가 있다는 것을 알게 될 것입니다. – Whisperity