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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.