将唯一值存储到数组中并与循环进行比较 - PHP

时间:2014-05-28 08:39:10

标签: php mysql arrays csv

我正在撰写一份PHP报告,该报告旨在使用commma分隔符纯粹作为CSV文件导出。

有三列与product_id相关,这三列如下:

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

这是指完整CSV文件的链接:

http://i.imgur.com/XELufRd.png

目前代码如下:

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

            ?>

所以目前这只是将product_ID回显到SKU列。代码需要将product_id输入到数组中并检查它是否是唯一的。如果product_id对于数组是唯一的,则product_id将不加改变地回显,并且父项将回显给父母/子女&#39;列然后将product_id重复到父SKU&#39;柱。

但是,如果检查了数组并且数组中已存在product_id,则product_id将回显到&#39; SKU&#39;带后缀的列,即_1。那么孩子就会回应父母与孩子的关系。列和原始父product_id回显给父母SKU&#39;柱。

但是 - 同一个SKU不能用相同的后缀重复,即12345_1,12345_1 - 所以可能会有另一个阵列用于检查后缀SKU。

如果有人可以提供帮助,那就太好了。 感谢

---更新 ANSWER ---

我自己设法解决了这个问题,并认为我会分享我的解决方案以供将来参考。

/*
     * 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++;

    ?>

0 个答案:

没有答案