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

发布于 2025-01-01 11:04:35 字数 6582 浏览 0 评论 0 原文

我有一个导出脚本,可将项目导出到 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;
            }
          }     }

但是,当我导出多个具有附加图像的产品时,上面一行中的图像将跟随到 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

我该怎么做才能使其正常工作?

干杯, Fredrik

编辑 下面是 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');
        }
    }
  }
?>

I have a export script that export items to a csv file. I want to export the additional images for each item on each product row in the csv file.
That part looks like this:

$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;
            }
          }     }

But when I export more than one product that have additional images the images from the line abowe follows to the next line in the csv file.
Heres an exampe of the result. Each item has one additional image:

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

What can I do to get this working?

Cheers,
Fredrik

Edit
Below is the complete section of php that grab the info and generate the export file:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一身软味 2025-01-08 11:04:35

似乎没有足够的代码来给你一个绝对的答案,但我的猜测是变量 $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;
        }
    } 
}

最好的祝福,

杰森

There doesn't seem to be enough of the code to give you an absolute answer but my guess would be that the variable $imageextra2 is not being reset on each iteration.

When you change a product you should reset the variable thats holding the images, which I assume is $imageextra2

Change This

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;
            }
           } 
}

To This

$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;
        }
    } 
}

Best Regards,

Jason

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文