RON_ron Posted December 2, 2010 Share Posted December 2, 2010 I want to get the COUNT of the fields (ArrayM11 - ArrayM19), which has the word "YES". The code isn't working. Appreciate any help. $Array1 = array(); for($j=0;$j<count($ArrayM1);++$j) { $result = array_count_values($ArrayM1[$j]); if (isset($result['YES'])) { $points1 = $result['YES']; } } Quote Link to comment https://forums.phpfreaks.com/topic/220437-isset/ Share on other sites More sharing options...
MasterACE14 Posted December 2, 2010 Share Posted December 2, 2010 I really don't understand what you're trying to do here... ? Quote Link to comment https://forums.phpfreaks.com/topic/220437-isset/#findComment-1142102 Share on other sites More sharing options...
Lostvoices Posted December 2, 2010 Share Posted December 2, 2010 what is ArrayM1? you have defined Array1 but i dont see ArrayM1 defined anywhere and you should probably use a foreach loop and just ++ to a variable if it contains yes if all you're after is how many contain YES Quote Link to comment https://forums.phpfreaks.com/topic/220437-isset/#findComment-1142103 Share on other sites More sharing options...
RON_ron Posted December 2, 2010 Author Share Posted December 2, 2010 Below was my code. that works. I'm trying to convert this using the arrays. $Array1 = array($winRa1, $winRa2, $winRa3, $winRa4, $winRa5, $winRa6, $winRa7, $winRa8); $result = array_count_values($Array1); if (isset($result['YES'])) { $points = $result['YES']; } This is what I'm trying to do now. Basically I need to get the COUNT of the fields which contain the word "YES". $ArrayM1 = array(); $Var = 'name'; $query = "SELECT * FROM db WHERE username = '".$Var."'"; $result = mysql_query($query); $score = mysql_fetch_assoc($result); foreach (range('1','2') as $ltr) { $ArrayM1[] = array($score['ro' . $ltr]); } $Array1 = array(); for($j=0;$j<count($ArrayM1);++$j) { $result = array_count_values($ArrayM1[$j]); if (isset($result['YES'])) { $points1 = $result['YES']; } } Quote Link to comment https://forums.phpfreaks.com/topic/220437-isset/#findComment-1142104 Share on other sites More sharing options...
trq Posted December 2, 2010 Share Posted December 2, 2010 Can you actual explain (in English) what it is your wanting to achieve? Your code is terrible. If you can explain (without examples because they are not helping at all) your goals, we might be able to help you right some decent code. Quote Link to comment https://forums.phpfreaks.com/topic/220437-isset/#findComment-1142110 Share on other sites More sharing options...
Lostvoices Posted December 2, 2010 Share Posted December 2, 2010 really can't understand what you're trying to do.. IF YOU are tying to COUNT how MANY elements in the ARRAY contain the VALUE "YES" then why not just do something like foreach($Array1 as $value){ $value = strtolower($value); if($value == "yes"){ $countingVariable++; } } Quote Link to comment https://forums.phpfreaks.com/topic/220437-isset/#findComment-1142114 Share on other sites More sharing options...
trq Posted December 2, 2010 Share Posted December 2, 2010 really can't understand what you're trying to do.. IF YOU are tying to COUNT how MANY elements in the ARRAY contain the VALUE "YES" then why not just do something like foreach($Array1 as $value){ $value = strtolower($value); if($value == "yes"){ $countingVariable++; } } Because.... $result = array_count_values($Array1); if (isset($result['YES'])) { $points = $result['YES']; } is more efficient. Quote Link to comment https://forums.phpfreaks.com/topic/220437-isset/#findComment-1142115 Share on other sites More sharing options...
RON_ron Posted December 2, 2010 Author Share Posted December 2, 2010 This is my existing code and I need to get the COUNT of the fields which contain the word "YES" out of my_db2. $S1 = array(); $Array1 = array(); $Variable1 = 'somename'; $query1 = "SELECT * FROM my_db1 WHERE username = '".$Variable1."'"; $result1 = mysql_query($query1); $score1 = mysql_fetch_assoc($result1); foreach (range('a','e') as $ltr) { $S1[] = array($score1['mopA' . $ltr]); } $Variable2 = 'nextX'; $query1 = "SELECT * FROM my_db2 WHERE rdx = '".$Variable2."'"; $result = mysql_query($query1); $scoreM = mysql_fetch_assoc($result); foreach (range('a','e') as $ltr) { $Array1[] = array($scoreM['rowB' . $ltr]); } $count = array(); for($i=0;$i<count($S1);++$i) { $count[$i] = count(array_intersect($S1[$i],$Array1[$i])); } $points = array_sum($count); echo $points; The below code worked before I brought in the arrays. But now I'm dealing with arrays and I'm not knowing how to get the count of the fields which contain the word "YES" in my_db2. $result = array_count_values($Array1); if (isset($result['YES'])) { $points = $result['YES']; } Thanks and appreciate for helping! Quote Link to comment https://forums.phpfreaks.com/topic/220437-isset/#findComment-1142120 Share on other sites More sharing options...
trq Posted December 2, 2010 Share Posted December 2, 2010 What does your table structure look like? And what fields might contain the word 'YES'? Quote Link to comment https://forums.phpfreaks.com/topic/220437-isset/#findComment-1142143 Share on other sites More sharing options...
RON_ron Posted December 2, 2010 Author Share Posted December 2, 2010 The structure is like this and the word 'YES' might be in the following fields in my_db2. rowBa | rowBb | rowBc | rowBd | rowBe Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/220437-isset/#findComment-1142148 Share on other sites More sharing options...
trq Posted December 2, 2010 Share Posted December 2, 2010 <?php $sql = "SELECT COUNT(*) AS cnt WHERE 'YES' IN(rowBa, rowBb, rowBc, rowBd, rowBe)"; if ($result = mysql_query($sql)) { $row = mysql_fetch_assoc($result)) { echo $row['cnt']; } } Quote Link to comment https://forums.phpfreaks.com/topic/220437-isset/#findComment-1142150 Share on other sites More sharing options...
RON_ron Posted December 2, 2010 Author Share Posted December 2, 2010 There is an error. Parse error: syntax error, unexpected ')' and I removed a bracket from (marked in red) mysql_fetch_assoc($result)) then it gives another error. Parse error: syntax error, unexpected '{' Quote Link to comment https://forums.phpfreaks.com/topic/220437-isset/#findComment-1142152 Share on other sites More sharing options...
sweeb Posted December 2, 2010 Share Posted December 2, 2010 Since you are only returning one row in your query mysql_fetch_assoc isn't necessary--- if you were using that the syntax would require a while loop after the if statement, however this would work if you're just looking for the counted records: $sql = "SELECT COUNT(*) AS cnt WHERE 'YES' IN(rowBa, rowBb, rowBc, rowBd, rowBe)"; if ($result = mysql_query($sql)) { $row = mysql_fetch_row($result); echo $row['cnt']; } Quote Link to comment https://forums.phpfreaks.com/topic/220437-isset/#findComment-1142154 Share on other sites More sharing options...
RON_ron Posted December 2, 2010 Author Share Posted December 2, 2010 Hi sweeb.... Thanks (and welcome) Actually I want to get the COUNT of 'YES' from the entire databse. E.g. rowBa | rowBb | rowBc | rowBd | rowBe NO | YES | NO | NO | NO NO | NO | YES | YES | YES NO | YES | NO | YES | NO count = 6 Quote Link to comment https://forums.phpfreaks.com/topic/220437-isset/#findComment-1142156 Share on other sites More sharing options...
sweeb Posted December 2, 2010 Share Posted December 2, 2010 Yeah, that code should do it. What is it returning? Quote Link to comment https://forums.phpfreaks.com/topic/220437-isset/#findComment-1142160 Share on other sites More sharing options...
trq Posted December 2, 2010 Share Posted December 2, 2010 Since you are only returning one row in your query mysql_fetch_assoc isn't necessary--- if you were using that the syntax would require a while loop after the if statement, however this would work if you're just looking for the counted records: $sql = "SELECT COUNT(*) AS cnt WHERE 'YES' IN(rowBa, rowBb, rowBc, rowBd, rowBe)"; if ($result = mysql_query($sql)) { $row = mysql_fetch_row($result); echo $row['cnt']; } mysql_fetch_assoc() returns an array just like mysql_fetch_row() does. *assoc returns an associative array though while *row returns a numerically indexed array so your syntax would fail because the cnt index does not exist. And in both cases there is no need for any while loop. Maybe you where thinking of.... $sql = "SELECT COUNT(*) AS cnt WHERE 'YES' IN(rowBa, rowBb, rowBc, rowBd, rowBe)"; if ($result = mysql_query($sql)) { echo mysql_result($result, 0); } which is shorter, but IMO less clear. Quote Link to comment https://forums.phpfreaks.com/topic/220437-isset/#findComment-1142164 Share on other sites More sharing options...
trq Posted December 2, 2010 Share Posted December 2, 2010 Hi sweeb.... Thanks (and welcome) Actually I want to get the COUNT of 'YES' from the entire databse. E.g. rowBa | rowBb | rowBc | rowBd | rowBe NO | YES | NO | NO | NO NO | NO | YES | YES | YES NO | YES | NO | YES | NO count = 6 To do that your going to have to do it the hard way. however, before going down that path, I would put it to you that your database is poorly designed. Why would you have 5 fields all containing the same or similar data? I should have mentioned this in my previous post. Any time you see field names like..... row1, row2, row3, row4 in a database says its a poor design. Look up some tutorials on database normalization and come back when your data is stored properly. Actually, you probably won't need to because a simple query will get you the answers you need. That's one of the benefits of good DB design. Quote Link to comment https://forums.phpfreaks.com/topic/220437-isset/#findComment-1142166 Share on other sites More sharing options...
RON_ron Posted December 2, 2010 Author Share Posted December 2, 2010 Why would you have 5 fields all containing the same or similar data? I really appreciate your input thorpe. Those columns actually represents Months and rowBa, rowBb, rowBc... row stands for the project we're doing and B stands for the team name. Then 1, 2, 3... to differentiate the fields. Is it still poorly designed? Thank you so much for the additional input. Quote Link to comment https://forums.phpfreaks.com/topic/220437-isset/#findComment-1142176 Share on other sites More sharing options...
RON_ron Posted December 2, 2010 Author Share Posted December 2, 2010 Hey thorpe.... I'm curious... isn't the table name required after the SELECT? $sql = "SELECT COUNT(*) AS cnt WHERE 'YES' IN(rowBa, rowBb, rowBc, rowBd, rowBe)"; if ($result = mysql_query($sql)) { echo mysql_result($result, 0); } Quote Link to comment https://forums.phpfreaks.com/topic/220437-isset/#findComment-1142179 Share on other sites More sharing options...
sweeb Posted December 2, 2010 Share Posted December 2, 2010 Those columns actually represents Months and rowBa, rowBb, rowBc... row stands for the project we're doing and B stands for the team name. Then 1, 2, 3... to differentiate the fields. Is it still poorly designed? It would be better off being made into a relational database where you had one table for team declaration with any other info about that team, such as Table Teams ID - TeamName 1 - Team1 2 - Team2 3 - Team3 Table ContainsInfo Team ID - Data - Month - ProjName 1 - YES - 2 - Alpha 2 - NO - 3 - Beta 3 - YES - 4 - Beta This way you only have to query one column for data results, and you can easily filter by Month or Team if needed and return Team info by using joined queries. Quote Link to comment https://forums.phpfreaks.com/topic/220437-isset/#findComment-1142188 Share on other sites More sharing options...
RON_ron Posted December 2, 2010 Author Share Posted December 2, 2010 Sorry for being babyish! But I'm not knowing what to do here. When I insert that piece of code it gives this; You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax $S1 = array(); $Array1 = array(); $Variable1 = 'somename'; $query1 = "SELECT * FROM my_db1 WHERE username = '".$Variable1."'"; $result1 = mysql_query($query1); $score1 = mysql_fetch_assoc($result1); foreach (range('a','e') as $ltr) { $S1[] = array($score1['mopA' . $ltr]); } $Variable2 = 'nextX'; $query1 = "SELECT * FROM my_db2 WHERE rdx = '".$Variable2."'"; $result = mysql_query($query1); $scoreM = mysql_fetch_assoc($result); foreach (range('a','e') as $ltr) { $Array1[] = array($scoreM['rowB' . $ltr]); } $count = array(); for($i=0;$i<count($S1);++$i) { $count[$i] = count(array_intersect($S1[$i],$Array1[$i])); } $points = array_sum($count); echo $points; $sql = "SELECT COUNT(*) AS cnt WHERE 'YES' IN(rowBa, rowBb, rowBc, rowBd, rowBe)"; if ($result = mysql_query($sql)) { $output = mysql_result($result, 0); } echo $output; Thanks for helping me out. Quote Link to comment https://forums.phpfreaks.com/topic/220437-isset/#findComment-1142190 Share on other sites More sharing options...
trq Posted December 2, 2010 Share Posted December 2, 2010 It is indeed missing the table name. Quote Link to comment https://forums.phpfreaks.com/topic/220437-isset/#findComment-1142362 Share on other sites More sharing options...
RON_ron Posted December 2, 2010 Author Share Posted December 2, 2010 I've included the FROM... that did not fix the issue. $sql = "SELECT COUNT(*) AS cnt FROM my_db2 WHERE 'YES' IN(rowBa, rowBb, rowBc, rowBd, rowBe)"; if ($result = mysql_query($sql)) { $output = mysql_result($result, 0); } Quote Link to comment https://forums.phpfreaks.com/topic/220437-isset/#findComment-1142402 Share on other sites More sharing options...
trq Posted December 2, 2010 Share Posted December 2, 2010 That will not give you a total count of the word 'YES'. It will only give you a count of how many rows contain the word 'YES'. Like I said, You'll need to read some tutorials on database normalization (or even take a look at sweeb's reply), and fix your db design before you can move forward. Quote Link to comment https://forums.phpfreaks.com/topic/220437-isset/#findComment-1142422 Share on other sites More sharing options...
RON_ron Posted December 3, 2010 Author Share Posted December 3, 2010 OK. I've used the "COUNT" command before but not with the "IN" command. $sql = "SELECT rowBa, rowBb, rowBc, rowBd, rowBe COUNT(*) AS cnt FROM my_db2 WHERE rowBa, rowBb, rowBc, rowBd, rowBe = 'YES'"; if ($result = mysql_query($sql)) { $output = mysql_result($result, 0); } Quote Link to comment https://forums.phpfreaks.com/topic/220437-isset/#findComment-1142465 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.