2012-02-01 36 views
0

我有一個將項目導出到csv文件的導出腳本。我想爲csv文件中每個產品行上的每個項目導出額外的圖像。 這一部分看起來是這樣的:導出文件中的foreach循環前綴數據

$img_query=tep_db_query("SELECT additional_images_id, products_id, popup_images 
             FROM " . TABLE_ADDITIONAL_IMAGES . " 
             WHERE products_id=" . $products['products_id'] .""); 

if (!tep_db_num_rows($img_query)) { 
    $imageextra2 = " "; 
    } else { 
    $img_rows=tep_db_num_rows($img_query); 
    while ($img = tep_db_fetch_array($img_query))  { 
      $img2 = $img['popup_images']; 
      $pid = $img['products_id']; 

       $a = array($img2); 
        foreach($a as &$img){ 

        } 
      foreach($a as $imageextra) { 

      $imageextra = "http://www.URL.com/images/". $img2.";"; 
      $imageextra2 .= $imageextra; 
      } 
      }  } 

但是,當我出口超過一個產品,它具有更多的圖片來自行abowe圖像遵循以CSV文件的下一行。這是一個結果的實例。每個項目有一個額外的圖像:

Item-A;AdditionalImage-A.jpg;AdditionalImage-A2.jpg 
Item-B;AdditionalImage-A.jpg;AdditionalImage-A2.jpg;AdditionalImage-B.jpg;AdditionalImage-B2.jpg 
Item-C;AdditionalImage-A.jpg;AdditionalImage-A2.jpg;AdditionalImage-B.jpg;AdditionalImage-B2.jpg;AdditionalImage-C.jpg;AdditionalImage-C2.jpg 

我能做些什麼來得到這個工作?

乾杯, 弗雷德裏克

編輯 下面是抓住信息並生成導出文件PHP的完整部分:

<?php 
    require('includes/application_top.php'); 

    if (isset($_POST['create'])) { 
    if (isset($_POST['product']) && is_array($_POST['product'])) { 

     foreach ($_POST['product'] as $product_id) { 
      $products_query_raw = "select p.products_id, p.products_image, p.products_model, products_image_pop, p.products_price, p.products_quantity, p.products_tax_class_id, pd.products_name, pd.products_description from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd where p.products_id = $product_id and pd.products_id = p.products_id and pd.language_id = $languages_id"; 
      $products_query = tep_db_query($products_query_raw); 
      if ($products = tep_db_fetch_array($products_query)) { 

//++++ QT Pro: End Changed Code 
    $products_attributes_query = tep_db_query("select count(*) as total from " . TABLE_PRODUCTS_OPTIONS . " popt, " . TABLE_PRODUCTS_ATTRIBUTES . " patrib where patrib.products_id='" . $products['products_id'] . "' and patrib.options_id = popt.products_options_id and popt.language_id = '" . (int)$languages_id . "'"); 
    $products_attributes = tep_db_fetch_array($products_attributes_query); 
    if ($products_attributes['total'] > 0) { 
//++++ QT Pro: Begin Changed code 
     $products_id = $products['products_id']; 
     require_once(DIR_WS_CLASSES . 'pad_single_radioset_print.php'); 
     $class = 'pad_single_radioset'; 
     $pad = new $class($products_id); 
     $attribs .= $pad->draw(); 
//++++ QT Pro: End Changed Code 
    } 

       $product_desc = (isset($_POST['strip_tags']) && ($_POST['strip_tags'] == '1')) 
           ? strip_tags($products['products_description'],'<br>, <li>, </li>, <ul>, </ul>') //ORG 
           : $products['products_description']; 

       $tax_rate = 0; 
       $taxes_query_raw = "select tax_rate from " . TABLE_TAX_RATES . " where tax_class_id = " . $products['products_tax_class_id']; 
       $taxes_query = tep_db_query($taxes_query_raw); 
       while ($taxes = tep_db_fetch_array($taxes_query)) { 
        $tax_rate += $taxes['tax_rate']; 
       } 
       $tax = ($products['products_price'] * $tax_rate)/100; 
       $stock = $products['products_quantity']; 

       $product_desc=preg_replace("/([[:space:]]{2,})/",' ',$product_desc); 

// Categories - just show the sub-category  
       $categories_list = Array(); 
       $categories_query_raw = "select cd.categories_name, cd.categories_id from " . TABLE_PRODUCTS_TO_CATEGORIES . " ptc, " . TABLE_CATEGORIES_DESCRIPTION . " cd where ptc.products_id = " . $products['products_id'] . " and cd.categories_id = ptc.categories_id and cd.language_id = $languages_id"; 
       $categories_query = tep_db_query($categories_query_raw); 
       while ($categories = tep_db_fetch_array($categories_query)) { 
        $categories_list[] = $categories['categories_name']; 
        $categories_id = $categories['categories_id']; 
       } 
       $category_name = implode('/', $categories_list); 

// Additional images  
     $img_query=tep_db_query("SELECT additional_images_id, products_id, popup_images 
              FROM " . TABLE_ADDITIONAL_IMAGES . " 
              WHERE products_id=" . $products['products_id'] .""); 

    if (!tep_db_num_rows($img_query)) {  
     $imageextra2 = "; ; "; 

    } else { 
     $img_rows=tep_db_num_rows($img_query); 

     while ($img = tep_db_fetch_array($img_query))  { 
       $img2 = $img['popup_images']; 
       $pid = $img['products_id']; 

       $a = array($img2); 

       $imageextra2 = " "; # This avoid letting the additional images from lines abowe follow. 

       foreach($a as $imageextra) { 
       $imageextra = "http://www.URL.com/images/". $img2.";"; 
       $imageextra2 .= $imageextra; 
       } 
       } 
} 

       $export .= 
          $products['products_model'].';'. 
          $products['products_name'].';'. 
          $product_price.';'. 
          $products['products_quantity'].';'. 
          $product_desc.';'. 
          $categories_id.';'. 
          $shipping_cost.';'. 
          'http://www.URL.com/images/'.basename($products['products_image_pop']).';'. 
          $imageextra2. 
          "\n"; 
      } 
     } 


     if ($fp = @fopen($_SERVER['DOCUMENT_ROOT'] . '/feed/t-butik.csv', 'w')) { 
      $utf = iconv("windows-1252","ISO-8859-1",$export); 
      $out = 'variable_name='.$utf; 
      fwrite($fp, $utf); 
      fclose($fp); 
      $messageStack->add("Feed generates successfully!!!", 'success'); 
     } else { 
      $messageStack->add("ERROR: Permissions error trying to write feed to disk.", 'error'); 
     } 
    } 
    } 
?> 
+0

如果將其刪除,那麼即使該項目有多個附加圖像,導出文件中也只會顯示其中一幅附加圖像。 但這可能是錯誤的做法。 – 2012-02-01 20:35:17

+0

是的,錯誤的方式。你正在做的就是循環訪問一個數組,並且完全不做任何事情。在那個循環之後,一切仍然是進入循環之前的方式。 – 2012-02-01 20:59:19

+0

第一次取消註釋時,我一定犯了些錯誤。我再次刪除它,你完全正確。它沒有必要在那裏。 – 2012-02-02 14:35:16

回答

0

似乎有沒有足夠的代碼給你一個絕對的答案,但我的猜測是變量$ imageextra2沒有被重置在每次迭代。

當你改變一個產品,你應該重新設置變量這就是抱着圖片,我以爲是$ imageextra2

改變這種

if (!tep_db_num_rows($img_query)) {  
    $imageextra2 = "; ; "; 

} else { 
    $img_rows=tep_db_num_rows($img_query); 

    while ($img = tep_db_fetch_array($img_query))  { 
      $img2 = $img['popup_images']; 
      $pid = $img['products_id']; 

      $a = array($img2); 

      $imageextra2 = " "; # This avoid letting the additional images from lines abowe follow. 

      foreach($a as $imageextra) { 
      $imageextra = "http://www.URL.com/images/". $img2.";"; 
      $imageextra2 .= $imageextra; 
      } 
      } 
} 

爲了這

$imageextra2 = " "; # resets images 

if (tep_db_num_rows($img_query)) {  
    $img_rows=tep_db_num_rows($img_query); 

    while ($img = tep_db_fetch_array($img_query))  { 
     $img2 = $img['popup_images']; 
     $pid = $img['products_id']; 

     $a = array($img2);     

     foreach($a as $imageextra) { 
      $imageextra = "http://www.URL.com/images/". $img2.";"; 
      $imageextra2 .= $imageextra; 
     } 
    } 
} 

最好的問候,

Jason

+0

我試過 reset($ a); 和 reset($ imageextra2); 但他們都沒有工作。 它需要更多的代碼來重置它嗎? $ imageextra2是我在決定應該在導出文件中的行中調用的內容。 如果我發佈更多的代碼會有幫助嗎? – 2012-02-02 14:43:26

+0

是的,它可能會更容易幫助,但reset()實際上只是將數組倒回開始。你想通過執行$ imageextra2 =''來重置處理圖像的變量。在每個產品的循環開始。 – 2012-02-03 14:59:25

+0

我試過$ imageextra2 =''; 當我添加該行時,即使產品有多個附加圖像,也只有每個產品的其中一幅附加圖像被導出。但是,它至少是產品的正確形象。 我使用生成導出的代碼的完整部分編輯我的帖子。 實際上,// ++++ QT Pro會產生與附加圖像相同的錯誤,abowe中的屬性會跟隨到下一行。 – 2012-02-06 08:39:26