RON_ron Posted November 25, 2010 Share Posted November 25, 2010 I can't get the COUNT of the matching results? Please help. $SomeVar = myname; $queryU = "SELECT * FROM adxone WHERE username = '".$SomeVar."'"; $resultU = mysql_query($queryU); while($scoreP = mysql_fetch_array($resultU)) { $scoreP['roundzAa']; $scoreP['roundzAb']; $scoreP['roundzAc']; $scoreP['roundzAd']; $scoreP['roundzAe']; $scoreP['roundzAf']; $scoreP['roundzAg']; $scoreP['roundzAh']; } $WinV = 'resultA'; $query = "SELECT * FROM acs WHERE resultx = '".$WinV."'"; $result = mysql_query($query); while($scoreUsr = mysql_fetch_array($result)) { $scoreUsr = $scoreM1['winxa']; $scoreUsr = $scoreM1['winxb']; $scoreUsr = $scoreM1['winxc']; $scoreUsr = $scoreM1['winxd']; $scoreUsr = $scoreM1['winxe']; $scoreUsr = $scoreM1['winxf']; $scoreUsr = $scoreM1['winxg']; $scoreUsr = $scoreM1['winxh']; } $count1 = count( array_intersect($resultU, $result) ); echo($count1); mysql_query("UPDATE comp SET id=$count1 WHERE username = '".$SomeVar."'"); ?> Link to comment https://forums.phpfreaks.com/topic/219782-coding-help-to-match-and-count-array-results/ Share on other sites More sharing options...
Pikachu2000 Posted November 25, 2010 Share Posted November 25, 2010 If you had error reporting on, I suspect you'd see that $result and $resultU aren't arrays. They are query result resources. Link to comment https://forums.phpfreaks.com/topic/219782-coding-help-to-match-and-count-array-results/#findComment-1139367 Share on other sites More sharing options...
RON_ron Posted November 25, 2010 Author Share Posted November 25, 2010 Sorry!!!!!!!! Here's the code. But this sends the count 0 all the time?? Also an error message Warning: array_intersect() [function.array-intersect]: Argument #1 is not an array in /home/...../TEST.php on line 36 $SomeVar = myname; $queryU = "SELECT * FROM adxone WHERE username = '".$SomeVar."'"; $resultU = mysql_query($queryU); while($scoreP = mysql_fetch_array($resultU)) { echo $scoreP['roundzAa']; echo $scoreP['roundzAb']; echo $scoreP['roundzAc']; echo $scoreP['roundzAd']; echo $scoreP['roundzAe']; echo $scoreP['roundzAf']; echo $scoreP['roundzAg']; echo $scoreP['roundzAh']; } $WinV = 'resultA'; $query = "SELECT * FROM acs WHERE resultx = '".$WinV."'"; $result = mysql_query($query); while($scoreM1 = mysql_fetch_array($result)) { echo $scoreM1['winxa']; echo $scoreM1['winxb']; echo $scoreM1['winxc']; echo $scoreM1['winxd']; echo $scoreM1['winxe']; echo $scoreM1['winxf']; echo $scoreM1['winxg']; echo $scoreM1['winxh']; } $count1 = count( array_intersect($resultU, $result) ); echo($count1); mysql_query("UPDATE comp SET id=$count1 WHERE username = '".$SomeVar."'"); ?> I'm expecting this would return the count like this; E.g. Assuming database A holds these records $scoreP['roundzAa']; holds "AUS" $scoreP['roundzAb']; holds "AUS" $scoreP['roundzAc]; holds "AUS" $scoreP['roundzAd']; holds "RUS" $scoreP['roundzAe']; holds "RUS" Assuming database B holds these records $scoreM1['winxa']; holds "US" $scoreM1['winxb']; holds "AUS" $scoreM1['winxc']; holds "UK" $scoreM1['winxd']; holds "RUS" $scoreM1['winxe']; holds "AUS" echo = 2//match the results by ORDER and echo the COUNT. Link to comment https://forums.phpfreaks.com/topic/219782-coding-help-to-match-and-count-array-results/#findComment-1139368 Share on other sites More sharing options...
Pikachu2000 Posted November 25, 2010 Share Posted November 25, 2010 $result and $resultU aren't arrays. They are query result resources. You need to build an array for each set of results in the while loop from each query, then use array_intersect() on those. Link to comment https://forums.phpfreaks.com/topic/219782-coding-help-to-match-and-count-array-results/#findComment-1139372 Share on other sites More sharing options...
RON_ron Posted November 25, 2010 Author Share Posted November 25, 2010 I hope this is the way to do this. But it's giving me an error. Parse error: syntax error, unexpected ',' in /home/....../TEST.php on line 108 $SomeVar = myname; $queryU = "SELECT * FROM adxone WHERE username = '".$SomeVar."'"; $resultU = mysql_query($queryU); $scoreU = mysql_fetch_assoc($resultU); $userRa1 = $scoreU['roundzAa']; $userRa2 = $scoreU['roundzAb']; $userRa3 = $scoreU['roundzAc']; $userRa4 = $scoreU['roundzAd']; $userRa5 = $scoreU['roundzAe']; $userRa6 = $scoreU['roundzAf']; $userRa7 = $scoreU['roundzAg']; $userRa8 = $scoreU['roundzAh']; $WinV = 'resultA'; $query = "SELECT * FROM acs WHERE resultx = '".$WinV."'"; $result = mysql_query($query); $scoreM = mysql_fetch_assoc($result); $winRa1 = $scoreM['markwinxa']; $winRa2 = $scoreM['markwinxb']; $winRa3 = $scoreM['markwinxc']; $winRa4 = $scoreM['markwinxd']; $winRa5 = $scoreM['markwinxe']; $winRa6 = $scoreM['markwinxf']; $winRa7 = $scoreM['markwinxg']; $winRa8 = $scoreM['markwinxh']; $ArrayUsr=array($userRa1, $userRa2, $userRa3, $userRa4, $userRa5, $userRa6, $userRa7, $userRa8), $ArrayMrk=array($winRa1, $winRa2, $winRa3, $winRa4, $winRa5, $winRa6, $winRa7, $winRa8, ), $count1 = count( array_intersect($ArrayUsr, $ArrayMrk) ); echo($count1); mysql_query("UPDATE comp SET id=$count1 WHERE username = '".$SomeVar."'"); ?> Link to comment https://forums.phpfreaks.com/topic/219782-coding-help-to-match-and-count-array-results/#findComment-1139382 Share on other sites More sharing options...
RON_ron Posted November 25, 2010 Author Share Posted November 25, 2010 I've spot the coding error and fixed it. it works... but not the way I expected. I'm expecting this would return the count like this; E.g. Assuming database A holds these records $scoreP['roundzAa']; holds "AUS" $scoreP['roundzAb']; holds "AUS" $scoreP['roundzAc]; holds "AUS" $scoreP['roundzAd']; holds "RUS" $scoreP['roundzAe']; holds "RUS" Assuming database B holds these records $scoreM1['winxa']; holds "US" $scoreM1['winxb']; holds "AUS" $scoreM1['winxc']; holds "UK" $scoreM1['winxd']; holds "RUS" $scoreM1['winxe']; holds "AUS" echo = 2//match the results by ORDER and echo the COUNT. Link to comment https://forums.phpfreaks.com/topic/219782-coding-help-to-match-and-count-array-results/#findComment-1139392 Share on other sites More sharing options...
kenrbnsn Posted November 25, 2010 Share Posted November 25, 2010 If you're not using the variables $userRa(1- or $winRa(1- any other places, there are ways to simplify this code. Here's one: <?php $SomeVar = myname; $queryU = "SELECT * FROM adxone WHERE username = '$SomeVar'"; $resultU = mysql_query($queryU); $scoreU = mysql_fetch_assoc($resultU); $WinV = 'resultA'; $query = "SELECT * FROM acs WHERE resultx = '$WinV'"; $result = mysql_query($query); $scoreM = mysql_fetch_assoc($result); $ArrayUsr = array(); $ArrayMrk = array(); foreach(range(a,h) as $ltr) { $ArrayUsr[] = $scoreU['roundzA' . $ltr]; $ArrayMrk[] = $scoreM['markwin' . $ltr]; } $count1 = count( array_intersect($ArrayUsr, $ArrayMrk) ); echo $count1 ; mysql_query("UPDATE comp SET id=$count1 WHERE username = '$SomeVar'"); ?> Ken Link to comment https://forums.phpfreaks.com/topic/219782-coding-help-to-match-and-count-array-results/#findComment-1139393 Share on other sites More sharing options...
RON_ron Posted November 25, 2010 Author Share Posted November 25, 2010 Hi Ken.. that's brilliant. But how can I get the COUNT if result1 of databaseA MATCH with result1 on databaseB and result2 of databaseA MATCH with result2 on databaseB, etc... databaseA - result1, result2, result3... databaseB - result1, result2, result3... Link to comment https://forums.phpfreaks.com/topic/219782-coding-help-to-match-and-count-array-results/#findComment-1139397 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.