2013-01-25 178 views
0

嗨,我有一個腳本,其中foreachforeach之內。 第一個foreach用於唯一的id,但第二個foreach用於產品的相關圖像,該圖像應該根據圖像的數量循環。但是,它限制我只獲取一個相關圖像,我如何獲取所有相關圖像?foreach在一個foreach中php

$i=0; 
foreach ($collection as $product_all) { 
    //echo $product_all->getId().'<br/>'; 
    if($i==10) break; 
    $id = $product_all->getId();   
    $neew = Mage::getModel('catalog/product')->load($id);   
    //echo'<pre>'; 
     echo 'active products id ===='.$neew ->getId().'<br/>'; 
     echo 'active products name ===='.$neew->getname().'<br/>';  
     echo 'active products style_ideas ===='.$neew->getstyle_ideas().'<br/>'; 
     echo 'active products size and fit ===='.$neew->getsize_fit().'<br/>'; 
     echo 'active products short description ===='.$neew->getshort_description().'<br/>'; 
     echo 'active products description ===='.$neew->getdescription().'<br/>'; 
     //print_r($neew); 
     if (count($neew->getMediaGalleryImages()) > 0){ 
      $i = 0 ; 
      $j = 0 ; 
      foreach ($neew->getMediaGalleryImages() as $_image){ 
        $relative_image = Mage::helper('catalog/image')->init($neew->getProduct(), 'image', $_image->getFile())->resize(2000); 
        $relative_image1 = str_replace('/webApps/migration/productapi/new/','/',$relative_image); 
        //echo 'relative_image => '.$relative_image1.'<br/>'; 
        $relative_image_save = $relative_image1 ; 
        $relative_image_save1 = explode('/', $relative_image_save);//explode/to get only image name 
        $end1 = end($relative_image_save1); 
        $relative_image3 = $end1; 
        //$handle1 = fopen($relative_image3, 'w') or die('Cannot open file: '. $relative_image); 
        $path12 = '/mnt/aviesta/development/webApps/migration/productapi/new/'.'sku-'.$neew->getsku().'_' .$i++.'.jpg'; 
        copy($relative_image_save, $path12); 
        echo 'relative image with sku path_'.$j++.' ====>'.$path12.'<br/><br/>'; 
       } 
      } 
     $i++; 
} 
+0

什麼'$ neew-> getMediaGalleryImages()'返回? –

+0

您確定MediaGalleryImages集合的大小不是1?在每次迭代中count($ neew-> getMediaGalleryImages())的值是多少? –

回答

0

從你的代碼,我認爲這個問題可以通過你的第二foreach循環之前初始化數組,並存儲陣列中的圖像可以幫助你在獲取所有圖像來解決....這是我明白你的問題。如果這是正確的..,比下面的代碼可以工作....

foreach() 
{ 
    //Your code.... 
    $image_list = array(); 
    $i = 0; 
    foreach() 
     { 
     // Your usual code 
     $image_list[$i] = your_image; 
     //storing images one-by-one in your variable.. 
     $i++; 
     } 
} 

您可以稍後再返回變量或者只是print_r的();得到輸出

0

我想我已經發布問題在小匆忙,腳本工作罰款,初始產品有一個單一的圖像的產品,但對於較新的產品有多個圖像,該腳本工作正常,謝謝所有的迴應帖子.....