Skip to content

Instantly share code, notes, and snippets.

@nitrix
Created November 28, 2012 22:43
Show Gist options
  • Save nitrix/4165278 to your computer and use it in GitHub Desktop.
Save nitrix/4165278 to your computer and use it in GitHub Desktop.
Dynamic table generator for Zack
<?php
//============= PARAMS ==============
$teacher = 'EMP000061'; //test teacher, can also be empty
$teacher = '';
$student = 'STU000545'; //test student, CANNOT be empty
//============= SQL QUERY ================
$query = '
SELECT
ra.assessment,
ra.studentID,
r.name AS r_name,
s.name AS s_name,
s.teacher,
s.habit AS s_habit
FROM
rubrics_assessments AS ra
LEFT JOIN
skills AS s ON ra.skillID = s.id
LEFT JOIN
rubrics AS r ON ra.rubricID = r.id
WHERE
ra.studentID = "'.$student.'"';
if (!empty($teacher)) {
$query .= ' AND s.teacher = "'.$teacher.'"';
}
$query .= ' AND r.deleted = 0 ORDER BY s.habit ASC';
mysql_connect('54.234.53.161','root','alloprofsp0');
mysql_select_db('zack_test');
$results = mysql_query($query) or die(mysql_error());
//=============== CRAZY TABLE GENERATOR =========
$table = array();
//Generate the table
while($d = mysql_fetch_assoc($results)) {
$table[$d['s_name']][$d['r_name']] = $d['assessment'];
$all_projects[$d['r_name']] = true;
$all_groups[$d['s_name']] = $d['s_habit'];
}
//Fill in the gaps using our `all_projects` array
foreach($table as $skill_name => $projects) {
foreach($all_projects as $project_name => $project_value) {
if (empty($table[$skill_name][$project_name])) {
$table[$skill_name][$project_name] = 'N/A';
}
}
}
//Draw the table's content only (we dont care about the names)
$lastGroupRead = '';
foreach($table as $skill_name => $projects) {
//Little HACK for groups/tags whatever
if ($all_groups[$skill_name] != $lastGroupRead) {
if ($lastGroupRead != '') echo '</table>';
$lastGroupRead = $all_groups[$skill_name];
echo '<h3>'.$lastGroupRead.'</h3>';
drawTableStructure();
}
echo '<tr>';
echo '<td>'.$skill_name.'</td>';
ksort($projects);
foreach($projects as $project_name => $grade) {
echo '<td>'.$grade.'</td>';
}
echo '</tr>';
}
echo '</table>';
//============================= FUNCTIONS ==================================
function drawTableStructure()
{
global $table;
echo '<table border="1">';
echo '<tr> <th>Corner</th>';
foreach($table as $skill_name => $projects) {
ksort($projects);
foreach($projects as $project_name => $grade) {
echo '<th>'.$project_name.'</th>';
}
break;
}
echo '</tr>';
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment