Lucky2710 Posted July 20, 2010 Share Posted July 20, 2010 I'm in the process of creating a site that you can make sports picks on. (Like college football, NASCAR, ect.) Im using mySQL and php for 99% of the site and now I'm to the point of scoring, and leader boards. I'm using a function to retrieve from the database each users picks. The function outputs an array of the users picks. Array ( [1] => 29 [2] => 2 [3] => 48 ) I have a user set up that I'm going to input the actual results of the game/race into. I want to somehow compare my user to all the other users picks and then award a set number of points to that particular user. I have a field in the database table setup for points that points will get updated into for each user. What i need help with is comparing the arrays of 'picks' and some way to award points based on the result of the arrays comparison. Here is my code so far... <?php //Get User id #'s $sql="SELECT id FROM users WHERE approved=1"; $result=mysql_query($sql); while($row = mysql_fetch_object($result)) { $ids[] = $row->id; } ($ids); //---------------------------------------------------------- //Call Race Picks function racePicks($z) { $sql1 = "SELECT nc_july25_1, nc_july25_2, nc_july25_3 FROM users WHERE id = $z"; $result1 = mysql_query($sql1); while ($admin_score = mysql_fetch_object($result1)) { $july25[1] = $admin_score->nc_july25_1; $july25[2] = $admin_score->nc_july25_2; $july25[3] = $admin_score->nc_july25_3; } } //----------------------------------------------------------- racePicks($ids[0]); //This is the Admin Array racePicks($ids[1]); ?> If you have any idea on how to achieve what I'm trying to do please help me or if you think I'm going at this all wrong tell me that too. Link to comment https://forums.phpfreaks.com/topic/208349-array-comparison-help/ Share on other sites More sharing options...
Andy-H Posted July 20, 2010 Share Posted July 20, 2010 <?php //Get User id #'s $sql="SELECT id FROM users WHERE approved=1"; $result=mysql_query($sql); while($row = mysql_fetch_object($result)) { $ids[] = $row->id; } //---------------------------------------------------------- //Call Race Picks function racePicks($z) { $sql1 = "SELECT nc_july25_1, nc_july25_2, nc_july25_3 FROM users WHERE id = $z"; $result1 = mysql_query($sql1); return mysql_fetch_row($result1); } //----------------------------------------------------------- $picks = racePicks($ids[0]); //This is the Admin Array $guess = racePicks($ids[1]); $correct = true; for ($i = 0; $picks[$i] == $guess[$i], $i < 2; $i++) { continue; $correct = false; break; } if ($correct) { //they got em right! } else { //they got em wrong } ?> Link to comment https://forums.phpfreaks.com/topic/208349-array-comparison-help/#findComment-1088850 Share on other sites More sharing options...
Andy-H Posted July 20, 2010 Share Posted July 20, 2010 <?php //Get User id #'s $sql="SELECT id FROM users WHERE approved=1"; $result=mysql_query($sql); while($row = mysql_fetch_object($result)) { $ids[] = $row->id; } //---------------------------------------------------------- //Call Race Picks function racePicks($z) { $sql1 = "SELECT nc_july25_1, nc_july25_2, nc_july25_3 FROM users WHERE id = $z"; $result1 = mysql_query($sql1); return mysql_fetch_row($result1); } //----------------------------------------------------------- $picks = racePicks($ids[0]); //This is the Admin Array $guess = racePicks($ids[1]); $correct = array_diff($guess, $picks); $correct = (empty($correct)); if ($correct) { //they got em right! } else { //they got em wrong } ?> I got it wrong, lol for loop only counts 0, 1 Link to comment https://forums.phpfreaks.com/topic/208349-array-comparison-help/#findComment-1088858 Share on other sites More sharing options...
Andy-H Posted July 20, 2010 Share Posted July 20, 2010 And the comparison needed to be in an if statement inside the loop to handle the continue / break. 2nd attempt should work ok IF I understand what you're trying to achieve. Anyway, the above is a sign that it's time for bed, good luck! Night. Link to comment https://forums.phpfreaks.com/topic/208349-array-comparison-help/#findComment-1088861 Share on other sites More sharing options...
Lucky2710 Posted July 21, 2010 Author Share Posted July 21, 2010 Ya that still doesn't work. That script compares that the arrays have the same size, its not comparing whats inside the array. Its comparing that "(Array ( [1] => 57 [2] =>60 [3] => 00 ) " = "Array ( [1] => 29[2] => 2 [3] => 48 ) " I need it to compare the values inside the array to one another. ''57 -> 29" "60 -> 2" ect. And thats where I'm lost at! Link to comment https://forums.phpfreaks.com/topic/208349-array-comparison-help/#findComment-1089054 Share on other sites More sharing options...
Andy-H Posted July 22, 2010 Share Posted July 22, 2010 Are the values you're selecting stored as varchar (or anything that will return a string value?), if so the last one should work. <?php //Get User id #'s $sql="SELECT id FROM users WHERE approved=1"; $result=mysql_query($sql); while($row = mysql_fetch_object($result)) { $ids[] = $row->id; } //---------------------------------------------------------- //Call Race Picks function racePicks($z) { $sql1 = "SELECT nc_july25_1, nc_july25_2, nc_july25_3 FROM users WHERE id = $z"; $result1 = mysql_query($sql1); return mysql_fetch_row($result1); } //----------------------------------------------------------- $picks = racePicks($ids[0]); //This is the Admin Array $guess = racePicks($ids[1]); $correct = array_diff($guess, $picks); $correct = (empty($correct)); if ($correct) { //they got em right! } else { //they got em wrong } ?> Otherwise: <?php //Get User id #'s $sql="SELECT id FROM users WHERE approved=1"; $result=mysql_query($sql); while($row = mysql_fetch_object($result)) { $ids[] = $row->id; } //---------------------------------------------------------- //Call Race Picks function racePicks($z) { $sql1 = "SELECT nc_july25_1, nc_july25_2, nc_july25_3 FROM users WHERE id = $z"; $result1 = mysql_query($sql1); return mysql_fetch_row($result1); } //----------------------------------------------------------- $picks = racePicks($ids[0]); //This is the Admin Array $guess = racePicks($ids[1]); $correct = true; foreach($guess as $val) { if (in_array($val, $picks)) { continue; } else { $correct = false; break; } } if ($correct) { //they got em right! } else { //they got em wrong } //for debugging echo '<pre>' . print_r($guess, true) . "\r\n\r\n" . print_r($picks, true) . '</pre>'; ?> Link to comment https://forums.phpfreaks.com/topic/208349-array-comparison-help/#findComment-1089702 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.