Jump to content

A (not so simple) gradebook question


Gaoshan

Recommended Posts

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:
[table]
[tr][td]John[/td][td]9[/td][/tr]
[tr][td]Mary[/td][td]7[/td][/tr]
[tr][td]Ann[/td][td]8[/td][/tr]
[tr][td]John[/td][td]5[/td][/tr]
[tr][td]Mary[/td][td]4[/td][/tr]
[tr][td]Ann[/td][td]9[/td][/tr]
[tr][td]John[/td][td]6[/td][/tr]
[/table]

;D What I want:
[table]
[tr][td]John[/td][td]9[/td][td]5[/td][td]6[/td][/tr]
[tr][td]Mary[/td][td]7[/td][td]4[/td][td]-[/td][/tr]
[tr][td]Ann[/td][td]8[/td][td]9[/td][td]-[/td][/tr]
[/table]

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;

Link to comment
https://forums.phpfreaks.com/topic/22035-a-not-so-simple-gradebook-question/
Share on other sites

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

[code]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[/code]
and got:

[table]
[tr][td]Jim[/td][td]9,9.1[/td][/tr]
[tr][td]Mary[/td][td]8[/td][/tr]
[tr][td]Ann[/td][td]6[/td][/tr]
[/table]

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!
My solution to this problem was as follows:
First the sql:
[code]$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");[/code]
And now the PHP to loop through the results and put what I want where I want it:
[code]
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>';
}[/code]

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.