Jump to content

cakephp help!


hybmg57

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/241334-cakephp-help/
Share on other sites

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']);
} ?>

Link to comment
https://forums.phpfreaks.com/topic/241334-cakephp-help/#findComment-1240499
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.