foreach循环导出文件中的先前数据

时间:2012-02-01 18:37:24

标签: php loops foreach export

我有一个将项目导出到csv文件的导出脚本。我想导出csv文件中每个产品行上每个项目的其他图像。 那部分看起来像这样:

$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
  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');
        }
    }
  }
?>

1 个答案:

答案 0 :(得分: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;
        }
    } 
}

最诚挚的问候,

杰森