Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0301· OpenAIAi2 WildChat-4.8M (AI2)

if ($datos["idtipopregunta"] == 6) { $opciones = pg_Exec("SELECT * FROM…

  1. User

    if (datos["id_tipo_pregunta"] == 6) { opciones = pg_Exec("SELECT * FROM {_SESSION['ESQUEMA_BD']}.opciones WHERE id_pregunta={datos['id_pregunta']} ORDER BY orden_opcion");

    echo ""; echo "";

    optionIndex=1;optionIndex = 1; order = array(); // Initialize the $order array

    while (info=pgfetcharray(info = pg_fetch_array(opciones)) { optionId=optionId = info['id_opcion']; // Option ID positionId = "position_optionId"; // Generate a position ID for each option based on its ID

    echo "<tr class='option-container' data-value='{$info['valor_opcion']}' draggable='true' ondragstart='dragStart(event)' ondragover='dragOver(event)' ondrop='drop(event)' data-index='$optionIndex'>";
    echo "<td class='option-content'>";
    echo "<span class='drag-icon'><i class='fas fa-arrows-alt'></i></span>";
    echo "<div class='option'>{$info['nombre_opcion']}</div>";
    echo "</td>";
    echo "<td class='position' id='$positionId' data-position='$optionIndex'>$optionIndex</td>"; // Display the current position
    echo "</tr>";
    
    $order[$optionIndex] = array(
      'id_opcion' => $optionId, // Option ID
      'positionId' => $positionId, // Position ID
      'position' => $optionIndex // Initial position
    );
    $optionIndex++;
    

    }

    echo ""; echo "";

    // Fetch the existing response respuesta = pg_fetch_array(pg_Exec("SELECT valor_respuesta FROM {_SESSION['ESQUEMA_BD']}.respuestas_empleados WHERE id_encuesta=idencuestaANDcedula=′id_encuesta AND cedula='cedula' AND id_pregunta='{$datos['id_pregunta']}'"));

    // Perform the calculation to determine the position of the dragged and dropped options draggedOption=isset(draggedOption = isset(_POST['draggedOption']) ? POST[′draggedOption′]:′′;_POST['draggedOption'] : ''; droppedOption = isset(POST[′droppedOption′])?_POST['droppedOption']) ? _POST['droppedOption'] : '';

    if (draggedOption && droppedOption) { draggedPosition=′′;draggedPosition = ''; droppedPosition = '';

    foreach ($order as $position => $option) {
      if ($option['id_opcion'] == $draggedOption) {
        $draggedPosition = $position;
      } elseif ($option['id_opcion'] == $droppedOption) {
        $droppedPosition = $position;
      }
    }
    
    // Swap the positions of the dragged and dropped options in the $order array
    $temp = $order[$draggedPosition];
    $order[$draggedPosition] = $order[$droppedPosition];
    $order[$droppedPosition] = $temp;
    
    // Update the position values in the HTML table and data-position attribute
    foreach ($order as $position => $option) {
      $positionId = $option['positionId'];
      echo "<script>document.getElementById('$positionId').textContent = $position;</script>";
      echo "<script>document.getElementById('$positionId').setAttribute('data-position', $position);</script>";
    }
    
    // Update the valor_respuesta with the updated order
    $orderString = implode(',', array_column($order, 'id_opcion'));
    $respuesta['valor_respuesta'] = $orderString;
    pg_query("UPDATE {$_SESSION['ESQUEMA_BD']}.respuestas_empleados SET valor_respuesta='{$respuesta['valor_respuesta']}' 
             WHERE id_encuesta=$id_encuesta AND cedula='$cedula' AND id_pregunta='{$datos['id_pregunta']}'");
    

    }

    // Display the valor_respuesta echo "valor_respuesta: {$respuesta['valor_respuesta']}"; } i need to all the rows of options have a position id and if the position of the option change the position id changes too

  2. Assistant

    The code you provided is already creating a position ID for each option based on its ID:

    $positionId = "position_$optionId"; // Generate a position ID for each option based on its ID
    

    To update the position ID when the position of an option changes, you can use the following code inside the loop where you are creating the options:

    $positionId = $option['positionId']; // Get the current position ID from the $order array
    echo "<tr class='option-container' data-value='{$info['valor_opcion']}' draggable='true' ondragstart='dragStart(event)' ondragover='dragOver(event)' ondrop='drop(event)' data-index='$optionIndex'>";
    echo "<td class='option-content'>";
    echo "<span class='drag-icon'><i class='fas fa-arrows-alt'></i></span>";
    echo "<div class='option'>{$info['nombre_opcion']}</div>";
    echo "</td>";
    echo "<td class='position' id='$positionId' data-position='$optionIndex'>$optionIndex</td>"; // Display the current position
    echo "</tr>";
    

    This code gets the current position ID from the $order array and uses it to set the ID of the td element where the position is displayed. The data-position attribute is also updated with the current position.

    When the positions of the options change, you update the $order array with the new positions and then update the HTML table and the valor_respuesta field with the new order. To update the position IDs, you need to loop through the $order array again and update the id and data-position attributes of each td element:

    // Update the position values in the HTML table and data-position attribute
    foreach ($order as $position => $option) {
      $positionId = $option['positionId'];
      echo "<script>document.getElementById('$positionId').textContent = $position;</script>";
      echo "<script>document.getElementById('$positionId').setAttribute('data-position', $position);</script>";
    }
    

    This code loops through the $order array and uses the position ID to update the textContent and data-position attributes of the corresponding td element in the HTML table.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.