Storing unique values into an array and comparing against a loop - PHP
- by Aphex22
I'm writing a PHP report which is designed to be exported purely as a CSV file, using commma delimiters.
There are three columns relating to product_id, these three columns are as follows:
SKU Parent / Child Parent SKU
12345 parent 12345
12345_1 child 12345
12345_2 child 12345
12345_3 child 12345
12345_4 child 12345
18099 parent 18099
18099_1 child 18099
Here's a link to the full CSV file:
http://i.imgur.com/XELufRd.png
At the moment the code looks like this:
$sql = "select * from product WHERE on_amazon = 'on' AND active = 'on'";
$result = mysql_query($sql) or die ( mysql_error() );?>
<?
// set headers
echo "
Type,
SKU,
Parent / Child,
Parent SKU,
Product name,
Manufacturer name,
Gender,
Product_description,
Product price,
Discount price,
Quantity,
Category,
Photo 1,
Photo 2,
Photo 3,
Photo 4,
Photo 5,
Photo 6,
Photo 7,
Photo 8,
Color id,
Color name,
Size name
<br>
";
// load all stock
while ($line = mysql_fetch_assoc($result) ) {
?>
<?php
// Loop through each possible size variation to see whether any of the quantity column has stock > 0
$con_size = array (35,355,36,37,375,38,385,39,395,40,405,41,415,42,425,43,435,44,445,45,455,46,465,47,475,48,485);
$arrlength=count($con_size);
for($x=0;$x<$arrlength;$x++) {
// check if size is available
if($line['quantity_c_size_'.$con_size[$x].'_chain'] > 0 ) {
?>
<? echo 'Shoes'; ?>,
<?=$line['product_id']?>,
,
,
<?=$line['title']?>,
<?
$brand = $line['jys_brand'];
echo ucfirst($brand);
?>,
<?
$gender = $line['category'];
if ($gender == 'Mens') {
echo 'H';
}
else{
echo 'F';
}
?>,
<?=preg_replace('/[^\da-z]/i', ' ', $line['amazon_desc']) ?>,
<?=$line['price']?>,
<?=$line['price']?>,
<?=$line['quantity_c_size_'.$con_size[$x].'_chain']?>,
<?
$category = $line['style1'];
switch ($category) {
case "ankle-boots":
echo "10013";
break;
case "knee-high-boots":
echo "10011";
break;
case "high-heel-boots":
echo "10033";
break;
case "low-heel-boots":
echo "10014";
break;
case "wedge-boots":
echo "10014";
break;
case "western-boots":
echo "10032";
break;
case "flat-shoes":
echo "10034";
break;
case "high-heel-shoes":
echo "10039";
break;
case "low-heel-shoes":
echo "10039";
break;
case "wedge-shoes":
echo "10035";
break;
case "ballerina-shoes":
echo "10008";
break;
case "boat-shoes":
echo "10018";
break;
case "loafer-shoes":
echo "10037";
break;
case "work-shoes":
echo "10039";
break;
case "flat-sandals":
echo "10041";
break;
case "low-heel-sandals":
echo "10042";
break;
case "high-heel-sandals":
echo "10042";
break;
case "wedge-sandals":
echo "10042";
break;
case "mule-sandals":
echo "10038";
break;
case "mary-jane-shoes":
echo "10039";
break;
case "sports-shoes":
echo "10026";
break;
case "court-shoes":
echo "10035";
break;
case "peep-toe-shoes":
echo "10035";
break;
case "flat-boots":
echo "10609";
break;
case "mid-calf-boots":
echo "10014";
break;
case "trainer-shoes":
echo "10009";
break;
case "wellington-boots":
echo "10012";
break;
case "lace-up-boots":
echo "10609";
break;
case "chelsea-and-jodphur-boots":
echo "10609";
break;
case "desert-and-chukka-boots":
echo "10032";
break;
case "lace-up-shoes":
echo "10034";
break;
case "slip-on-shoes":
echo "10043";
break;
case "gibson-and-derby-shoes":
echo "10039";
break;
case "oxford-shoes":
echo "10039";
break;
case "brogue-shoes":
echo "10039";
break;
case "winter-boots":
echo "10021";
break;
case "slipper-shoes":
echo "10016";
break;
case "mid-heel-shoes":
echo "10039";
break;
case "sandals-and-beach-shoes":
echo "10044";
break;
case "mid-heel-sandals":
echo "10042";
break;
case "mid-heel-boots":
echo "10014";
break;
default:
echo "";
}
?>,
http://www.getashoe.co.uk/full/<?=$line['product_id']?>_1.jpg, http://www.getashoe.co.uk/full/<?=$line['product_id']?>_2.jpg,
http://www.getashoe.co.uk/full/<?=$line['product_id']?>_3.jpg, http://www.getashoe.co.uk/full/<?=$line['product_id']?>_4.jpg,
,
,
,
,
<?
$colour = preg_replace('/[^\da-z]/i', ' ', $line['colour']);
if( preg_match( '/white.*/i', $colour))
{
echo '1';
}
elseif( preg_match( '/yellow.*/i', $colour))
{
echo '4';
}
elseif( preg_match( '/orange.*/i', $colour))
{
echo '7';
}
elseif( preg_match( '/red.*/i', $colour))
{
echo '8';
}
elseif( preg_match( '/pink.*/i', $colour))
{
echo '13';
}
elseif( preg_match( '/purple.*/i', $colour))
{
echo '15';
}
elseif( preg_match( '/blue.*/i', $colour))
{
echo '19';
}
elseif( preg_match( '/green.*/i', $colour))
{
echo '25';
}
elseif( preg_match( '/brown.*/i', $colour))
{
echo '28';
}
elseif( preg_match( '/grey.*/i', $colour))
{
echo '35';
}
elseif( preg_match( '/black.*/i', $colour))
{
echo '38';
}
elseif( preg_match( '/gold.*/i', $colour))
{
echo '41';
}
elseif( preg_match( '/silver.*/i', $colour))
{
echo '46';
}
elseif( preg_match( '/multi.*/i', $colour))
{
echo '594';
}
elseif( preg_match( '/beige.*/i', $colour))
{
echo '6887';
}
elseif( preg_match( '/nude.*/i', $colour))
{
echo '6887';
}
else
{
echo '534';
}
?>,
<?=$line['colour']?>,
<?=$con_size[$x]?>
<br>
<?
// finish checking if size is available
} }
?>
So at the moment this is simply echoing out the product_ID into the SKU column. The code would need to enter the product_id into an array and check whether it is unique. If the product_id is unique to the array, then the product_id is echoed out unaltered, and parent is echoed out to the 'Parent/Child' column and then the product_id is repeated to the 'Parent SKU' column.
However, if the array is checked and the product_id already exists in the array, then the product_id is echoed out to the 'SKU' column with a suffix i.e. _1. Then child is echoed to the 'Parent / Child' column and the original parent product_id echoed to the 'Parent SKU' column.
HOWEVER - the same SKU cannot be repeated with the same suffix i.e. 12345_1, 12345_1 - so presumably there would be to be another array for the suffixed SKUs to be checked against.
If anybody could help, it would be great.
Thanks
--- UPDATE ANSWER ---
I managed to solved this myself and thought I would share my solution for future reference.
/*
* Array to collect product_ids and check whether unique.
* If unique product_id becomes parent SKU
* If not product_id becomes child of previous parent and suffixed with _1, _2 etc...
*/
if (!in_array($line['product_id'], $SKU)) {
$SKU[] = $line['product_id'];
$parent = $line['product_id'];
$a = 0;
?>
<? echo 'Shoes'; ?>,
<?
echo $parent;
?>,
<?
echo "Parent";
?>,
<?
echo $parent;
?>,
<? }
else {
$child = $line['product_id'] . "_" . $a; ?>
<? echo 'Shoes'; ?>,
<?
echo $child;
?>,
<?
echo "Child";
?>,
<?
echo $child;
<?
// increment suffix value for child SKU
$a++;
?>