Cartesian algorithm

<?php
error_reporting(0);

if(isset($_POST[“Import”])){

$filename=$_FILES[“file”][“tmp_name”];

if($_FILES[“file”][“size”] > 0)
{

$file = fopen($filename, “r”);

$output .= “<table>”;

while (($getData = fgetcsv($file, 10000, “,”)) !== FALSE)
{
$product_id = $getData[0];
$Accessories = $getData[1];
$Related = $getData[2];
$upc = $getData[3];
$Product_Title = $getData[4];
$Product_category = $getData[5];
$Compare_price = $getData[6];
$Offer_price = $getData[7];
$Commission = $getData[8];
$Wholesale_price = $getData[9];
$Unit_Cost = $getData[10];
$Taxexempt = $getData[11];
$Qty = $getData[12];
$Low_threshhold = $getData[13];
$Warehouse_Location = $getData[14];
$Warehouse_Bin = $getData[15];
$ProductMake = $getData[16];
$ProductModel = $getData[17];
$SummaryDescription = htmlspecialchars($getData[18]);
$DetailedDescription = htmlspecialchars($getData[19]);
$Options = $getData[20];
$Variants = $getData[21];
$Weight = $getData[22];
$Height = $getData[23];
$Length = $getData[24];
$Width = $getData[25];
$img_url = $getData[26];
$Ship_to_customer = $getData[27];
$Pickup_in_store = $getData[28];
$Charge_shipping = $getData[29];
$AccessoriesTabTitle = $getData[30];
$AccessoriesLimit = $getData[31];
$AccessoriesPosition = $getData[32];

$attributesArray1 = explode(“,”, $Variants);
//echo “<pre>attributesArray1:”.print_r($attributesArray1,true).”</pre>”;
if(!empty($attributesArray1)){
foreach($attributesArray1 as $attributes){
$_attributes = str_replace(“\\”, “,”, $attributes);
$attributesArray2[] = explode(“,”, $_attributes);
}
}
//echo “<pre>attributesArray2:”.print_r($attributesArray2,true).”</pre>”;
//echo “<pre>Cartesian product:”.print_r(Cartesian::build($attributesArray2),true).”</pre>”;
foreach(Cartesian::build($attributesArray2) as $a)
{
$price = explode(“|”,implode(‘,’,$a));
$Variants_price = explode(‘,’,$price[1]);

if($Offer_price != ”)
{
$Offer_price = $Offer_price;
}else{
$Offer_price = $Variants_price[0];
}

/*if (strpos($a, ‘|’) != true) {
$b = $a;
}else{
$b = “test”;
} */
$output .= “<tr style=’border:1px solid #000′><td>”.$product_id.”</td><td>”.$Accessories.”</td><td>”.$Related.”</td><td>”.$upc.”</td><td>”.$Product_Title.”</td><td>”.$Product_category.”</td><td>”.$Compare_price.”</td><td>”.$Offer_price.”</td><td>”.$Commission.”</td><td>”.$Wholesale_price.”</td><td>”.$Unit_Cost.”</td><td>”.$Taxexempt.”</td><td>”.$Qty.”</td><td>”.$Low_threshhold.”</td><td>”.$Warehouse_Location.”</td><td>”.$Warehouse_Bin.”</td><td>”.$ProductMake.”</td><td>”.$ProductModel.”</td><td>”.$SummaryDescription.”</td><td>”.$DetailedDescription.”</td><td>”.$Options.”</td><td>”.str_replace(‘+’,’-‘,implode(‘,’,$a)).”</td><td>”.$Weight.”</td><td>”.$Height.”</td><td>”.$Length.”</td><td>”.$Width.”</td><td>”.$img_url.”</td><td>”.$Ship_to_customer.”</td><td>”.$Pickup_in_store.”</td><td>”.$Charge_shipping.”</td><td>”.$AccessoriesTabTitle.”</td><td>”.$AccessoriesLimit.”</td><td>”.$AccessoriesPosition.”</td></tr>”;
$attributesArray2 = ”;
}
}
$output .= ‘</table>’;
header(“Content-Type: application/xls”);
header(“Content-Disposition: attachment; filename=export_excel.xls”);
print $output; exit();
fclose($file);
}
}

class Cartesian
{
public static function build($set)
{
if (!$set) {
return array(array());
}
$subset = array_shift($set);
$cartesianSubset = self::build($set);
foreach ($subset as $value) {
foreach ($cartesianSubset as $p) {
array_unshift($p, $value);
$result[] = $p;
}
}
return $result;
}
}

?>

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s