jaymc Posted January 28, 2007 Share Posted January 28, 2007 Ive attatched a screenshot of my database structureBasically I want to prodcude some results that will add up all the rows from 1-31 and output the result in the following format1 - total number2 - total number3 - total number4 - total number5 - total number....Whats the best way to do this, with a query? Rather than having 31 seperate queries all using the SUM() method[attachment deleted by admin] Quote Link to comment Share on other sites More sharing options...
bibby Posted January 29, 2007 Share Posted January 29, 2007 I'm trying to blow your mind or nothin, but you can do your flags in a single field, if you felt like it, using bits.[url=http://us3.php.net/manual/en/language.operators.bitwise.php]http://us3.php.net/manual/en/language.operators.bitwise.php[/url][url=http://en.wikipedia.org/wiki/Bitwise_operation]http://en.wikipedia.org/wiki/Bitwise_operation[/url]Say you have 4 flags (quests in a game).! - Defeated Ogre! - Saved the old man! - Helped Timmy out of the well! - Slayed the DragonAny could be on or off at any given time.Assign them binary values relating to decimal place.1 - Defeated Ogre2 - Saved the old man4 - Helped Timmy out of the well8 - Slayed the DragonIf I saved the old man and helped timmy, my number is 6.Some bitwise operators are '&' , '|' (ever wonder why you've been using && and || ? These are tests for truth, return 1 or 0)[code]if (($myNumber & 2) == 2) echo "you had saved the old man";/*$mynumber=6;6 in binary is 110 .2 in binary is 10.So 0110 & (& finds like bits)0010 =-----0010*/[/code]To try to answer your question though, you could use a function. Write it once, use it wether you have 31 fields or 31,000 fields.[code]function countFlags($flagnum){ $q=mysql_query("select sum($flagnum) from flags"); $r=mysql_fetch_row($q); return $r[0];}function countAllFlags(){ for($i=1;$<=31;$i++) $list.=$i . ' - ' . countFlag($i) return $list;}echo countAllFlags();[/code] Quote Link to comment Share on other sites More sharing options...
fenway Posted January 30, 2007 Share Posted January 30, 2007 I agree -- no need for one flag per field. Quote Link to comment 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.