hopbop Posted May 21, 2012 Share Posted May 21, 2012 So I'm sitting at my desk scratching my head on how to do a task where I pull information form a database (that i know how to do) but then i need to see if two variables equal a string and if they do count out how many times that two variables equal there two string checks and out put the counted number ? this is what I have thought of but dose not to work because I cant figure out a way to code it? $dbPull = mysql_fetch_array($result) if( $dbPull['Foo'] == 'string1' && $bdPull['Bar'] == 'string2') this is were I get lost I'm looking for (how many times in the DB dose String2 show up in the array were the above, IF statement is true ) any ideas would be most appreciated Quote Link to comment https://forums.phpfreaks.com/topic/262875-i-cant-figure-out-how-to-handle-this/ Share on other sites More sharing options...
PeoMachine Posted May 21, 2012 Share Posted May 21, 2012 I'm not sure if i understand what you need... you need to count how much times these values repeat on the array fetched from the database, right? you can use: array_search('foo', $yourArray); http://php.net/array_search Quote Link to comment https://forums.phpfreaks.com/topic/262875-i-cant-figure-out-how-to-handle-this/#findComment-1347317 Share on other sites More sharing options...
hopbop Posted May 21, 2012 Author Share Posted May 21, 2012 I'm not sure if i understand what you need... you need to count how much times these values repeat on the array fetched from the database, right? you can use: array_search('foo', $yourArray); http://php.net/array_search I gave that a try but it echoed out 53 but it should of sad 2 ? Quote Link to comment https://forums.phpfreaks.com/topic/262875-i-cant-figure-out-how-to-handle-this/#findComment-1347325 Share on other sites More sharing options...
PeoMachine Posted May 21, 2012 Share Posted May 21, 2012 What you really need? You will fetch it only one time or more? If you need to count it on the same column everytime, use the SQL Count. Quote Link to comment https://forums.phpfreaks.com/topic/262875-i-cant-figure-out-how-to-handle-this/#findComment-1347338 Share on other sites More sharing options...
hopbop Posted May 21, 2012 Author Share Posted May 21, 2012 ill try to exsplan it diffrently what i am trying to do is display a count for itemA in the database were if itemB = foo and itemA = bar ... so if say db row one itemB is boo and itemA is bar and db row two itemB is foo and itemA is bar the over all count is 1 Quote Link to comment https://forums.phpfreaks.com/topic/262875-i-cant-figure-out-how-to-handle-this/#findComment-1347342 Share on other sites More sharing options...
PeoMachine Posted May 21, 2012 Share Posted May 21, 2012 Try to use the SQL: SELECT COUNT(*) FROM table WHERE itemA = 'foo' and itemB = 'bar'; Quote Link to comment https://forums.phpfreaks.com/topic/262875-i-cant-figure-out-how-to-handle-this/#findComment-1347346 Share on other sites More sharing options...
Kays Posted May 21, 2012 Share Posted May 21, 2012 Just query it. Depending on how your table is structured, I'm thinking you need to run a sub-select. Example: SELECT COUNT(*) AS col1_count, col2_count FROM `table` T1, (SELECT COUNT(*) AS col2_count FROM `table` WHERE `col2` = 'itemB') T2 WHERE `col1` = 'itemA'; Quote Link to comment https://forums.phpfreaks.com/topic/262875-i-cant-figure-out-how-to-handle-this/#findComment-1347349 Share on other sites More sharing options...
hopbop Posted May 21, 2012 Author Share Posted May 21, 2012 thank you, you are gentlemen and scholars in my book I have now got it working and here is the code for record include '../config.php'; $query = "SELECT COUNT(*) AS varb FROM clients WHERE col1='foo' and col2 ='bar'"; $result = mysql_query($query); while($data=mysql_fetch_array($result)){ $count = $data['varb']; } echo $count; Quote Link to comment https://forums.phpfreaks.com/topic/262875-i-cant-figure-out-how-to-handle-this/#findComment-1347354 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.