How to recursively get all the analogues from the table in the article?

I created a simple analog table:

+----+-------+-------+
| id | sku_1 | sku_2 |
+----+-------+-------+
|  1 | a1    | abcd  |
|  2 | a2    | a3    |
|  3 | a3    | a1    |
+----+-------+-------+
3 rows in set (0.00 sec)

What does it mean? This means that a product with an article abcdhas an analogue with an article a1, otherwise, for example, a product with an article a3has an analogue with an article a1.

How to recursively get all products from this table in one article?

My decisions are wrong:

// Small Class to get analogs of products
class Analogs {

    public function get_analogs($sku)
    {
        if (!$sku) return false;

        $link = mysql_connect('localhost','','');
        mysql_select_db('test');

        $sku = mysql_real_escape_string($sku,$link);

        $query = mysql_query("SELECT * FROM analogs WHERE sku_1='".$sku."' OR sku_2='".$sku."'");

        while($analogs[]=mysql_fetch_assoc($query))
        continue;

        return $analogs;    
    }


    public function MixedAnalogs($sku)
    {
        if (!$sku) return false;

        $link = mysql_connect('localhost','','');
        mysql_select_db('test');

        $sku = mysql_real_escape_string($sku,$link);

        $query = mysql_query("select sku_1 sku from analogs where sku_2 = '$sku' UNION
                              select sku_2 sku from analogs where sku_1 = '$sku'");

        while($analogs[]=mysql_fetch_assoc($query))
        continue;

        return $analogs;
    } 


}

$mixed_analogs = AnalogsMix('abcd',$ids=array());

echo "<pre>";
print_r($mixed_analogs);
echo "</pre>";

// Recursive function to get analogs of analog
function AnalogsMix($sku,$ids=array())
{
    $class_analogs = new Analogs();
    $analogs = $class_analogs->get_analogs($sku);

    foreach ($analogs as $analog)
    {
        $cross = null;

        if ($analog['sku_1']==$sku)
        {
            $cross->sku = $analog['sku_2'];
        }
        else
        {
            $cross->sku = $analog['sku_1'];
        }

        $cross->id = $analog['id'];

        if (!in_array($analog['id'],$ids))
        {
            $ids[] = $analog['id'];
            $mixed[] = AnalogsMix($cross->sku,$ids);
        }
    }

    if (isset($mixed))
    {
        return $mixed;
    }
    else
    {
        return false;
    }
}
+5
source share
3 answers

SQL UNION

select sku_1 sku from analogs where sku_2 = $yourid
union
select sku_2 sku from analogs where sku_1 = $yourid

Then you will get in the results only identifiers of analogues.

+1
source

Here, I suppose, you have all your pairs in an array. For example, for your example, you call analogsOf(array(array("a1", "abcd"), array("a2", "a3"), array("a3", "a1")), "abcd").

, , , , , , . , , .

function analogsOf(array $pairs, $key) {
    $res = array($key); // The result, with only the given key
    $i = 0;             // Index of the current item
    $changed = false;   // Have we added an item to $res during that iteration ?

    while ($i < count($pairs)) {
        $current = $pairs[$i];

        foreach ($res as $item) {
            if (($current[0] === $item) && (!in_array($current[1], $res)) {
                $res[] = $current[1];
                $i = 0;  // Reiterate as $res changed
            }
            else if (($current[1] === $item) && (!in_array($current[0], $res)) {
                $res[] = $current[0];
                $i = 0; // Reiterate as $res changed
            }
            else {
                $i++;  // Nothing found here, go to next item
            }
        }
    }

    return $res;
}

, , , . , , , , , , , , .

+1

, , . abcd->a1,a1->a3,a3->a2,a2->abcd. php . , .

parent- > child . , .

let abcd , a1 , - abcd->a1. a1 , a1->abcd, . , ID , id!= ID ( )

, , . . , , .

<?php 

mysql_connect('localhost','','');
mysql_select_db('test');

function getSku($sku, $id, $rel = '') {
    $query = mysql_query("SELECT * FROM analogs WHERE sku_1 = '$sku' AND id != '$id'" );
    if (mysql_num_rows($query)) {
        $row = mysql_fetch_assoc($query);
        $sku = $row['sku_2']; //PARENT SKU
        $id = $row['id']; //LAST ID
        $rel .= $row['sku_1']. '-->' . $row['sku_2']. "<br>";

    } else {
        $query = mysql_query("SELECT * FROM analogs WHERE sku_2 = '$sku' AND id != '$id'" );
        if (mysql_num_rows($query)) {
            $row = mysql_fetch_assoc($query);
            $sku = $row['sku_1']; //PARENT SKU
            $id = $row['id']; //LAST ID
            $rel .=$row['sku_2']. '-->' . $row['sku_1']. '<br>';
        } else {

            return (string)$rel; //NOTHING FOUND
        }
    }
    return getSku($sku,$id,$rel);    

}

echo $new = getSku('abcd','-1');
+1

All Articles