<?php
error_reporting(E_ALL);
ini_set('max_execution_time', '0'); 
ini_set('memory_limit','2048M');
	if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }
  
  	include("configi.php");


  $theValue = function_exists("mysqli_real_escape_string") ? mysqli_real_escape_string($conn, $theValue) : mysqli_escape_string($conn, $theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}


//copy image2wbmp


$phxcrmclient_id = 142;

include_once("configi.php");



$load = 1;

if ($load == 1){
	
	echo "started load";

	//load 
	
	//truncate 142_prod_search
	
	$sqltps = "truncate 142_prod_search";

	$resulttps=mysqli_query($conn, $sqltps);
		
		

	$queryiindx1="select * from ".$phxcrmclient_id."_prodserv where uid <> 17";
				
				
	$resultiindx1=mysqli_query($conn, $queryiindx1);
	
	$prod_tags_string = "";
				
	while($rowiindx1=mysqli_fetch_array($resultiindx1)) { 
	
	echo "uid".$rowiindx1['uid'];

		$uid =  $rowiindx1['uid'];
		$prod_title =   $rowiindx1['prod_title'];
		$prod_title_raw =   $rowiindx1['prod_title'];
		$prod_barcode =  $rowiindx1['prod_barcode'];
		$prod_code =  $rowiindx1['prod_code'];
		$prod_retail =  $rowiindx1['prod_retail'];
		$prod_status =  $rowiindx1['prod_status'];
		$Option1_Value = $rowiindx1['Option1_Value'];
		
		$prod_quickpick_sel = $rowiindx1['prod_quickpick_sel'];
		$prod_quickpick_order = $rowiindx1['prod_quickpick_order'];
		
		$prod_title = $prod_title . " " . $Option1_Value;
		$prod_title =   GetSQLValueString($prod_title, "text");
		$prod_barcode =   GetSQLValueString($rowiindx1['prod_barcode'], "text");
		$prod_isbn =   "''";
		$prod_image_url1 =   GetSQLValueString($rowiindx1['prod_image_url1'], "text");
		
		$prod_status =   GetSQLValueString($prod_status, "text");

		
		//insert into 142_prod_search
		
		$sqlinsertplu = "INSERT INTO ".$phxcrmclient_id."_prod_search (sku, sel_uid, main_uid, v_uid, header,  title, barcode, price, quickpick_sel, quickpick_order,  prod_image_url1, prod_status) values (".$prod_code.",".$uid.",".$uid.",0,1,".$prod_title.",".$prod_barcode.",".$prod_retail.",".$prod_quickpick_sel.",".$prod_quickpick_order.",".$prod_image_url1.",".$prod_status.")";
		
		echo "123<br>";
		echo $sqlinsertplu;

		$resultinsertplu=mysqli_query($conn, $sqlinsertplu);
		
		//lookup variants - insert into 142_prod_search
		

			$queryiindx2="select * from ".$phxcrmclient_id."_prodserv_var where v_main_uid = ".$uid;
		
			$resultiindx2=mysqli_query($conn, $queryiindx2);
				
			while($rowiindx2=mysqli_fetch_array($resultiindx2)) { 

				$v_uid	 =   $rowiindx2['v_uid'];
				$v_name =   $rowiindx2['v_name'];
				$v_sku	 =  $rowiindx2['v_sku'];
				$v_price =  $rowiindx2['v_price'];
				$v_barcode =  $rowiindx2['v_barcode'];
				$v_quickpick_sel =  $rowiindx2['v_quickpick_sel'];
				$v_quickpick_order =  $rowiindx2['v_quickpick_order'];
				
				$prod_title2 = $prod_title_raw . " " . $v_name;
		
				$prod_title2 =   GetSQLValueString($prod_title2, "text");
				$prod_barcode =   "";
				$prod_barcode =   GetSQLValueString($v_barcode, "text");
				$v_sku =   GetSQLValueString($v_sku, "text");
				$v_image_url1 =   GetSQLValueString($v_image_url1, "text");
		
				//insert into 142_prod_search
		
				$sqlinsertplu2 = "INSERT INTO ".$phxcrmclient_id."_prod_search ( sel_uid, main_uid, v_uid, header,  title, barcode, price, quickpick_sel, quickpick_order, sku) values (".$v_uid.",".$uid.",".$v_uid.",0,".$prod_title2.",".$prod_barcode.",".$v_price.",".$v_quickpick_sel.",".$v_quickpick_order.",".$v_sku.",".$v_image_url1.")";


			//	echo $sqlinsertplu2;
				
				
				$resultinsertplu2=mysqli_query($conn, $sqlinsertplu2);
			}
		
	}

echo "done product table";

}

