Ryflex Posted September 19, 2010 Share Posted September 19, 2010 Hi everyone, Like the title says I need to count the amount of 1's in a column but I can't figure out how. Gr and thanx already Ryflex Quote Link to comment https://forums.phpfreaks.com/topic/213837-count-the-number-of-multiple-showing-variable-in-a-column/ Share on other sites More sharing options...
colleyboy Posted September 19, 2010 Share Posted September 19, 2010 Probably a simple multiplication script... i.e: <?php $column1+$column2+$column3+$column4+$column5+$column6+$column7=$total; echo ('$total'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/213837-count-the-number-of-multiple-showing-variable-in-a-column/#findComment-1112943 Share on other sites More sharing options...
Adam Posted September 19, 2010 Share Posted September 19, 2010 'Column' can have several meanings, can you be more specific? Quote Link to comment https://forums.phpfreaks.com/topic/213837-count-the-number-of-multiple-showing-variable-in-a-column/#findComment-1112944 Share on other sites More sharing options...
Ryflex Posted September 19, 2010 Author Share Posted September 19, 2010 I mean a single column with about 20 records (will be more later on) and it is standard filled with all 0's. When 1 of the rows has a 1 in it it means that row is busy upgrading and the rest should not be able to. That's why I need to have something that tells me if there's a 1 in the column. Thnx for looking in to it Ryflex Quote Link to comment https://forums.phpfreaks.com/topic/213837-count-the-number-of-multiple-showing-variable-in-a-column/#findComment-1112948 Share on other sites More sharing options...
colleyboy Posted September 19, 2010 Share Posted September 19, 2010 for example... say you have named the rows... or if this in array just basically make sure you add and total up the rows/columns you want to get the total of. Pretty simple. I.E: row 1's name is andy row 2's name is bill. both have 1 in each. say for example: <?php $bill = ['bill']; $andy = ['andy']; $total = $andy+$bill; echo '$total'; ?> something like that Quote Link to comment https://forums.phpfreaks.com/topic/213837-count-the-number-of-multiple-showing-variable-in-a-column/#findComment-1112949 Share on other sites More sharing options...
Ryflex Posted September 19, 2010 Author Share Posted September 19, 2010 @ Colleyboy thnx for the quick reply but that won't be the answer since it's about the column in the database. After a while there will be a 1000 or more records.... I need a more universal solution I guess... Thnx anyway Ryflex Quote Link to comment https://forums.phpfreaks.com/topic/213837-count-the-number-of-multiple-showing-variable-in-a-column/#findComment-1112951 Share on other sites More sharing options...
Adam Posted September 19, 2010 Share Posted September 19, 2010 Just run a query like: select count(column_name) from table_name where column_name = 1 Quote Link to comment https://forums.phpfreaks.com/topic/213837-count-the-number-of-multiple-showing-variable-in-a-column/#findComment-1112952 Share on other sites More sharing options...
PFMaBiSmAd Posted September 19, 2010 Share Posted September 19, 2010 An example showing some data and the expected results for that data would certainly help. Quote Link to comment https://forums.phpfreaks.com/topic/213837-count-the-number-of-multiple-showing-variable-in-a-column/#findComment-1112954 Share on other sites More sharing options...
Ryflex Posted September 19, 2010 Author Share Posted September 19, 2010 building finish upgrading house 1284944810 1 shed 0 0 mall 0 0 In my code it says when the time now exceeds the finish in the table set it 0 and set upgrading 0. While house is upgrading the other 2 shouldn't be able to. What I need is a simple SELECT that gives me how much 1's there are (because later on it will be possible to upgrade 2 at a time. Thnx again Ryflex Quote Link to comment https://forums.phpfreaks.com/topic/213837-count-the-number-of-multiple-showing-variable-in-a-column/#findComment-1112968 Share on other sites More sharing options...
Adam Posted September 19, 2010 Share Posted September 19, 2010 select count(upgrading) from table_name where upgrading = 1 Quote Link to comment https://forums.phpfreaks.com/topic/213837-count-the-number-of-multiple-showing-variable-in-a-column/#findComment-1112972 Share on other sites More sharing options...
Ryflex Posted September 19, 2010 Author Share Posted September 19, 2010 @ MrAdam I tried the following but still it upgrades when it's supossed not to. $count_query = "Select count(upgrading) From buildings Where upgrading = '1'"; $count_upgrading = mysql_query($count_query); // Data CHECK if($count_upgrading > '0') Thnx Ryflex Quote Link to comment https://forums.phpfreaks.com/topic/213837-count-the-number-of-multiple-showing-variable-in-a-column/#findComment-1112977 Share on other sites More sharing options...
Adam Posted September 19, 2010 Share Posted September 19, 2010 Have a read up on how to use mysql_query right - you need to use it in conjunction with mysql_fetch_array. Quote Link to comment https://forums.phpfreaks.com/topic/213837-count-the-number-of-multiple-showing-variable-in-a-column/#findComment-1112982 Share on other sites More sharing options...
Ryflex Posted September 19, 2010 Author Share Posted September 19, 2010 @ MrAdam $count_query = "Select count(upgrading) From buildings Where upgrading = '1'"; $count_mysql = mysql_query($count_query); $count_array = mysql_fetch_array($count_mysql); $count_upgrading = $count_array['count']; Sorry just starting so still trying to find shortcuts where there aren't any. This better??? Because now I'm not able to upgrade anything anymore.... Thnx Ryflex Quote Link to comment https://forums.phpfreaks.com/topic/213837-count-the-number-of-multiple-showing-variable-in-a-column/#findComment-1112987 Share on other sites More sharing options...
Ryflex Posted September 20, 2010 Author Share Posted September 20, 2010 Can anyone help me please??? Ryflex Quote Link to comment https://forums.phpfreaks.com/topic/213837-count-the-number-of-multiple-showing-variable-in-a-column/#findComment-1113023 Share on other sites More sharing options...
mikosiko Posted September 20, 2010 Share Posted September 20, 2010 ...... Because now I'm not able to upgrade anything anymore.... Thnx Ryflex better if you post most of the code than you have... you last sentence is telling that you have more code that is been affected for what you are showing... without knowing/seeing the rest of the code is impossible to know what is happening in reality Quote Link to comment https://forums.phpfreaks.com/topic/213837-count-the-number-of-multiple-showing-variable-in-a-column/#findComment-1113049 Share on other sites More sharing options...
PFMaBiSmAd Posted September 20, 2010 Share Posted September 20, 2010 If you want to reference the result of count(upgrading) as $count_array['count'], you would need to use an alias name count in the query - Select count(upgrading) as count If you were developing and debugging your code on a system with error_reporting set to E_ALL and display_errors set to ON, you would be getting an undefined error message when referencing $count_array['count']. Quote Link to comment https://forums.phpfreaks.com/topic/213837-count-the-number-of-multiple-showing-variable-in-a-column/#findComment-1113050 Share on other sites More sharing options...
mikosiko Posted September 20, 2010 Share Posted September 20, 2010 good catch PFMaBiSmAd... is so natural for me to use aliases always that I didn't look for that detail Quote Link to comment https://forums.phpfreaks.com/topic/213837-count-the-number-of-multiple-showing-variable-in-a-column/#findComment-1113056 Share on other sites More sharing options...
Ryflex Posted September 20, 2010 Author Share Posted September 20, 2010 @ PFMaBiSmAd and mikosiko Thnx for the solution. I didn't know you should make aliases (Didn't know they existed) but it works perfectly now. Ryflex Quote Link to comment https://forums.phpfreaks.com/topic/213837-count-the-number-of-multiple-showing-variable-in-a-column/#findComment-1113068 Share on other sites More sharing options...
Adam Posted September 20, 2010 Share Posted September 20, 2010 I didn't know you should make aliases You don't have to assign it an alias. By fetching the data with mysql_fetch_array you could have used either $count_array[0] or $count_array['count(upgrading)'] to access the value. Quote Link to comment https://forums.phpfreaks.com/topic/213837-count-the-number-of-multiple-showing-variable-in-a-column/#findComment-1113116 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.