List by ID in Order !!

legendary_003

New Member
Hello...
I-m trying to do the following !!
Listing data in the table !!
I would like....if the list has 4 rows..whetever the ID list is..make the ID list 1 2 3 4 !!
Example.....if have a list with ID 1 3 3...make stay 1 2 3 !!

Follow the try next:

echo "<table class='tabela_dados' border='1'>
<tr>
<td>ID</td>
<td>Nome Empresa</td>
<td>Responsável</td>
<td>Telefone 1</td>
<td>Telefone 2</td>
<td>E-mail 1</td>
<td>E-mail 2</td>
<td>Endereço</td>
<td>CEP</td>
<td>Bairro</td>
<td>AÇÃO 1</td>
<td>AÇÃO 2</td>

</tr>
";

$sql = "SELECT ID FROM usuarios_dados WHERE Usuario='$usuario'";
$result = $conn->query($sql);
$num_rows = $result->num_rows;

$Novo_ID = 1;
for ($i = 0; $i < $num_rows; $i++) {
$registro = $result -> fetch_row();
$sql2 = "UPDATE usuarios_dados SET ID='$Novo_ID' WHERE ID='$Novo_ID'";
$result2 = $conn->query($sql2);
$Novo_ID++;
}

$sql = "SELECT * FROM usuarios_dados";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr>
<td>$row[ID]</td>
<td>$row[Nome_Empresa]</td>
<td>$row[Responsavel]</td>
<td>$row[Telefone_1]</td>
<td>$row[Telefone_2]</td>
<td>$row[Email_1]</td>
<td>$row[Email_2]</td>
<td>$row[Endereço]</td>
<td>$row[CEP]</td>
<td>$row[Bairro]</td>
<td>
<form method='post' action='Editar_Dados.php'>
<input type='hidden' name='usuario' value='$usuario'>
<input type='hidden' name='senha' value='$senha'>
<input type='hidden' name='ID' value='$row[ID]'>
<input type='submit' style='padding: 10px;' value='EDITAR'>
</form>
</td>
<td>
<form method='post' action='Deletar_Dados.php'>
<input type='hidden' name='usuario' value='$usuario'>
<input type='hidden' name='senha' value='$senha'>
<input type='hidden' name='ID' value='$row[ID]'>
<input type='submit' style='padding: 10px;' value='DELETAR'>
</form>
</td>
</tr>
";
}
} else {
echo "0 results";
}

$conn->close();
 

legendary_003

New Member
I got the answer:

$sql = "SELECT * FROM usuarios_dados WHERE Usuario='$usuario'";
$result = $conn->query($sql);
$num_rows = $result->num_rows;

for ($i = 1; $i <= $num_rows; $i++) {
$registro = $result -> fetch_assoc();
$sql2 = "UPDATE usuarios_dados SET ID='$i' WHERE ID='$registro[ID]' AND Usuario='$usuario'";
$result2 = $conn->query($sql2);
}

$sql = "SELECT * FROM usuarios_dados WHERE Usuario='$usuario'";
$result = $conn->query($sql);
 

Dwightsune

New Member
Instead of running an update query for each row inside a loop, you might consider retrieving all rows first, re-indexing in your application code, and then applying batch updates.

For handling scenarios where unique constraints or validation checks are critical, integrating an ID verification API could ensure data consistency and integrity before updates. This might come in handy if your IDs need to align with external systems or validation rules.
 
Last edited:
Top