Jump to content


Photo

A (not so simple) gradebook question


  • Please log in to reply
7 replies to this topic

#1 Gaoshan

Gaoshan
  • Members
  • PipPip
  • Member
  • 25 posts

Posted 25 September 2006 - 10:05 PM

Hi all, my problem is this:
I want to be able to list students and then list their grades for each assignment after them (in a traditional gradebook pattern).
I only want the students listed once and some students will not yet have been graded on all assignments. Also, not all assignments will have been created. I suppose I could do the work in PHP but it seems this MUST be doable in SQL. Any help is greatly appreciated (I'm a teacher, BTW)

I can get a list that restricts the students by student id and lists their first assignment but my problem is getting second, third, etc. assignments to go after that (it is easy to get them to appear UNDER that but then it lists the student's name again). I would assume the main issue here is how to extract the different assignment numbers from the single column that they share.  ???

>:( What I get:








John9
Mary7
Ann8
John5
Mary4
Ann9
John6


;D What I want:




John956
Mary74-
Ann89-


Here is an overview of the database as it stands now:

-- Table structure for table `assignmentdata`
--

TABLE `assignmentdata` (
  `id` smallint(6) NOT NULL auto_increment,
  `assignmentnum` tinyint(20) NOT NULL,
  `assignmentname` varchar(100) NOT NULL,
  `maxgradepoints` tinyint(100) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

-- --------------------------------------------------------

--
-- Table structure for table `grades`
--

TABLE `grades` (
  `id` smallint(6) NOT NULL auto_increment,
  `studentid` smallint(6) NOT NULL,
  `assignmentid` smallint(6) NOT NULL,
  `earned` decimal(3,1) NOT NULL,
  `comment` text NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 ;

-- --------------------------------------------------------

--
-- Table structure for table `studentdata`
--

TABLE `studentdata` (
  `id` smallint(6) NOT NULL auto_increment,
  `studentid` smallint(6) NOT NULL,
  `firstname` varchar(30) NOT NULL,
  `lastname` varchar(30) NOT NULL,
  `attendance` varchar(10) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;



#2 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 25 September 2006 - 10:33 PM

Well, depending on the output, you could probably cheat with a GROUP_CONCAT().
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#3 Gaoshan

Gaoshan
  • Members
  • PipPip
  • Member
  • 25 posts

Posted 26 September 2006 - 01:21 AM

OK. I was not familiar with GROUP_CONCAT. I used the following query:

SELECT s.firstname, GROUP_CONCAT( g.earned ) AS grouped
FROM studentdata s, grades g, assignmentdata a
WHERE g.assignmentid = a.assignmentnum
AND s.studentid = g.studentid
GROUP BY s.studentid
and got:





Jim9,9.1
Mary8
Ann6


Which is better than I had been able to get before. I'll play with that and see if it solves the problem. Thanks for the tip!

#4 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 26 September 2006 - 02:23 AM

You can have an ORDER BY in your GROUP_CONCAT() as well, and choose the separator.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#5 Gaoshan

Gaoshan
  • Members
  • PipPip
  • Member
  • 25 posts

Posted 26 September 2006 - 02:26 AM

Yes. I figured I could use the default comma separator and then use explode in PHP to get the various values. I just wish I knew how to do everything with one SQL statement here rather than resort to doing part of it in PHP.

#6 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 26 September 2006 - 02:33 AM

Well, you can use a space, and an IFNULL() statement to replace NULLs with '-' as desired.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#7 Gaoshan

Gaoshan
  • Members
  • PipPip
  • Member
  • 25 posts

Posted 26 September 2006 - 02:35 AM

True. I'll see what works best with my setup and give that a try.

#8 Gaoshan

Gaoshan
  • Members
  • PipPip
  • Member
  • 25 posts

Posted 28 September 2006 - 04:59 AM

My solution to this problem was as follows:
First the sql:
$mysqldb->query("SELECT s.firstname, COUNT(DISTINCT g.assignmentid) AS totalstu, GROUP_CONCAT( g.earned ) AS grouped
FROM studentdata s, grades g, assignmentdata a
WHERE g.assignmentid = a.assignmentnum
AND s.studentid = g.studentid
GROUP BY s.studentid");
And now the PHP to loop through the results and put what I want where I want it:
while ($row = $mysqldb->fetchObject()) {
			echo '<tr><td>' . $row->firstname . '</td>';
			for ($i = 0; $i < $row->totalstu; $i++){
				$bustedout = explode(',', $row->grouped);
				echo '<td>' . $bustedout[$i] . '</td>';
			}
			echo '</tr>';		
	}

Thanks to fenway for pointing out the piece of the puzzle that I was missing... GROUP_CONCAT




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users