nezbo Posted October 29, 2008 Share Posted October 29, 2008 Hi all I have 2 Arrays one is derived form a mysql and the other is a Split array. What I am trying to do is create a report that counts up the number of items in the Split array that match the value in the mysql query. Mysql Split Array Array Site 1 53 Site 2 17 etc... etc... I am not to sure where to start... I have done something like this before but it was very messy and I am sure the must be a better way? Neil Quote Link to comment https://forums.phpfreaks.com/topic/130549-solved-counting-2-arrays/ Share on other sites More sharing options...
bobbinsbro Posted October 29, 2008 Share Posted October 29, 2008 <?php $matches = 0; foreach ($split as $split){ foreach($mysql as $mysql){ if (strcmp($split, $mysql) == 0){ //check for a match ++$matches; //count the match break; //break out of mysql foreach loop and continue the parent loop. } } } ?> this is case sensitive. for case insensitivity, use strcasecmp(). Quote Link to comment https://forums.phpfreaks.com/topic/130549-solved-counting-2-arrays/#findComment-677261 Share on other sites More sharing options...
nezbo Posted October 29, 2008 Author Share Posted October 29, 2008 Cheers for this, but i am a bit confused about how to get mysql and split info in to it and out of it... This is what i have so far? $GetAllSites = @mysql_query("SELECT dental_emerg.userID, dental_emerg.sitePref, dental_emerg.timeStamp, dental_emerg.valid FROM dental_emerg INNER JOIN ( SELECT userID, MAX(timeStamp) as latest FROM dental_emerg GROUP BY userID ) as X ON dental_emerg.userID = X.userID AND dental_emerg.timeStamp = X.latest WHERE dental_emerg.valid = 0"); while ($GetAllSites2 = @mysql_fetch_array($GetAllSites)) { $setVSites = @mysql_query("SELECT * FROM site WHERE Emerg = 1 and valid = 0"); $matches = 0; foreach ($split as $split){ foreach($mysql as $mysql){ if (strcmp($split, $mysql) == 0){ //check for a match ++$matches; //count the match break; //break out of mysql foreach loop and continue the parent loop. } } } } <?php $matches = 0; foreach ($split as $split){ foreach($mysql as $mysql){ if (strcmp($split, $mysql) == 0){ //check for a match ++$matches; //count the match break; //break out of mysql foreach loop and continue the parent loop. } } } ?> Case Sensitivity dosnt matter cos i am using numeric ID and i want to link that to the Site name etc... this is case sensitive. for case insensitivity, use strcasecmp(). Quote Link to comment https://forums.phpfreaks.com/topic/130549-solved-counting-2-arrays/#findComment-677270 Share on other sites More sharing options...
bobbinsbro Posted October 29, 2008 Share Posted October 29, 2008 first of all, if your using numeric comparison, use if ($split == $mysql){ instead of if (strcmp($split, $mysql) == 0){ i'm assuming $GetAllSites2 is the $mysql array, and $setVSites is the $split. so first you want to $setVSites2 = mysql_fetch_array($setVSites); then swap all $split for $setVSites2 and all $mysql for $GetAllSites2. then the code will search for each value in $setVSites2 in the array $GetAllSites2, and count the amount of matches into $matches. to use the result, simply use $matches somewhere after the loop. Quote Link to comment https://forums.phpfreaks.com/topic/130549-solved-counting-2-arrays/#findComment-677279 Share on other sites More sharing options...
nezbo Posted October 29, 2008 Author Share Posted October 29, 2008 I think i am starting to understand... I now have this code : $GetLastEntry = @mysql_query("SELECT dental_emerg.userID, dental_emerg.sitePref, dental_emerg.timeStamp, dental_emerg.valid FROM dental_emerg INNER JOIN ( SELECT userID, MAX(timeStamp) as latest FROM dental_emerg GROUP BY userID ) as X ON dental_emerg.userID = X.userID AND dental_emerg.timeStamp = X.latest WHERE dental_emerg.valid = 0"); while ($GetLastEntry2 = @mysql_fetch_array($GetLastEntry)) { $setVSites = @mysql_query("SELECT * FROM site WHERE Emerg = 1 and valid = 0"); //This is the mysql $split = split(";", $GetLastEntry2['sitePref']; //this is the split $matches = 0; foreach ($split as $split){ foreach($setVSites as $setVSites){ if ($split == $mysql){ //check for a match ++$matches; //count the match break; //break out of mysql foreach loop and continue the parent loop. } } } } So how do I get the information on the screen? Quote Link to comment https://forums.phpfreaks.com/topic/130549-solved-counting-2-arrays/#findComment-677299 Share on other sites More sharing options...
bobbinsbro Posted October 29, 2008 Share Posted October 29, 2008 the code wont work, because mysql_query() returns an object, not an array, so $setVSites isn't an array and connot be used in a foreach() loop. to print the number of matches to screen, just use echo $matches; to get $setVSites into an array you have to use $someVariable = mysql_fetch_array($setVSites); the problem is that as far as i know, $setVSites may contain less rows than $GetLastEntry2, so at some point you may be trying to access a row that doesn't exist (because the loop will continue untill there are no more rows in $GetLastEntry2). Quote Link to comment https://forums.phpfreaks.com/topic/130549-solved-counting-2-arrays/#findComment-677304 Share on other sites More sharing options...
nezbo Posted October 29, 2008 Author Share Posted October 29, 2008 Cheers for all the advice but i am not to sure how to get it to work so i have now come up with this code... This also still dosnt work and i am not to sure why? $GetLastEntry = @mysql_query("SELECT dental_emerg.userID, dental_emerg.sitePref, dental_emerg.timeStamp, dental_emerg.valid FROM dental_emerg INNER JOIN ( SELECT userID, MAX(timeStamp) as latest FROM dental_emerg GROUP BY userID ) as X ON dental_emerg.userID = X.userID AND dental_emerg.timeStamp = X.latest WHERE dental_emerg.valid = 0"); $setVSites = @mysql_query("SELECT * FROM site WHERE Emerg = 1 and valid = 0"); //This is the mysql for getting the sites while ($someVariable = @mysql_fetch_array($setVSites))//convert the mysql object to array { $numberZero = 0; echo $someVariable['SiteName'] . "="; while ($GetLastEntry2 = @mysql_fetch_array($GetLastEntry))//convert the mysql object to array { $split = split(";", $GetLastEntry2['sitePref']); //this is the split $countSplit = count($split); //count the number of splits for($i=0; $i <= $countSplit; $i++) { if ($split[$i] == $someVariable['SiteID']) { $numberZero = $numberZero + 1; } } } echo $numberZero; echo "<br>"; } Quote Link to comment https://forums.phpfreaks.com/topic/130549-solved-counting-2-arrays/#findComment-677353 Share on other sites More sharing options...
bobbinsbro Posted October 29, 2008 Share Posted October 29, 2008 what's the output? btw, you should be using explode() instead of split() for what your doing. does that make any difference? Quote Link to comment https://forums.phpfreaks.com/topic/130549-solved-counting-2-arrays/#findComment-677359 Share on other sites More sharing options...
nezbo Posted October 29, 2008 Author Share Posted October 29, 2008 I am getting the results of all the sites : But I am only getting the results for 1 site and 0 for the rest, i know there are some i there... PCC=1 St Peters Centre=0 Darwen Health Centre=0 Bacup Health Centre=0 Haslingden Health Centre=0 Howard Street Community=0 Larkhill Health Centre=0 Montague Health Centre=0 Special Care Dentistry (RBH)=0 Colne Sure Start=0 Yarnspinners PCC=0 Clitheroe Hospital=0 RBH (GA Office)=0 Oak House Dental Centre=0 Quote Link to comment https://forums.phpfreaks.com/topic/130549-solved-counting-2-arrays/#findComment-677367 Share on other sites More sharing options...
nezbo Posted October 29, 2008 Author Share Posted October 29, 2008 The working code :: $setVSites = @mysql_query("SELECT * FROM site WHERE Emerg = 1 and valid = 0"); //This is the mysql for getting the sites while ($someVariable = @mysql_fetch_array($setVSites))//convert the mysql object to array { echo "<tr><td>"; $GetLastEntry = @mysql_query("SELECT dental_emerg.userID, dental_emerg.sitePref, dental_emerg.timeStamp, dental_emerg.valid FROM dental_emerg INNER JOIN ( SELECT userID, MAX(timeStamp) as latest FROM dental_emerg GROUP BY userID ) as X ON dental_emerg.userID = X.userID AND dental_emerg.timeStamp = X.latest WHERE dental_emerg.valid = 0"); $numberZero = 0; echo $someVariable['SiteName']; while ($GetLastEntry2 = @mysql_fetch_array($GetLastEntry))//convert the mysql object to array { $splited = split(";", $GetLastEntry2['sitePref']); //this is the split $countSplit = count($splited); //count the number of splits for($i=0; $i <= $countSplit; $i++) { if ($splited[$i] == $someVariable['SiteID']) { $numberZero = $numberZero + 1; } } } echo "</td><td> = " . $numberZero . "</tb></tr>"; } Quote Link to comment https://forums.phpfreaks.com/topic/130549-solved-counting-2-arrays/#findComment-677377 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.