hybmg57 Posted July 7, 2011 Share Posted July 7, 2011 Hi I need to create a leaderboard with Cakephp. There are tables already set up. They are six controllers (region) which have an add function to put data into separate tables (region). In this add function I have to check if the user's score is greater than any score in the leader board table, if so insert the new score and delete the last entry. How do I do this? If someone can help me with the code it would be greatly appreciated! Quote Link to comment https://forums.phpfreaks.com/topic/241334-cakephp-help/ Share on other sites More sharing options...
sanfly Posted July 9, 2011 Share Posted July 9, 2011 You really need to give more info about your table structure when you ask for help. All I can do is make my best guess based on the info provided I cant guarantee this will work as I dont have the table structures and form data, and also Im not able to test, but you should be able to get the right idea Step 1: Get the lowest score in the leaderboard <?php $lb = $this->Leaderboard->find('first', array('order' => array('Leaderboard.score DESC'))); $lowestscore = $lb['Leaderboard']['score']; ?> Step 2: Check if the users score is greater than the lowest score <?php if($lowestscore < $this->data['User']['score']){ // Delete current low score $this->Leaderboard->delete($lb['Leaderboard']['id']); // Insert the new score $this->data['Leaderboard']['user'] = $this->data['User']['id']; $this->data['Leaderboard']['score'] = $this->data['User']['score']; // Also add any other fields you need $this->Leaderboard->save($this->data['Leaderboard']); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/241334-cakephp-help/#findComment-1240499 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.