Jump to content
Sign in to follow this  
stas23ik

Парсинг товаров. Проблемы с парсингом категорий(не получается реализовать иерархию).

Recommended Posts

С горем пополам реализовал парсинг товаров из xls. По товарам проблем нет. Проблема осталась такая - неправильно парсятся категории. А именно проблема в построении иерархии. Наверняка что-то упустил в БД. В excel категории и подкатегории реализованы как вложенные раскрывающиеся списки.

class Category {
	
	public $id     = 0;
	public $name   = '';
	public $parent = 0;
	public $level  = 0;

	function __construct($params) {

		if (isset($params['data']) && isset($params['row']) && isset($params['parent']) && isset($params['pdo'])) {
			$unformatted_name = $params['data']->getCellByColumnAndRow(2, $params['row'])->getValue();
		
			$this->name       = $this->format_name($unformatted_name);
			$this->level  	  = $this->detect_level($unformatted_name);
			$this->parent     = ($this->level === 0) ? 0 : $params['parent'];
			$this->load($params['pdo'], $params['parent']);
		}
		
	}

	private function format_name($unformatted_name) {
		$lower = mb_strtolower( trim($unformatted_name), 'UTF-8' );
		
		return mb_strtoupper(mb_substr($lower, 0, 1, 'UTF-8'), 'UTF-8') . mb_substr(mb_convert_case($lower, MB_CASE_LOWER, 'UTF-8'), 1, mb_strlen($lower), 'UTF-8');
	}

	private function detect_level($unformatted_name) {
		$spaces = preg_match_all("/^(\s+).*/", $unformatted_name, $matches);

		return strlen( $matches[1][0] ) / 4 - 1;
	}

	private function load($pdo, $parent) {
		$sth = $pdo->prepare("SELECT * FROM ".DB_PREFIX."category_description WHERE name='".$this->name."'");
		$sth->execute();
		$result = $sth->fetch();

		if ( $result !== false ) 
			$this->id = $result['category_id'];
		else {
			$sth = $pdo->prepare("INSERT INTO ".DB_PREFIX."category (`parent_id`, `column`, `status`) VALUES (:parent_id, 1, 1)");
			$sth->bindParam(':parent_id', $this->parent, PDO::PARAM_INT);
			$sth->execute();
			$this->id = $pdo->lastInsertId();

			$sth = $pdo->prepare("INSERT INTO ".DB_PREFIX."category_description (`category_id`, `language_id`, `name`) VALUES (:category_id, 1, :name)");
			$sth->bindParam(':category_id', $this->id, PDO::PARAM_INT);			
			$sth->bindParam(':name', $this->name, PDO::PARAM_STR);
			$sth->execute();

			$sth = $pdo->prepare("INSERT INTO ".DB_PREFIX."category_to_store (`category_id`, `store_id`) VALUES (:category_id, 0)");
			$sth->bindParam(':category_id', $this->id, PDO::PARAM_INT);
			$sth->execute();
		}

	}

	public static function isCategory($data, $row) {
		return ( trim($data->getCellByColumnAndRow(1, $row)->getValue()) == "" );
	}

}

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
You are posting as a guest. If you have an account, please sign in.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×

Important Information

On our site, cookies are used and personal data is processed to improve the user interface. To find out what and what personal data we are processing, please go to the link. If you click "I agree," it means that you understand and accept all the conditions specified in this Privacy Notice.