Dynamic Ranking

Hi guys, I’m creating a website where you can vote on different games, and they will then get a rank according to the votes. I want my voting system to recognize wether a vote on a certain platform for the game was posted by this user before, and if so he/she should be able to update this vote. If he/she hasn’t voted yet, the person should be able to submit a new vote.

I already have the system recognizing wether a vote exists or not, but no matter what a new vote would be submitted instead of an already submitted vote updated. How can i fix this?

Here is my code in the controller:


protected function getRankingList($model)

    {

        

        $user_id=Yii::app()->user->getId(); 

		$game_id=$model->id;

        $tab_list=Platform::getPlatforms();

        $rankings = array();

        foreach($tab_list as $key=>$value)

        {

            if(Ranking::model()->find("create_user_id=$user_id and game_id=$game_id and platform_id=$key"))

            {

            	$rank=Ranking::model()->find("create_user_id=$user_id and game_id=$game_id and platform_id=$key");

            } 

            else

            {

                 $rank = new Ranking;

            }

            $rankings[$key]= $rank;

        }

        

        if(isset($_POST['Ranking']))

		{

			$rank->game_id=$model->id;

			$rank->attributes=$_POST['Ranking'];

			

			$valid = $rank->validate();

			if ($valid)

			{

			    $rank->save(false);

			    $this->redirect(array('index'));

			}

		}

        return $rankings;

    }

Please let me know if something is not explained well enough - I’m not a native english speaker :)

I found the code rather confusing; I think you should separate the logic as follows:

  • Validation rule in your Ranking model to prevent users from voting on the same game twice.

  • Extract the ‘update’ logic into its own controller method.

  • Note that $rank in the update logic refers to the LAST rank retrieved from your Foreach loop, which might cause problems and is confusing/hard to follow.

I realise now that the whole problem i’m having is because of the last of your points:

What can i do to change this, so it refers to the rank related to the form?

anyone?

— EDIT —

I’ve discovered a way to do this now:


protected function getRankingList($model)

    {

        

        $user_id=Yii::app()->user->getId(); 

		$game_id=$model->id;

        $tab_list=Platform::getPlatforms();

        $rankings = array();

        foreach($tab_list as $key=>$value)

        {

            if(Ranking::model()->find("create_user_id=$user_id and game_id=$game_id and platform_id=$key"))

            {

            	$rank=Ranking::model()->find("create_user_id=$user_id and game_id=$game_id and platform_id=$key");

            } 

            else

            {

                 $rank = new Ranking;

            }

            $rankings[$key]= $rank;

        }

        

        if(isset($_POST['Ranking']))

		{

			$platform = $_POST['Ranking']['platform_id'];

			$rankings[$platform]->game_id=$model->id;

			$rankings[$platform]->attributes=$_POST['Ranking'];

			//$scenario = $model->getScenario();


			if ($rankings[$platform]->save())

			{

			    //echo $scenario;

			    $this->redirect(array('view','id'=>$model->id));

			    //echo $platform;

			}

		}

        return $rankings;

    }