Sideclef Posted September 16, 2008 Share Posted September 16, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/124523-how-to-compare-more-than-one-result/ Share on other sites More sharing options...
Maq Posted September 16, 2008 Share Posted September 16, 2008 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 } Quote Link to comment https://forums.phpfreaks.com/topic/124523-how-to-compare-more-than-one-result/#findComment-643036 Share on other sites More sharing options...
cooldude832 Posted September 16, 2008 Share Posted September 16, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/124523-how-to-compare-more-than-one-result/#findComment-643038 Share on other sites More sharing options...
Sideclef Posted September 16, 2008 Author Share Posted September 16, 2008 Ok this might be a stupid question but what does explode do ? I mean I really don't understand the lines. Quote Link to comment https://forums.phpfreaks.com/topic/124523-how-to-compare-more-than-one-result/#findComment-643053 Share on other sites More sharing options...
Sideclef Posted September 16, 2008 Author Share Posted September 16, 2008 Never mind I see but how will that determine if there are 2 fields as the max? Quote Link to comment https://forums.phpfreaks.com/topic/124523-how-to-compare-more-than-one-result/#findComment-643060 Share on other sites More sharing options...
Sideclef Posted September 16, 2008 Author Share Posted September 16, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/124523-how-to-compare-more-than-one-result/#findComment-643084 Share on other sites More sharing options...
cooldude832 Posted September 16, 2008 Share Posted September 16, 2008 using what I wrote $num_winners = count($winners) if($num_$winners) >1){ #tie $num_winners count of winners } else{ # winner } Quote Link to comment https://forums.phpfreaks.com/topic/124523-how-to-compare-more-than-one-result/#findComment-643132 Share on other sites More sharing options...
Sideclef Posted September 17, 2008 Author Share Posted September 17, 2008 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']); Quote Link to comment https://forums.phpfreaks.com/topic/124523-how-to-compare-more-than-one-result/#findComment-643514 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.