PHP:for循环中未定义的偏移量

时间:2014-01-26 16:25:35

标签: php for-loop undefined offset

在制作照片库时我遇到了问题。每张照片我试着显示它有多少评论,但是如果一张照片有0条评论,它会给我一个“未定义的偏移”错误。我不知道我做错了什么,因为它确实显示有0条评论。

这是与问题相关的代码: (问题出现在行中:if($reacties[$i]==0){

if((isset($_GET['vanafFoto'])) AND (intval($_GET['vanafFoto']>=0)) AND (intval($_GET['vanafFoto'] < $countFotos))){ 
        $begin = intval($_GET['vanafFoto']); 

       if(($begin + $aantalFotos) <= $countFotos){ 
                     $eind = ($begin + $aantalFotos); 
           } // end if 
      else { 
                   $eind = $countFotos; 
           } // end else 

} // end if 
else { 
     $begin = 0; 
     $eind = $aantalFotos; 
} // end else 

$countFotos = count($fotoArray); 


                                                 // path naar echte foto 

} // end else 

echo "<table border='0' cellpadding='0' cellspacing='2'><tr><td ><b>" . $pathspatie . "</b> <small>(" . $count . ")</small> 
<br><br><center><small>Pictures " . ($begin + 1) . " - " . $eind . "</small></center></td></tr></table>"; 



if(($begin - $aantalFotos) >= 0){ 
             $navigation = "<a href='" . $_SERVER['PHP_SELF'] . "?page=album&boek=" . $originalPath . "&vanafFoto=" . ($begin - $aantalFotos) . "'><</a> " . $navigation;             
    } // end if 


if(($begin + $aantalFotos) < $count){ 
             $navigation .= " <a href='" . $_SERVER['PHP_SELF'] . "?page=album&boek=" . $originalPath . "&vanafFoto=" . ($begin + $aantalFotos) . "'>></a>";             
   } // end if 

echo $navigation . "<br><br>"; 
echo "</td></tr><tr>"; 
$fotonr = 1; 
for($i=$begin; $i < $eind; $i++){ 


$thumb = str_replace($path2, $thumbPath, $fotoArray[$i]); 


    echo "<td align='center'><a href='" . $_SERVER['PHP_SELF'] . "?page=album&boek=" . $originalPath . "&fotoID=" . $i . "'><img border='0' src='" . $thumb . "' height='100'><br>"; 
echo "<small>reacties ("; 
if($reacties[$i]==0){     // error occurres here.
    echo "0"; 
    } // end if 
else { 
    echo $reacties[$i]; 
    } // end else     
echo ")</small>"; 
echo "</a></td>"; 
    $fotonr++; 
    if($fotonr == ($clm + 1)){ 
    echo "</tr>\n<tr>"; 
     $fotonr = 1; 
} // end if 




} // end for 

如果有人能看出问题是什么,那就太棒了!

1 个答案:

答案 0 :(得分:2)

我不明白你的确切目标,但也许再写一次检查更好:

if(!isset($reacties[$i]) || $reacties[$i]==0){
    echo "0"; 
}