// Get original by whatever method
$original = $this->Game->findById($id);
// Extract vars here for clarity
$x = $original['Game']['X'];
$y = $original['Game']['Y'];
$z = $original['Game']['Z'];
// Perform search
$results = $this->Game->find('all', array(
  'fields' => array(
    '*', // Specify normal fields here
    "(ABS(Game.X-$x)+ABS(Game.Y-$y)+ABS(Game.Z-$z)) AS difference'
  ),
  'order' => 'difference ASC',
  'limit' => 10
));

This will retrieve the 10 closest games, using the L_0 (manhattan)
metric like you were above.

hth
grigri
On Aug 28, 8:16 pm, Un <[EMAIL PROTECTED]> wrote:
> Each game in my games database has fields that represent dimensions.
> They are simply numbers ranging from 0-7, basically "how X" or "how
> Action-style" the game is. We'll call these fields X, Y, and Z.
>
> I wish to create a search tool that finds other games that are most
> similar to the inputted game (using the X, Y, and Z fields' numerical
> values).
>
> Below is my current solution. For each game, I proceed to compare each
> numerical field. I will call our original game $original. Just take a
> looksy:
>
> function specialsearch($original) {
>    $games = $this->Game->find('all', array('fields'=>array('X', 'Y',
> 'Z')));
>    foreach ($games as $game) {
>       $results[ $game['Game']['id'] ]['X'] = abs( $game['Game']['X'] -
> $original['Game']['X'] );
>       //We'd repeat this for each field, Y, Z and so on...
>
>       $results[ $game['Game']['id'] ]['strength'] =
> $results[ $game['Game']['id'] ]['X'] + $results[ $game['Game']['id'] ]
> ['Y'] + $results[ $game['Game']['id'] ]['Z'];
>    }
>
>    //This is where we'd put all the logic for saving or returning the
> results.
>
> }
>
> This should work well. We wind up with a distance from your original
> in each dimension. So when we add them together, we find exactly how
> dissimilar it is. 0 would be a game that's identical to this one, and
> infinity would be the opposite of this game.
>
> But this seems inefficient, especially if I want to display, for
> example, 5 other games that are similar to this one on every "view"
> action! Does anybody have a better solution?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to