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();
 
It seems like you want to renumber the IDs in your database table so that they are consecutive starting from 1.

<?php
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;

// Renumbering the IDs
$Novo_ID = 1;
while ($row = $result->fetch_assoc()) {
$old_ID = $row['ID'];
$sql_update = "UPDATE usuarios_dados SET ID='$Novo_ID' WHERE ID='$old_ID'";
$conn->query($sql_update);
$Novo_ID++;
}

// Fetching the updated data
$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();
?>

In this code, I've modified the loop that fetches the IDs from the database. Instead of incrementing $Novo_ID in a separate loop, I've combined it with the loop that fetches the IDs. This way, we directly update the IDs in the database to be consecutive starting from 1.


Best Regard
Danish Hafeez | QA Assistant
ICTInnovations
 
I resolved the problem:
The correct code is:

$sql = "SELECT * FROM usuarios_dados";
$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]'";
$result2 = $conn->query($sql2);
}
 
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);
 
Back
Top