N
N
nanny_ogg2017-09-09 09:42:04
1C-Bitrix
nanny_ogg, 2017-09-09 09:42:04

How to implement voting for a separate element using infoblocks in Bitrix?

Good afternoon. There are infoblock elements that are displayed on the page as a list. It is necessary for everyone to make a button to vote, and somehow take into account votes with binding to ip and time interval. I tried to do it manually, created my own tables in the database, created a separate voting.php file in the component template, in Ajax, when you click on the vote button, this file goes to the handler, it connects, works, everything is fine. Database connection problem. I tried to connect global $DB - it doesn't work, var_dump($DB) returns NULL, requests are not sent. I tried mysql_query, same problem - "mysql_query(): Access denied for user ''@'localhost'".
How can this problem be solved? Any option, by standard means using infoblocks, or manually, using your own tables in the database. If with infoblocks, then tell me, at least in which direction to dig, I'm not very friendly with Bitrix.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrey Nikolaev, 2017-09-09
@nanny_ogg

The regular mechanism of voting for an element of infoblocks does not imply the recording of time and ip, only the fact of voting. If you want to make such a vote, you need to either create polls or use your own table.
Let's take a look at your situation:
Most likely you just created a file. And in it, respectively, the service part was NOT connected.
I would recommend that you use the Bitrix ORM for this situation (very ... But as a
quick solution, you can also directly query the database (although this will be a less elegant solution).
In this case, the code will look something like this:

<?
require $_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/include/prolog_before.php";

/**
 * Тут можете проверить, что все переменные пришли
 * Я для себя буду использовать переменные:
 * - ELEMENT_ID 
 * - USER_ID
 * 
 * Просто писать факт голосования.
 * В таблицу project_element_vote ( ID, ELEMENT_ID, USER_ID )
 */

global $USER;

/* @var object Объект приложения */
$oApplication = \Bitrix\Main\Application::getInstance();

/* @var object Объект соединения с БД */
$oConnection = $oApplication->getConnection();

/* @var object Хелпер для безопасной записи в БД*/
$oHelper = $oConnection->getSqlHelper();

/* @var object Объект для работы с входящими переменными */
$oRequest = $oApplication->getContext()->getRequest();


if ( $oRequest->isAjaxRequest() && !empty( $oRequest->get('ELEMENT_ID') ) )
{
  $iUser    = $USER->GetId();
  $iElement = $oHelper->forSql( $oRequest->get('ELEMENT_ID') );

  $sql = "INSERT INTO project_element_vote('ELEMENT_ID','USER_ID') VALUES ('{$iElement}','{$iUser}')";
  $res = $oConnection->query($sql);

  if ( $res )
  {
    echo 'ok';
    die();
  }
}
echo 'bad';

N
nanny_ogg, 2017-09-09
@nanny_ogg

yes, thank you very much) I needed this - require $_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/include/prolog_before.php";

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question