I have a script that exports the contents of a product table into a tab delimited file (to be used as a datafeed). For the image filename, I have to add a path before the file name and have used the following code:
if ((eregi(".jpg", $value)) OR (eregi(".gif", $value))) {
$value = str_replace('"', '""', $value);
$value = str_replace("\r", '', $value);
$value = str_replace("\n", '', $value);
$value = '"http://www.sunfriendlyproducts.com/cart/images/' . $value . '"' . "\t";
This works fine, except that if the product description field also contains an image, it is doing the replace there.
How can I only make this function work for that one particular column name? or is there a better way to do this...
The entire script is below:
<? require ("config.inc");
/*****************************************
Write the query, call it, and find the number of fields
/**************************************/
$select = "SELECT name, description, group_no, name, price, image, category1, category2, category3, keywords, sale_price, oversized, option1, option1_value, option2, option2_value FROM scdcart where active='yes'";
$export = mysql_query($select);
$count = mysql_num_fields($export);
/**************************************
Extract field names and write them to the $header
variable
/*****************************************/
$output_header = array('GROUP_NAME', 'GROUP_DESCRIPTION', 'LINE_ITEM_CODE', 'LINE_ITEM_NAME', 'LINE_ITEM_PRICE', 'IMAGE_REFERENCE', 'FIRST_LEVEL_DEPARTMENT', 'SECOND_LEVEL_DEPARTMENT', 'THIRD_LEVEL_DEPARTMENT', 'KEYWORDS', 'LINE_ITEM_SALE_PRICE', 'PER_PRODUCT_SHIPPING', 'OPTION_TYPE1', 'OPTION_VALUE1', 'OPTION_TYPE2', 'OPTION_VALUE2');
for ($i = 0; $i < $count; $i++) {
$header .= $output_header[$i] ."\t";
}
/*****************************************
Extract all data, format it, and assign to the $data
variable
/*****************************************/
while($row = mysql_fetch_row($export)) {
$line = '';
foreach($row as $value) {
if ((!isset($value)) OR ($value == "")) {
$value = "\t";
} else {
if ((eregi(".jpg", $value)) OR (eregi(".gif", $value))) {
$value = str_replace('"', '""', $value);
$value = str_replace("\r", '', $value);
$value = str_replace("\n", '', $value);
$value = '"http://www.sunfriendlyproducts.com/cart/images/' . $value . '"' . "\t";
} else {
$value = str_replace("'", '""', $value);
$value = str_replace("\r", '', $value);
$value = str_replace("\n", '', $value);
$value = '"' . $value . '"' . "\t";
}}
$line .= $value;
}
$data .= trim($line)."\n";
}
/*****************************************
Set the default message for zero records
/*****************************************/
if ($data == "") {
$data = "\n(0) Records Found!\n";
}
/*****************************************
Set the automatic downloadn section
/*****************************************/
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"altura" . date("Ymd") . ".txt");
header("Pragma: no-cache");
header("Expires: 0");
print "$header\n$data";
?>