<?php  
    $mysql_server_name='localhost'; 
    $mysql_username='root'; 
    $mysql_password='';   
    $mysql_database='hairleft';
    $conn=mysql_connect($mysql_server_name,
    $mysql_username,$mysql_password,
    $mysql_database); 
 
   
   
    $arr1 = array("aa","bbb","ccc","ddd","eeee","fff","gg","hhhh","iii","jj","kkkk","lll","mmm","nnn");
    $arr2 = array("oooo","ppp","qqqq","rrrr","sss");
    //$arr3 = array("wqwq","sasa","xzxz","vcvc","bvbv","nbnb","mnmn","hghg");
    $sql = "insert into tag(name,status,first_customer_id,first_store_id,title) values('test11',1,0,0,'test11')";
    $i=1;
//    foreach($arr3 as $ar3){
    foreach($arr1 as $ar1){
       
        foreach($arr2 as $ar2){
            $qq = "2 ".$ar1." ".$ar2;
            $i++;
            //echo $qq.$i;
            $sql .= ",('".$qq."',1,0,0,'".$qq."')";
           
       
            //tag_id  name  status  first_customer_id  first_store_id  description  titlecontent  title 


           
        }
    }
   
    mysql_select_db($mysql_database,$conn);  
    $result=mysql_query($sql);  
    //echo $sql;  
   // mysql_close($conn);  
   // echo "chenggong";
   

    $sql = "select * from tag where tag_id = (select max(tag_id) from tag)";
    $result = mysql_query($sql);
    while($tagmaxids=mysql_fetch_array($result)){
        $tagmaxid = $tagmaxids['tag_id'];
        //echo $tagmaxid;
    }
   

    $sql = "select * from tag_relation where tag_id = (select max(tag_id) from tag_relation)";
    $result = mysql_query($sql);
    while($tagmaxids=mysql_fetch_array($result)){
        $tagrelationmaxid = $tagmaxids['tag_id'];
        //echo $tagmaxid;
    }

    echo $tagmaxid."<br/>";
    echo $tagrelationmaxid."<br/>";
    if($tagrelationmaxid==""){
        $tagrelationmaxid = 1;
    }

    for($i=$tagrelationmaxid;$i<=$tagmaxid;$i++){
        if($i<$tagmaxid){
            $sql_tagid .= $i.",";
        }else{
            $sql_tagid .= $i;
        }
    }

   // echo $sql_tagid;
   
    $sql_tag  = "select tag_id,name from tag where tag_id > ".$tagrelationmaxid." and tag_id < ".$tagmaxid;
    //echo $sql_tag;
    $result = mysql_query($sql_tag);
    //$tagid_name=mysql_fetch_array($result);
    while($tagmaxids=mysql_fetch_array($result)){
        $tag_name[] = $tagmaxids;

        //echo $tagmaxid;
    }
    foreach($tag_name as $tadd){
        //echo $tadd['tag_id'].$tadd['name']."<br/>";
       
        $str = $tadd['name'];
        $res=explode(" ",$str);
        $re0 = $res[0];
        $re1 = $res[1];
        $re2 = $res[2];
       
        $sql ="SELECT * FROM catalog_product_entity_varchar where attribute_id=96 and (value like '%".$re0."%' or value  like '%".$re1."%' or value  like '%".$re2."%' )  ORDER BY CASE WHEN value  LIKE '%".$re0."%".$re1."%".$re2."%' THEN 0 WHEN value LIKE '%".$re0."%".$re2."%".$re1."%' THEN 1 WHEN value LIKE '%".$re1."%".$re0."%".$re2."%' THEN 2 WHEN value LIKE '%".$re2."%".$re0."%".$re1."%' THEN 3 WHEN value LIKE        '%".$re1."%".$re2."%".$re0."%' THEN 4 WHEN value LIKE '%".$re2."%".$re1."%".$re0."%' THEN 5  WHEN value LIKE '%".$re0."%' AND  value LIKE '%".$re1."%' THEN 6  WHEN value LIKE '%".$re0."%' AND value LIKE '%".$re2."%' THEN 7 WHEN value LIKE '%".$re1."%' AND value LIKE '%".$re2."%' THEN 8  WHEN value LIKE '%".$re0."%' THEN 9 WHEN value LIKE '%".$re1."%' THEN 10  WHEN value LIKE '%".$re2."%' THEN 11 end ";
       
        $result = mysql_query($sql);

        //echo $sql;
        $i = 1;
        $strs = "";
        while($product_coll = mysql_fetch_array($result)){
            if($i ==1){
                $strs .= "('".$tadd['tag_id']."','".$product_coll['entity_id']."',1,1)";
            }else{
                $strs .= ",('".$tadd['tag_id']."','".$product_coll['entity_id']."',1,1)";
            }
    //    echo $sql;
            $i++;
        }
       
        $strs = "insert into tag_relation(tag_id,product_id,store_id,active) values ".$strs;
         $result=mysql_query($strs);
        //echo "######";
        //echo  $strs;
    //    */
    }
echo "good";
   
   


   
    /*  

tag_id    product_id  store_id  active 

   SELECT *
FROM counterss
ORDER BY CASE
WHEN car LIKE '%zhao%qi%yong%'
THEN 0
WHEN car LIKE '%zhao%yong%qi%'
THEN 1
WHEN car LIKE '%yong%zhao%qi%'
THEN 2
WHEN car LIKE '%qi%zhao%yong%'
THEN 3
WHEN car LIKE '%yong%qi%%zhao'
THEN 4
WHEN car LIKE '%qi%yong%zhao%'
THEN 5
WHEN car LIKE '%zhao%'
AND car LIKE '%qi%'
THEN 6
WHEN car LIKE '%zhao%'
AND car LIKE '%yong%'
THEN 7
WHEN car LIKE '%yong%'
AND car LIKE '%qi%'
THEN 8
WHEN car LIKE '%yong%'
THEN 9
WHEN car LIKE '%qi%'
THEN 10
WHEN car LIKE '%zhao%'
THEN 11
END
LIMIT 0 , 30






END
   
   
    SELECT *
FROM counterss
ORDER BY CASE WHEN car LIKE '%ao qi y%'
THEN 0
WHEN car LIKE '%qi y%'
THEN 1
END
   
   
   
   
   
   
    */
   
   
   
    ?>