TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   PHP Group Array "teams" and add up points (http://www.talkphp.com/general/4733-php-group-array-teams-add-up-points.html)

godsdead 07-14-2009 03:21 PM

PHP Group Array "teams" and add up points
 
hey all :-)
Now i have been trying to figure this our for ages, and its just not coming to me :/

Heres the scenario, i am creating a flatfile points system for a school.
all information is read from an excel file, points are generated from an algorithm i created and the points per student are outputted, there is an output generated below:
i cant figure out how to just display the team and the teams overall points.
Quote:

Name ID Attendance Milestone Actual Assessment R1 R2 R3 Team Points
Student1 1 90 4a 4a 1 0 0 Blue Team 16
Student2 2 75 5b 5c 2 1 1 Red Team 14
Student3 3 95 5c 3b 0 0 0 Blue Team 15
Student4 4 100 5a 6b 1 2 1 Blue Team 40
Student5 5 100 4b 4c 0 0 1 Green Team 30
Student6 6 65 6c 6a 0 1 0 Red Team 17
Student7 7 96 4a 5a 1 0 1 Green Team 26
Student8 8 98 3b 3a 1 2 2 Green Team 45
So i want to output something simple like this:
Quote:

Green Team Overall Points: 150
Red Team Overall Points: 127
Blue Team Overall Points: 89
the furthest i got, was giving each student an array with points and team name..
PHP Code:

// This skips the headings
     
$i 2;
// Create an array
     
$studentinfo = array();
     while (
$i <= $rowsc)
     {
// Student
         
$no $i++;
// Students ID
         
$id $data->val($no,2);
// The Team they belong too
         
$team $data->val($no,9);
// There overall points, which is grabbed from elsewhere
         
$r3 "$overalladded";
// Creating the array..
         
$studentinfo[$id] = array("$team","$r3");
     }
// I tried something with this, but didn't get very far
     //$unique_teams_array = array_unique($studentinfo);
     //ksort($studentinfo);
     // Output
     
echo"<pre>";
         
print_r($studentinfo);
     echo
"</pre>"

which outputted:
PHP Code:

Array
(
     [
1] => Array
         (
             [
0] => Blue Team
             
[1] => 0
         
)

     [
2] => Array
         (
             [
0] => Red Team
             
[1] => 1
         
)

     [
3] => Array
         (
             [
0] => Blue Team
             
[1] => 0
         
)

     [
4] => Array
         (
             [
0] => Blue Team
             
[1] => 1
         
)

     [
5] => Array
         (
             [
0] => Green Team
             
[1] => 1
         
)

     [
6] => Array
         (
             [
0] => Red Team
             
[1] => 0
         
)

     [
7] => Array
         (
             [
0] => Green Team
             
[1] => 1
         
)

     [
8] => Array
         (
             [
0] => Green Team
             
[1] => 2
         
)


side notes:
i am using http://code.google.com/p/php-excel-reader/ to read the excel file.

adamdecaf 07-14-2009 07:16 PM

Nice job so far, you could output the data like so:

PHP Code:

<?php
// rem: $studentinfo

// Set the teams
   
$green;
   
$red;
   
$blue;

   foreach (
$studentinfo as $student) {
      switch(
strtolower($student[0])) {
         case 
'green team':
             
$green += $student[1];
         break;

         case 
'blue':
             
$blue += $student[1];
         break;

         case 
'red':
             
$red += $student[1];
         break;

         default:
             
// Nothing, or you can add points to an extra team.
         
break;
      } 
   }

?>

This code would require that the totals in $student[1] were correct (It looks like your projected output is a "correct" total, mine is not). To fix it you would need to tally up the totals (from the excel sheet) before you would add them to the final score.

rguy84 07-15-2009 06:47 PM

Just a usability tip, I'd go one step further and spit that stuff out in a table vs a pre after you get the data sorted. :)

godsdead 07-15-2009 08:38 PM

Cheers :)
im only using pre for development, i actulary have a very nice table UI that i will be using ;p

I think the problem with that would be that we dont know the exact name of each and every team, so i dont want to hardcore teamnames in.. so there could be unlimated teams with unlimated students in each.. i understand the theory what i need to do, i just cant put the code into practise! :(


All times are GMT. The time now is 05:44 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0