duanjupiao4730 2016-12-28 07:08
浏览 19
已采纳

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

I want to hide the error of Undefined offset, of course best if it could be resolved.

The code shows the images I wanted.(3 images per page). Click page 1 show the image with number 001,002,003 page 2 show the image with number 004,005,006 etc....

My codes

 $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条回答 默认 最新

  • dongyangben6144 2016-12-28 07:17
    关注

    Add isset to check element in array is present or not

    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> ";
      }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#.net#的问题:End Function
  • ¥50 用AT89C52单片机设计一个温度测量与控制电路
  • ¥15 无法import pycausal
  • ¥15 VS2022创建MVC framework提示:预安装的程序包具有对缺少的注册表值的引用
  • ¥15 weditor无法连接模拟器Local server not started, start with?
  • ¥20 6-3 String类定义
  • ¥15 嵌入式--定时器使用
  • ¥20 51单片机学习中的问题
  • ¥30 Windows Server 2016利用兩張網卡處理兩個不同網絡
  • ¥15 Python中knn问题