2012-02-01 37 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