We hope you enjoy your visit.

You're currently viewing our forum as a guest. This means you are limited to certain areas of the board and there are some features you can't use. If you join our community, you'll be able to access member-only sections, and use many member-only features such as customizing your profile, sending personal messages, and voting in polls. Registration is simple, fast, and completely free.


Join our community!


If you're already a member please log in to your account to access all of our features:

Username:   Password:
Add Reply
php/mysql; adding values
Topic Started: Apr 1 2005, 08:03 AM (292 Views)
mathiaus
Member Avatar
mathiaus maximus™
[ *  *  *  *  *  * ]
Right, it's me again!

I thought I'd post this here so I wouldn't have to wait till after IF dropouts

This one I just can't get my head around at all :ph34r:
anyways here my problem....

I have a mysql with a table called members.
The things in this row are

  • id
  • name
  • memgroup
  • points

Now what I am trying to do is to take all the values of 'points' and add them together for each 'memgroup'.

So far the best thing I have found is to add all of the rows for each group up rather than the actual values. This was using the count(*) thingy.

There is a set number of memgroups which is 6 but a changing number of members within each group and overall

Any ideas or help or links or basicly anything which results in me achieving me this would be very mutch appreciated ;)

Many thanks in advanced
Matt :P
Offline Profile Quote Post Goto Top
 
Rory
Member Avatar
i;m a mess
[ *  *  *  *  *  *  * ]
OK, if you have 6 membergroups, are they numbered 1-6? or are called something? if they are not numbered, then what you should do is create a new table called membergroups and give it a primary key called membergroup_id and then the name, permissions etc. (depending on what your membergroup will do)

then, in your members table replace membergroup with membergroup_id as a foreign key.

ok, so if you have the member groups numbered 1-6, then you should do a for statement that loops through each membergroup number:

Code:
 

$countArray = array();
for ($i = 1; $i<=6; $i++) {
      $pointCount = 0;
     $firstQuery = "SELECT * FROM members WHERE membergroup_id = '$i'";
     $firstResult = mysql_query($firstQuery);
     while($row = mysql_fetch_array($firstResult, MYSQL_NUM)) {
            $pointCount += $row[3];
     }
     $countArray[$i] = $pointCount;
}


you should then be able to access what the total for each membergroup by going:

echo $countArray[2];

^ that should show the 2nd membergroup count.

I'm not 100% sure if all the code is correct, but it should be (at least the principles behind it should work... i hope)
Offline Profile Quote Post Goto Top
 
mathiaus
Member Avatar
mathiaus maximus™
[ *  *  *  *  *  * ]
Ok.
Some parts of the code make a bit of sense to me (still a complete n00b!)

However I get the following error when I use it
Quote:
 
Parse error: parse error, unexpected T_FOR in /home/siriusbl/public_html/housepoints/pointstest.php on line 13


which is the line
for ($i = 1; $i<=6; $i++) {

any ideas as to whats causing the problem?

Thanks
Matt
Offline Profile Quote Post Goto Top
 
Rory
Member Avatar
i;m a mess
[ *  *  *  *  *  *  * ]
i forgot two ;'s, so that might be the problem. i edited my post with the updated version, so check that and see if it works.
Offline Profile Quote Post Goto Top
 
mathiaus
Member Avatar
mathiaus maximus™
[ *  *  *  *  *  * ]
Works wonderfully thanks :D

:rory: saves the day again!! And quickly as well!

Offline Profile Quote Post Goto Top
 
Rory
Member Avatar
i;m a mess
[ *  *  *  *  *  *  * ]
No problem. It's a good thing that i started coding php again last night, otherwise i probably would have been too rusty to work it out :P
Offline Profile Quote Post Goto Top
 
Jamie-ZNS
Member Avatar

[ *  *  *  * ]
Rory
April 1, 2005 08:30 AM
i forgot two ;'s, so that might be the problem. i edited my post with the updated version, so check that and see if it works.

Well, ;'s tend to be evil and often run away so you forget about them.
Offline Profile Quote Post Goto Top
 
Rory
Member Avatar
i;m a mess
[ *  *  *  *  *  *  * ]
indeed... down with ;'s. we should just remove all puntuation.
Offline Profile Quote Post Goto Top
 
Jamie-ZNS
Member Avatar

[ *  *  *  * ]
Rory
April 1, 2005 11:59 AM
indeed... down with ;'s. we should just remove all puntuation.

That would be so much easier Maybe a little confusing because Im not using any punctuation and its a little hard to understand >_<
Offline Profile Quote Post Goto Top
 
1 user reading this topic (1 Guest and 0 Anonymous)
« Previous Topic · Technology Chat · Next Topic »
Add Reply