php error hide / solve error未定义的偏移量

时间:2016-12-28 07:08:37

标签: php

我想隐藏未定义偏移的错误,当然最好能解决它。

代码显示我想要的图像。(每页3张图片)。 单击第1页显示编号为001,002,003的图像 第2页显示数字004,005,006等图像....

我的代码

 $catCode = isset($_GET["cat"]) ? $_GET["cat"] : "ac"; //getting url
        $upperCatCode = strtoupper($catCode);
        $arr = [];

        //to get my images based on their image number
        foreach ($productArr[$catCode] as $imgNumber => $productDetail) {
            array_push($arr, $imgNumber);
            $imgNumber = $arr;
        } 

        echo"<div align='center'>  
        <ul class='pagination'>";

        // getting the total pages I need for the category if i want to display 3 images per page
        for ($i = 1; $i < count($productArr[$catCode]) / 3 + 1; $i++) {
            echo"<li><a href='http://localhost/ca1/collectionPage.php?cat={$catCode}&page={$i}'> {$i}</a></li>
         ";
        }
        echo"<li><a href='http://localhost/ca1/collectionSummary.php?cat={$catCode}'>view all</a></li>";
        echo"</ul>
        </div>";

// the loop i used to loop through the images
for ($i = 0; $i < count($productArr[$catCode]); $i++) {
        // count the total number of images
        $total = count($arr);
        // limit the number of images shown
        $limit = 3;
        //calculate the total number of pages
        $totalPages = ceil($total / $limit);
        $page = max($page, 1); //get 1 page when $_GET['page'] <= 0
        $page = min($page, $totalPages); //get last page when $_GET['page'] > $totalPages
        $offset = ($page - 1) * $limit;
        if( $offset < 0 ) {$offset = 0;}
        $newArr = array_slice( $arr, $offset, $limit );

            echo "<div class='col-md-4'>
                <div class='thumbnail'>
                    <a href='http://localhost/ca1/collectionDetail.php?cat={$catCode}&itemCode={$newArr[$i]}'><img class='img-responsive image_size' src='images/$catCode/row_{$catCode}{$newArr[$i]}_m0.jpg'></a>
                    <div class='caption'>
                        <p align='center'><b>Model No.Row-{$upperCatCode}-{$newArr[$i]}</b></p>
                    </div>
                </div>
            </div> ";

1 个答案:

答案 0 :(得分:0)

添加isset以检查数组中的元素是否存在

if(isset($newArr[$i]) && $newArr[$i] ){
 echo "<div class='col-md-4'>
            <div class='thumbnail'>
                <a href='http://localhost/ca1/collectionDetail.php?cat={$catCode}&itemCode={$newArr[$i]}'><img class='img-responsive image_size' src='images/$catCode/row_{$catCode}{$newArr[$i]}_m0.jpg'></a>
                <div class='caption'>
                    <p align='center'><b>Model No.Row-{$upperCatCode}-{$newArr[$i]}</b></p>
                </div>
            </div>
        </div> ";
  }