if ($datos["idtipopregunta"] == 6) { $opciones = pg_Exec("SELECT * FROM…
- 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 "";
order = array(); // Initialize the $order array
while (opciones)) { 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=cedula' AND id_pregunta='{$datos['id_pregunta']}'"));
// Perform the calculation to determine the position of the dragged and dropped options _POST['draggedOption']) ? droppedOption = isset(_POST['droppedOption'] : '';
if (draggedOption && droppedOption) { 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
- 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 IDTo 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
$orderarray and uses it to set the ID of thetdelement where the position is displayed. Thedata-positionattribute is also updated with the current position.When the positions of the options change, you update the
$orderarray with the new positions and then update the HTML table and thevalor_respuestafield with the new order. To update the position IDs, you need to loop through the$orderarray again and update theidanddata-positionattributes of eachtdelement:// 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
$orderarray and uses the position ID to update thetextContentanddata-positionattributes of the correspondingtdelement in the HTML table.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.