-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsurvey.php
More file actions
71 lines (62 loc) · 1.78 KB
/
survey.php
File metadata and controls
71 lines (62 loc) · 1.78 KB
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
<?php
require_once "config.php";
require_once "common.php";
if (!isset($_REQUEST['id'])) {
header("Location: index.php");
exit();
}
$mysqli = dbconnect();
$id = $_REQUEST['id'];
$q = $mysqli->query("select name, amount from budgets where id = $id");
$r = $q->fetch_assoc();
$bname = $r['name'];
$bamount = $r['amount'];
$q->free_result();
myheader($bname);
$uid = $_SESSION['user'];
$q = $mysqli->query("select id, name, mincost, maxcost from items where budget = $id");
for ($i = 0; $i < $q->num_rows; $i++) {
$q->data_seek($i);
$r = $q->fetch_assoc();
$iid = $r['id'];
$names[$iid] = $r['name'];
$mincosts[$iid] = $r['mincost'];
$maxcosts[$iid] = $r['mincost'];
}
$q->free_result();
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if (isset($_REQUEST['amount']))
$amounts = $_REQUEST['amount'];
else
$amounts = array();
$tot = 0;
foreach ($amounts as $amount)
$tot += $amount;
if ($tot > $bamount)
$error = "<b>Total cannot exceed $bamount.<br/>";
else {
foreach ($names as $iid => $name) {
$mysqli->query("delete from selections where user = $uid and item = $iid");
if (isset($amounts[$iid]))
$mysqli->query("insert into selections (user, item, amount) values ($uid, $iid, $amounts[$iid])");
}
echo "<p><em>Fine choice!</em></p>\n";
}
}
if (isset($error))
echo $error;
?>
<form id="survey" action="survey.php" method="post">
<input type="hidden" name="id" value="<?php echo $id;?>"/>
<?php
foreach ($names as $iid => $name) {
echo '<input type="checkbox" name="amount['.$iid.']" value="'.$mincosts[$iid].'">'.$name.'</input>';
echo ' ($'.$mincosts[$iid].')';
// TODO: open in a new window
echo ' | <a href="comments.php?id='.$iid.'">view comments</a>';
echo "<br>\n";
}
?>
<input type="submit"/>
</form>
<?php myfooter()?>