Jump to content

Recommended Posts

Ive attatched a screenshot of my database structure

Basically I want to prodcude some results that will add up all the rows from 1-31 and output the result in the following format

1 - total number
2 - total number
3 - total number
4 - total number
5 - 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]
Link to comment
https://forums.phpfreaks.com/topic/36077-adding-columns/
Share on other sites

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 Dragon

Any could be on or off at any given time.
Assign them binary values relating to decimal place.


1 - Defeated Ogre
2 - Saved the old man
4 - Helped Timmy out of the well
8 - Slayed the Dragon

If 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]
Link to comment
https://forums.phpfreaks.com/topic/36077-adding-columns/#findComment-171517
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.