mazman13 Posted October 25, 2007 Share Posted October 25, 2007 Ok so I pulled certain collums from my database. The value of the collums is either Y or N. I want to figure out on each $var how many Y's there are. I don't care how many N's. At the end I'm going to add up all the Y's of all the VARs for stats. Whats the best way of counting Y's in a $var? Link to comment https://forums.phpfreaks.com/topic/74774-solved-need-help-with-looking-in-var-for-certain-info/ Share on other sites More sharing options...
pocobueno1388 Posted October 25, 2007 Share Posted October 25, 2007 You just want to count how many Y's are in that column of the database, correct? <?php $query = "SELECT COUNT(column) AS num FROM table WHERE column='Y'"; $result = mysql_query($query)or die(mysql_error()); $row = mysql_fetch_assoc($result); echo "There are $num Y's"; ?> Link to comment https://forums.phpfreaks.com/topic/74774-solved-need-help-with-looking-in-var-for-certain-info/#findComment-378025 Share on other sites More sharing options...
mazman13 Posted October 25, 2007 Author Share Posted October 25, 2007 That might work but I already took a bunch of collums out of the database. So I want to do a loop or something to count the Y's in the $var Link to comment https://forums.phpfreaks.com/topic/74774-solved-need-help-with-looking-in-var-for-certain-info/#findComment-378033 Share on other sites More sharing options...
pocobueno1388 Posted October 25, 2007 Share Posted October 25, 2007 Could you show your code so it's easier to see what your doing? Link to comment https://forums.phpfreaks.com/topic/74774-solved-need-help-with-looking-in-var-for-certain-info/#findComment-378035 Share on other sites More sharing options...
mazman13 Posted October 25, 2007 Author Share Posted October 25, 2007 $connection = mysql_connect($host,$user,$password) or die ("Sorry. Something messed up and it couldn't connect to the dumb server."); $db = mysql_select_db($database, $connection) or die ("Something is messed up with the database."); $query = "SELECT greeted,quality,cleanliness,service,informed,promised,additional,refer FROM data"; $result = mysql_query($query) or die ("Can't do anything with the query!"); $total = mysql_num_rows($result); while ($row = mysql_fetch_array($result,MYSQL_ASSOC)) { extract($row); .....and this is where im stuck Link to comment https://forums.phpfreaks.com/topic/74774-solved-need-help-with-looking-in-var-for-certain-info/#findComment-378042 Share on other sites More sharing options...
premiso Posted October 25, 2007 Share Posted October 25, 2007 Extracting will not work within a loop. No real reason to extract anyways I would suggest this: while ($row = mysql_fetch_array($result,MYSQL_ASSOC)) { $rows = $row; } foreach ($rows as $row) { echo $row['greeted']; // or now extract($row); echo $greeted; } You will have to use the extracted values before the loop continues on if that is the way you choose to go. Link to comment https://forums.phpfreaks.com/topic/74774-solved-need-help-with-looking-in-var-for-certain-info/#findComment-378054 Share on other sites More sharing options...
kenrbnsn Posted October 25, 2007 Share Posted October 25, 2007 You should use the function array_count_values() <php $query = "SELECT greeted,quality,cleanliness,service,informed,promised,additional,refer FROM data"; $result = mysql_query($query) or die ("Can't do anything with the query!"); $total = mysql_num_rows($result); while ($row = mysql_fetch_assoc($result)) { $tmp = array_count_values($row); echo 'There are ' . $tmp['Y'] . ' Ys in this row<br>'; } ?> Ken Link to comment https://forums.phpfreaks.com/topic/74774-solved-need-help-with-looking-in-var-for-certain-info/#findComment-378103 Share on other sites More sharing options...
mazman13 Posted October 25, 2007 Author Share Posted October 25, 2007 The columns contain either a Y or a N. I want to count how many Ys are in one column...wouldn't that function count everything in that array? Maybe I just missed something. Thanks again! Link to comment https://forums.phpfreaks.com/topic/74774-solved-need-help-with-looking-in-var-for-certain-info/#findComment-378184 Share on other sites More sharing options...
BlueSkyIS Posted October 25, 2007 Share Posted October 25, 2007 that function returns an array containing the count of each element type, how many N's how many Y's, etc. Link to comment https://forums.phpfreaks.com/topic/74774-solved-need-help-with-looking-in-var-for-certain-info/#findComment-378186 Share on other sites More sharing options...
mazman13 Posted October 25, 2007 Author Share Posted October 25, 2007 I see! Thanks. I'll try that. Link to comment https://forums.phpfreaks.com/topic/74774-solved-need-help-with-looking-in-var-for-certain-info/#findComment-378199 Share on other sites More sharing options...
darkfreaks Posted October 25, 2007 Share Posted October 25, 2007 dont forget to click topic solved Link to comment https://forums.phpfreaks.com/topic/74774-solved-need-help-with-looking-in-var-for-certain-info/#findComment-378209 Share on other sites More sharing options...
mazman13 Posted October 26, 2007 Author Share Posted October 26, 2007 The problem with array_count_values is that when in the loop it returns the info as: 111111 I want the combined total. What can I do? Link to comment https://forums.phpfreaks.com/topic/74774-solved-need-help-with-looking-in-var-for-certain-info/#findComment-378576 Share on other sites More sharing options...
premiso Posted October 26, 2007 Share Posted October 26, 2007 More specification would help. Combined totals of what? Of the total Y's or the total Y's for a column? <?php $query = "SELECT greeted,quality,cleanliness,service,informed,promised,additional,refer FROM data"; $result = mysql_query($query) or die ("Can't do anything with the query!"); $total = mysql_num_rows($result); $totalsy = array("greeted" => 0, "quality" => 0, "cleanliness" => 0, "service" =>0, "informed"=>0, "promised" =>0, "additional" =>0, "refer" => 0); $totalsn = array("greeted" => 0, "quality" => 0, "cleanliness" => 0, "service" =>0, "informed"=>0, "promised" =>0, "additional" =>0, "refer" => 0); while ($row = mysql_fetch_assoc($result)) { foreach ($totals as $key => $val) { if ($row[$key] == "Y") { $totalsy[$key] = $val + $totalsy[$key]; }else { $totalsn[$key] = $val + $totalsn[$key]; } } } echo 'The total amount of y\'s for greeted was ' . $totalsy['greeted']; ?> Assuming that is what you are after. A more dynamic approach would be: <?php $query = "SELECT greeted,quality,cleanliness,service,informed,promised,additional,refer FROM data"; $result = mysql_query($query) or die ("Can't do anything with the query!"); $total = mysql_num_rows($result); $totals = array(); while ($row = mysql_fetch_assoc($result)) { foreach ($row as $key => $val) { if ($row[$key] == "Y") { $totals[$key]['y'] = $val + $totals[$key]['y']; }else { $totals[$key]['n'] = $val + $totals[$key]['n']; } } } echo 'The total amount of y\'s for greeted was ' . $totals['greeted']['y']; ?> Link to comment https://forums.phpfreaks.com/topic/74774-solved-need-help-with-looking-in-var-for-certain-info/#findComment-378594 Share on other sites More sharing options...
mazman13 Posted October 26, 2007 Author Share Posted October 26, 2007 Thanks! Link to comment https://forums.phpfreaks.com/topic/74774-solved-need-help-with-looking-in-var-for-certain-info/#findComment-378731 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.