I have a table with these values
Juan 5
Pedro 7
Luis 6
I want to replace the consecutive numbers that are listed from least to largest starting from number 1 ie a php script that detects that the table does not have the number 1 then assign to joan that is the lowest value 5 the number 1 ,
I ask again, there is number 2, then I assign the next child that would be Luis to change the 6 for a 2
I ask again, there is number 3 if it is not then I assign the number to Pedro change the number 7 to the 3
The final result would be as follows
Juan 1
Pedro 3
Luis 2
The code is as follows:
foreach($idEventsToChangePosition as $ids) {
$position = $wpdb->get_var($wpdb->prepare("SELECT position FROM $db_table_name WHERE idSiteEvent = %d AND idBlock = %d", $ids, $idBlock));
$wpdb->update(
$db_table_name,
array('position' => $position - 1),
array('idBlock' => $idBlock, 'idSiteEvent' => $ids)
);
}
It only works if the tables have position from 1 but if one does not have the position 1 it is not possible
The table contains two fields:
idSiteEvent position
Juan 5
Pedro 7
Luis 6
Where should I assign to these idSiteEvent numbers from least to greatest but starting at 1, that is, the script for mysql in php must detect who is the smallest number and change it by 1 then the next one and so on until achieving
idSiteEvent position
Juan 1
Luis 2
Pedro 3