Jump to content
  • разработка интернет магазинов на opencart
  • доработка интернет магазинов на opencart

Дорилиить запрос в БД


kuripka2222
 Share

Recommended Posts

Всем привет) 
Вопрос для знающих php и js

Как сгделать проверку по IP?

Сейчас на сайте я сгделал добавления лайка (мне нравится товар) , при клике на кнопку в базу данных добавляется значение + 1, но если кликнуть на кнопку 20 раз, то и значение в базе увелилится на 20. Как мне дорилиить всю эту конструкцию иким спосопотому чтом, чтобы при клике проверялось ip пользователя и если он ранее уже нажимал, то в иком случае чтобы из базы вылиилось 1 значение. 




В могдели 

public function updateLike($product_id) {
	$this->db->query("UPDATE " . DB_PREFIX . "product SET likes = (likes + 1) WHERE product_id = '" . (int)$product_id . "'");
}

В контроллере 

public function setLike() {
		$this->load->language('product/product');
		$this->load->model('catalog/product');

		if (isset($this->request->post['product_id'])) {
			$product_id = $this->request->post['product_id'];
		} else {
			$product_id = 0;
		}
  
		$updateLike = $this->model_catalog_product->updateLike($product_id);
 
		$likessd = $this->model_catalog_product->getProduct($product_id);
 
		$json = array();
 
		$json['likes'] = sprintf($this->language->get('text_likes'), $likessd['likes']);
 
		$this->response->addHeader('Content-Type: application/json');
		$this->response->setOutput(json_encode($json));
    }

В вигде 

<button class="btn btn-default" id="like" data-loading-text="{{ text_loading }}" onclick="addLike({{ product_id }});">{{ text_like }}
<span class="like_count">{{ likes }}</span></button>

<script>
function addLike(product_id) {
	var product_id = product_id;
	$.ajax({
		url: 'index.php?route=product/product/setLike',
		type: 'post',
		data: 'product_id=' + product_id,
		dataType: 'json',
		beforeSend: function () {
			$('#like').button('loading');
		},
		complete: function () {

		},
		success: function (json) {
			setTimeout(function () {
				$('#like').html('<span class="like_count">{{ text_like }} ' + json['likes'] + '</span>');
			}, 100);
		},
		error: function (xhr, ajaxOptions, thrownError) {
			alert(thrownError + '\r\n' + xhr.statusText + '\r\n' + xhr.responseText);
		},
	});
}
</script>

 

Link to comment
Share on other sites


Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

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.