TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 07-14-2009, 03:21 PM   #1 (permalink)
The Wanderer
 
Join Date: Jan 2008
Posts: 6
Thanks: 0
godsdead is on a distinguished road
Angry 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.

Last edited by codefreek : 07-14-2009 at 06:56 PM. Reason: PHP tags added - please read http://www.talkphp.com/lounge/4563-prettifying-pasted-code-talkphp.html
godsdead is offline  
Reply With Quote
Old 07-14-2009, 07:16 PM   #2 (permalink)
The Addict
 
Join Date: May 2009
Posts: 287
Thanks: 5
adamdecaf is on a distinguished road
Default

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.
__________________
My Site
adamdecaf is offline  
Reply With Quote
Old 07-15-2009, 06:47 PM   #3 (permalink)
The Contributor
 
Join Date: Jun 2009
Location: Seattle, WA
Posts: 76
Thanks: 1
rguy84 is on a distinguished road
Default

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. :)
__________________
Ryan | Blog | Twitter
Send a message via AIM to rguy84 Send a message via MSN to rguy84 Send a message via Yahoo to rguy84 Send a message via Skype™ to rguy84
rguy84 is offline  
Reply With Quote
Old 07-15-2009, 08:38 PM   #4 (permalink)
The Wanderer
 
Join Date: Jan 2008
Posts: 6
Thanks: 0
godsdead is on a distinguished road
Default

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! :(
godsdead is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 11:30 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design