-
Notifications
You must be signed in to change notification settings - Fork 0
/
levels.php
executable file
·164 lines (139 loc) · 4.09 KB
/
levels.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
include 'header.php';
?>
<?php
$table = basename(__FILE__);
$table = substr($table, 0, strpos($table, '.php'));
if($_POST['updatebutton']){
// print_r($_POST);
if(($program_id = $_POST['ID']) && ($amounts = $_POST['level_amount'])){
$level_ids = array();
foreach($amounts as $amount){
$level_id = doquery("SELECT ID FROM levels WHERE level_amount = $amount;");
$level_id = $level_id[0]->ID;
if(!$level_id){
doquery("INSERT INTO levels VALUES(NULL, $amount, '');");
$level_id = mysql_insert_id();
}
$level_ids[] = $level_id;
}
foreach($level_ids as $level_id){
$query = "INSERT INTO level_relationships VALUES(NULL, $program_id, $level_id);";
if(mysql_query($query)===false){
//echo "<div>error: $query (" . implode(',',$_POST).")</div>";
// break;
}
}
$query = "DELETE FROM level_relationships WHERE program_id = $program_id && level_id NOT IN (".implode(',', $level_ids).");";
if(mysql_query($query)===false){
echo "<div>error: $query</div>";
}
echo "<div class='message'>Levels updated.</div>";
}else{
echo "<div>Error: not all fields were filled out.</div>\n";
}
}
if($ID = $_GET['ID']){
$query="SELECT p.ID, p.program_name, l.level_amount FROM programs as p LEFT JOIN level_relationships as lr ON (p.ID = lr.program_id) LEFT JOIN levels AS l ON (lr.level_id = l.ID) WHERE p.ID = $ID ORDER BY p.ID, l.level_amount;";
}else{
$query="SELECT p.ID, p.program_name, l.level_amount FROM programs as p LEFT JOIN level_relationships as lr ON (p.ID = lr.program_id) LEFT JOIN levels AS l ON (lr.level_id = l.ID) ORDER BY p.ID, l.level_amount;";
}
$results = doquery($query);
//print_r($results);
?>
<script type='text/javascript'>
function deleteSelected(){
$("#level_amount option:selected").remove();
}
function addLevelAmount(amount){
amount = parseInt(amount);
if(!isNaN(amount)){
var n = $("#level_amount option").length;
if(n > 0){
var curval = 0;
for(var i = 0; i< n; ++i){
curval = parseInt($("#level_amount option:eq("+i+")").val());
if(curval == amount){
return;
}else if(curval > amount){
$("#level_amount option:eq("+i+")").before("<option value='" + amount + "' >"+amount+"</option>");
break;
}
}
}
if(n == $("#level_amount option").length){
$('#level_amount').append("<option value='" + amount + "' >"+amount+"</option>");
}
}
}
</script>
<h2>Manage Levels</h2>
<?php
if($ID){
?>
<form method='post' action='' onsubmit="$('#level_amount option').attr('selected', 'selected'); return true;" />
<?php } ?>
<table>
<tr>
<th></th>
<th>Program name</th>
<th>Levels </th>
</tr>
<?php
for($i = 0; $i < sizeof($results); ++$i){
$item = $results[$i];
echo "<tr>";
echo "<td><input type='hidden' name='ID' value=\"$item->ID\"/>";
if(!$ID){
echo "<a href='?ID=$item->ID' style='font-size: 14px; background-color: #DDD;padding: 3px;' >Edit</a>";
}
echo " </td>";
echo "<td>$item->program_name</td>";
echo "<td style='border-top: 20px solid white'>";
$curId = $item->ID;
if($ID){
?>
<select multiple='multiple' style='float:left;width: 200px;margin-right: 5px;' id='level_amount' name='level_amount[]' size='5'>
<?php
if($item->level_amount){
$j = $i;
do {
$item = $results[$j];
echo "<option value='$item->level_amount'>$item->level_amount</option>\n";
++$j;
}while( ($j < sizeof($results)) && ($curId == $results[$j]->ID) );
$i = $j -1;
}
?>
</select>
<input type='text' style='width: 80px;' /> <input onclick="if($(this).prev().val()){addLevelAmount($(this).prev().val());};" type='button' value='Add amount'/>
<br /> <br />
<input onclick="deleteSelected();" type='button' value='Delete Selected'/>
<? }else{
echo "<ul>";
if($item->level_amount){
$j = $i;
do {
$item = $results[$j];
echo "<li> $item->level_amount</li>\n";
++$j;
}while( ($j < sizeof($results)) && ($curId == $results[$j]->ID) );
$i = $j -1;
}
echo "</ul>";
}?>
</td>
</tr>
<?php } ?>
</table>
<?php
if($ID){
?>
<input type='submit' name='updatebutton' value='Save' />
</form>
<?php } ?>
<br /> <br />
<a href='index.php' >Go back to main </a>
<?php
include 'footer.php';
?>