2014-02-12 71 views
0

我正在嘗試解析未定義的偏移量。我有谷歌並且通過堆棧溢出來看,但是我發現的例子不適用,或者對於我目前熟悉的人來說太複雜了。我非常環保,但我在做出努力之前,我曾經保證過要求幫助和智慧。函數返回未定義的偏移量錯誤

這是因爲它現在存在於功能:

function PrintFolio($aaPh) //aaPh =associative array place holder 

{ 
//print out X number rows of unto 4 images from array with X items 
//if array had 7 items, print one row of 4 next row 3 
//if array had 16 items, print 4 rows of 4, etc. 
    //if array had 13 items, print 3 rows of 4, final row with one item 

$itemsCount = sizeof($aaPh);//get size of array 

$height = (int)ceil(sizeof($aaPh)/4);//rounded up division by 4 

//$height = $height + 1; 
$keys = array_keys($aaPh); //get keys 

//loop through array of X items, for each group of 4 print as a row 
for($row = 0; $row < $height; $row++) //looping through the rows 

    { 
     echo '<div class="row flush">'; //open div 
     for($image = 0; $image < 4; $image++) //each row is composed of 4 images 
     { 
      $aaPhIndex = array_keys($aaPh)[$row*4+$image]; //if associative array 
      if($row*4+$image < $itemsCount) { 
       $aaPhIndex = $keys[$row*4+$image]; 
       printf(TEMPLATE_PORTFOLIO, $aaPhIndex, $aaPh[$aaPhIndex]); 
       //$template = '<div class="3u"><a href="_img/fulls/%1$s" class="image full"><img src="_img/thumbs/%1$s" alt="" title="%2$s" /></a></div>'; 
      } 
     } 

     echo '</div>'; //end div group of 4 
    }//end loop 
}//end function 

它帶有一個數組,切片它在到的四個單元,然後打印該陣列爲一系列圖像到屏幕。但是,如果我沒有一個數字是4可以設置的,它將顯示任何未定義的偏移量錯誤(我希望我正確地說)。

  • 我的目標是不會有不確定的偏移誤差打印到這些屏幕,不修改我的網站錯誤報告功能(我認爲這樣做會被欺騙,因爲它不會解決問題,只是將其隱藏它沒有按」對我來說似乎非常重要)。

回答

1

你爲什麼不只是把斷行各4元后?

function PrintFolio($aaPh) //aaPh =associative array place holder 
{ 
//loop through the array 
for($row = 0; $row < Count($aaPh); $row++) //looping through the rows 
{ 
    if (!$row || !($row%4)) { 
     if ($row) echo '</div>'; // close previously opened divs 
     echo '<div class="row flush">'; //open div 
    } 
    /* show your picture code here */ 

}//end loop 
echo '</div>'; 
}//end function 

這是您的意見後的更新。 因爲你不需要你的關聯數組,只是按鍵的值,該代碼可以進行如下更改:

function PrintFolio($aaPh) //aaPh =associative array place holder 
{ 
//loop through the array 
$row=0; 
foreach(array_keys($aaPh) as $img) //looping through array keys only 
{ 
    if (!$row || !($row%4)) { 
     if ($row) echo '</div>'; // close previously opened divs 
     echo '<div class="row flush">'; //open div 
    } 
    echo '<img src="' . $img . '">'; 
    $row++; // increase row counter. 
}//end loop 
echo '</div>'; 
}//end function 

確保把正確的路徑到標籤

+0

我跑了這一點,當我這樣做,不返回任何補償,但不返回任何圖像或者(所以我一定不能正確理解的東西時,我代替我,如果你如果)。那是你建議我做的,如果你的建議取代了我的意見? – Chezshire

+0

你用你的圖片輸出代碼替換了我的(/ *在這裏顯示你的圖片代碼* /)嗎? :) – cyadvert

+0

請給出一個示例數據$ aaPh請 – cyadvert

1

既然你不能假設你總是會有一些可以被四整除的元素,你需要測試每一個元素。在您計算索引的內部循環中,然後使用它來引用數組,只需添加一個檢查以確保數組元素存在。從這:

$aaPhIndex = $keys[$row*4+$image]; 
printf(TEMPLATE_PORTFOLIO, $aaPhIndex, $aaPh[$aaPhIndex]); 

要這樣:

$aaPhIndex = $keys[$row*4+$image]; 
if (isset($aaPh[$aaPhIndex])) { 
    printf(TEMPLATE_PORTFOLIO, $aaPhIndex, $aaPh[$aaPhIndex]); 
} 
+0

哦?真? (他回憶說,前幾天他讀到的是「isset」,但顯然沒有完全內化「isset」)。謝謝。我讀過'array_values',希望我的回答可能在那裏。當你建議並回報時,我會嘗試與'小島'合作。再次謝謝你。 – Chezshire

+0

嗯。我認爲我沒有正確直覺你所指示的。我按照你的建議修改了我的代碼,但仍然收到錯誤信息。我印象深刻的是,你的建議縮短了我的代碼的線 - 這很酷。我的代碼現在讀爲:'$ aaPhIndex = $ keys [$ row * 4 + $ image]; \t \t \t \t \t如果(isset($ AAPH [$ aaPhIndex])){ \t \t \t \t \t的printf(TEMPLATE_PORTFOLIO,$ aaPhIndex,$ AAPH [$ aaPhIndex]); \t \t \t \t}」 – Chezshire

1

在你的邏輯錯誤是,在這條線上

for($image = 0; $image < 4; $image++) //each row is composed of 4 images 

你假設總是有4張圖片。但正如你自己所說的,如果數組有13個項目,那麼最後一行將只包含一個圖像。這將導致$aaPhIndex = array_keys($aaPh)[$row*4+$image];訪問不存在的數組索引。

要解決此問題,您必須修改$image < 4以解釋最後一行,或者只檢查索引是否不超過項目數。例如通過將錯誤行的情況下,你已經寫道:

if($row*4+$image < $itemsCount) { 
    $aaPhIndex = array_keys($aaPh)[$row*4+$image]; //if associative array 
    $aaPhIndex = $keys[$row*4+$image]; 
    printf(TEMPLATE_PORTFOLIO, $aaPhIndex, $aaPh[$aaPhIndex]); 
    //$template = '<div class="3u"><a href="_img/fulls/%1$s" class="image full"><img src="_img/thumbs/%1$s" alt="" title="%2$s" /></a></div>'; 
} 

希望這個作品