<?php
include_once('simple_html_dom.php');

$HOST_NAME = "localhost";
$DB_NAME = "advice";
$CHAR_SET = "charset=utf8";
$USERNAME = "root";
$PASSWORD = "";


$conn = new mysqli($HOST_NAME, $USERNAME, $PASSWORD, $DB_NAME);


$sql = "select DISTINCT `pro_category` from products" ; 
$result = $conn->query($sql);

while($row = $result->fetch_assoc()) {
	$sql = "INSERT INTO product_category (pro_type_id,category_name,category_value) VALUES (0,'". $row["pro_category"] ."','". $row["pro_category"] ."')";
    echo $sql .";";
  }

exit();

class Product
{
	public $cat;
	public $subcat;
    public $name;
    public $desc;
    public $srp;
    public $tech;
    public $p1;
    public $p2;
    public $p3;
    public $war;
    public $code;
    public $img;
}


$aa = file_get_contents("cc.html");
//echo $aa;
$b = strip_tags($aa, "<tbody><tr><th><table><td>");
$b = str_replace("&nbsp;","",$b);
$b = str_replace("<th","<td",$b);
$b = str_replace("</th","</td",$b);
$b = str_replace("'","",$b);
//echo $b;
$document = new DOMDocument;
$document->loadXML($b);
$cells = $document->getElementsByTagName("tr");

$products = array();
$subcat = "";
for ($i=1; $i < count($cells) ; $i++) { 
	$tdcell = $cells[$i]->getElementsByTagName("td");

	/*foreach ($tdcell as $key => $value) {
		echo $value->nodeValue ."|";
		if($value->nodeValue!="")
	}*/

	if(count($tdcell)>5){
		
		$id = explode("_",  $tdcell[1]->getAttribute('id'))[2];
		
		$pro = new Product();
		$pro->cat = "จักรเย็บผ้า";
		$pro->code = $id;
		$pro->img = "https://img.advice.co.th/images_nas/pic_product4/".$id."/".$id."OK_BIG_1.jpg";
		$pro->subcat = trim($subcat);
		$pro->name = trim($tdcell[1]->nodeValue);
		$pro->desc = trim($tdcell[2]->nodeValue);
		$pro->srp = trim($tdcell[3]->nodeValue);
		$pro->tech = trim($tdcell[4]->nodeValue);

		$pro->p1 = trim($tdcell[7]->nodeValue);
		$pro->p2 = trim($tdcell[8]->nodeValue);
		$pro->p3 = trim($tdcell[9]->nodeValue);
		$pro->war = trim($tdcell[10]->nodeValue);

		array_push($products, $pro);
		
	}else{
		$subcat = $tdcell[0]->nodeValue;
	}
}
//var_dump($products);

foreach ($products as $key => $value) {
	$sql = "INSERT INTO products (pro_category, pro_sub_category, pro_name, pro_desc, pro_srp, pro_technician, pro_price_1, pro_price_2, pro_price_3,pro_warranty,pro_code,pro_img)
	VALUES ('".$value->cat."','".$value->subcat."','".$value->name."','".$value->desc."','".$value->srp."','".$value->tech."','".$value->p1."','".$value->p2."','".$value->p3."','".$value->war."','".$value->code."','".$value->img."')";
	//$connect->exec($sql);
	echo $sql;
}
?>