0

I have the following function:

function find_combinations($ids, $tar_bultos, $pdo, $table_name_temp, $table_name_temp_bultos, $current_combination = [], $current_sum = 0)
{
    if ($current_sum == $tar_bultos) {
        // Crear una fila en la tabla $table_name_temp_bultos con 0 en todas las columnas
        $pdo->exec("INSERT INTO {$table_name_temp_bultos} (" . implode(',', $ids) . ") VALUES (" . implode(',', array_fill(0, count($ids), '0')) . ")");
        
        // Obtener el ID de la fila recién creada
        $row_id = $pdo->lastInsertId();

        // Actualizar la fila recién creada con los valores 1 en las columnas correspondientes
        foreach ($current_combination as $comb_id) {
            if (in_array($comb_id, $ids)) {
                $pdo->exec("UPDATE {$table_name_temp_bultos} SET {$comb_id} = 1 WHERE id = {$row_id}");
            }
        }
        return;
    }

    if (!$ids || $current_sum > $tar_bultos) {
        return;
    }

    $id = array_shift($ids);
    $bultos = $pdo->prepare("SELECT BULTOS FROM {$table_name_temp} WHERE ID = :id");
    $bultos->bindParam(':id', $id);
    $bultos->execute();
    $bultos_value = $bultos->fetchColumn();

    // Prueba sin incluir el ID actual en la combinación
    find_combinations($ids, $tar_bultos, $pdo, $table_name_temp, $table_name_temp_bultos, $current_combination, $current_sum);

 
    find_combinations($ids, $tar_bultos, $pdo, $table_name_temp, $table_name_temp_bultos, $current_combination, $current_sum);
}

The problem is that it generates the rows, but it creates them with a value of 0 (the default value of the rows when they are empty), that is, it is not indicating which columns are part of the combination it finds. Start generating rows and all to 0. There can't be an all 0 row any more than there can be an all 1 row...

To organize a bit what it does:

  1. I have two tables $table_name_temp and $table_name_temp_bultos
  2. in the table $table_name_bultos there are several columns, but I am going to name you 2 that are the ones that I will need (ID and BULTOS)
  3. the table $table_name_temp_bultos is generated in a function above, converting the id values of $table_name_temp to the column names (ids are variables, so it should always be a variable object)
  4. what I need it to do is: Search in the 'BULKS' (INT) column of $table_name_temp the combination that results in the value of another variable defined $tar_bultos
  5. when it finds a combination that gives that result, it inserts a blank row in $table_name_temp_bultos and adds a 1 to the columns whose names match the IDs that are part of the combination found.
  6. this should be repeated in a loop to find all the possible combinations and fill each row with the data of those combinations
Kendo
  • 1

0 Answers0