Hello. There is a table of the form: enter image description here

It consists of two tables - the structure of the table t1:

enter image description here

table structure t2:

enter image description here

Relationship: t1.id = t2.value_id

In accordance with the 'uf_sap_code' and 'uf_sap_code_parent' fields, you need to find the parent id and fill in the 'iblock_section_id' field with them. I'm trying to write recursion, but I can't:

function getCategory() { global $ibs; $query = $ibs->GetList( array('sort' => 'asc'), array('IBLOCK_ID' => 5), false, array('ID', 'NAME', 'IBLOCK_SECTION_ID', 'UF_SAP_CODE', 'UF_SAP_CODE_PARENT') ); $result = array(); while ($row = $query->Fetch()) { $result[$row["UF_SAP_CODE_PARENT"]][] = $row; } return $result; } $category_arr = getCategory(); function getParentId($parent_id) { global $category_arr; if (isset($category_arr[$parent_id])) { foreach ($category_arr[$parent_id] as $value) { $id = $value["ID"]; getParentId($value["UF_SAP_CODE"]); } return $id; } } 

Unfortunately, recursion was never encountered before. As I understand it, you need the condition of deepening, but I can not figure out. Tell me please.

  • Well, in general, in the bitrix you can add parents to the elements / sections through the admin panel. Manually climb and edit the table is not the best option. Why complicate things so much? Here it seems to me that recursion is not needed at all, and you can restrict yourself to a simple SQL query executed in this table, which will loop through all the records that do not have an empty 'uf_sap_code_parent' and put it into 'iblock_section_id'. - Nikolaj Sarry
  • The fact is that this table is an example. In fact, the nesting level is more than 4 ... This code is part of a self-written script for loading a csv file into the information block, so the bitrix tools are not suitable. According to the collective farm problem, I decided: I made five requests in a row in a cycle, but I would still like the correct code. - nicklas
  • in 'iblock_section_id', you need to add not the value of 'uf_sap_code_parent', but the corresponding 'id', for example, for id: 14,15,16,17 the value of iblock_section_id should be id 13 - nicklas

1 answer 1

If I truly understood everything, here it is really possible to do without recursion, despite the fact that the links in the table can be .. nested many times. The main point is that in the field iblock_section_id put down the ID of the parent? I think there would be one such SQL query

 UPDATE your_table t1 JOIN your_table t2 ON t1.uf_sap_code_parent = t2.uf_sap_code SET t1.iblock_section_id = t2.id 

Instead of "your_table" the name of your table. During UPDATE, we simultaneously find the parent (by the fields uf_sap_code_parent and uf_sap_code) and retrieve the ID from it, which will then be recorded in the iblock_section_id of the updated table.

  • Thank you for your assistance! Tomorrow I will try - nicklas
  • I would like to clarify: t1 and t2 are two different tables? - nicklas
  • I have an id and iblock_section_id in one table, and uf_sap_code and uf_sap_code_parent in another. - nicklas
  • UPDATE b_iblock_section JOIN b_uts_iblock_5_section ON b_iblock_section.id = b_uts_iblock_5_section.VALUE_ID SET iblock_section_id = id WHERE uf_sap_code_parent = uf_sap_code - nicklas
  • Did I write correctly above? with such a request, nothing happens - nicklas