• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Update Row Value If Another Row is Deleted

#1
Hello.

The app I am building has a ranking system. Nothing fancy, just 1st, 2nd, 3rd. Is there any possible way using CodeIgniter, I could update values in the other rows based on the row that is deleted?

So, If for example,  I delete 1st place from the table, 2nd would become 1st and 3rd would become 2nd.

I have started trying to build a formula in PHP using the rows Rank Value and the number of rows in the table, but it is proving quite challenging and I was just wondering if there is a simpler way using a CodeIgniter feature.  I did try a Google search but I could not see any results that mention anything.

Thank you for any help. Smile
Reply

#2
After you delete a row, you can update the other rows where the rank is higher. You just need to know what rang was deleted. Something like this:

Code:
UPDATE SOMETABLE SET RANK = RANK + 1 WHERE RANK > ?

You can do this in your code, or with a database trigger, or if you use CI4, with a model event (before delete or after delete): https://codeigniter4.github.io/userguide...parameters
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
Reply

#3
(02-01-2020, 03:00 PM)includebeer Wrote: After you delete a row, you can update the other rows where the rank is higher. You just need to know what rang was deleted. Something like this:

Code:
UPDATE SOMETABLE SET RANK = RANK + 1 WHERE RANK > ?

You can do this in your code, or with a database trigger, or if you use CI4, with a model event (before delete or after delete): https://codeigniter4.github.io/userguide...parameters


This works perfectly!, Thank you so much for the help!
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.