<?php
require_once 'config.php';
require_once 'database.php';
class productClear extends database
{
   public function index($page)
    {
    	$start = ($page - 1) * 5000;
		//return $this->fetchAll('tb_product', ['cl_image_location' => 'Server', ''], 'cl_created_at', 'DESC');
		 $sql = ' SELECT * FROM tb_product WHERE cl_image_location="Server" AND cl_image is NOT NULL LIMIT ' . $start . ',5000 ';
		return $this->callQuery($sql);
	}

	public function updateNullOnImage($id)
	{
		return $this->update('tb_product', ['cl_image' => null], ['cl_id' => $id]);
	}
}

$fp = fopen('product_clear_text.txt', 'w');//opens file in append mode  
fwrite($fp, '');  
fclose($fp);  

$page = 1;
if(isset($_GET['page']))
{
	$page = $_GET['page'];
}

$obj = new productClear();
$products = $obj->index($page);


$currentCount  = 1;
$fp = fopen('product_clear_text.txt', 'a');//opens file in append mode  
fwrite($fp, 'Starting from ' . $page * 5000 . '<br/>');  
fclose($fp);

foreach($products AS $product)
{
	if (!file_exists('../uploads/products/' . $product['cl_image'])) {
		$obj->updateNullOnImage($product['cl_id']);
		$fp = fopen('product_clear_text.txt', 'a');//opens file in append mode  
		fwrite($fp, 'COunt:' . $currentCount . '. ID: ' . $product['cl_id'] . '<br/>');  
		fclose($fp);
		$currentCount++;
	}
}