Jump to content

? How to compare more than one result.


Sideclef

Recommended Posts

I have this script that I have been writing. To finish it I have to figure out how i can compare say up to 8 fields in a table. What I need to do is see which field has the highest result or if there is a tie on the result between more than one field. I then will need to send an event saying what the results are in order and distinguish if it was 1 highest return or more than one.  I would think I could use ORDER By and use the max. Then how would I go about distinguishing the results in php.

example of the table fields:

 

id    user    num

1  user1      1

2  user2      5

3  user3      2

4  user4      5

5  user5      6

6  user6      3

7  user7      4

8  user8      4

 

Would I use some kind of if statement or an array of some kind. I have really never dealt with anything like this I am still new to php. Hope this made sense.

Link to comment
Share on other sites

It's psuedo, and I haven't tested the logic but it gives you an idea.  Hope this helps...

 

$tie = 0;
$max = 0;
$temp = 0;

Select * from table order by user;  
Loop through the results{
$temp = $data['num'];
if ($temp>$max)
{
     $max = $temp;
}
elseif($temp == $max)
{
     $tie = $temp;
}
elseif($temp{
     $max = $max
}
}

if($tie == $max)
{
     //there is a tie
}

Link to comment
Share on other sites

Simple idea

 

<?php
$q = "Select GROUP_CONCAT(DISTINCT user) as win FROM `table` GROUP BY num ORDER BY num LIMIT 1";
$r = mysql_query($q) or die(mysql_error()."<br /><br />".$q);
$row = mysql_fetch_assoc($r);
$winners = explode(",",$row['win']);
print_r($winners);
?>

 

 

NOTE do not use such a lengthy query listed above its going to overwork your system

Link to comment
Share on other sites

I think I may need to explain this a little more.

Basically its a simple game of who has the highest # or if its tied out. I have the script wrote up until its time to compare. The game will be the choice of 2-8 players and I have to find the highest result or if it is a tie and return it all into a message containing the results.

Link to comment
Share on other sites

Ok not sure if I did this right this function kinda confuses  ??? me somewhat. Anyways I need to return username and the roll from this. $winners is returning this "ARRAY". So how would I return them? with this.

 $q = "SELECT username,
         GROUP_CONCAT(CAST(`roll` as CHAR))
         FROM `dice_game_info` WHERE `game_id`=6
         GROUP BY `username`";
$r = mysql_query($q) or die(mysql_error()."<br /><br />".$q);
$row = mysql_fetch_assoc($r);
$winners = explode(",",$row['win']);

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.