This repository has been archived by the owner on Feb 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
editcontainer_addexistingplants.php
121 lines (109 loc) · 3.46 KB
/
editcontainer_addexistingplants.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
<?php
////get
//Receive a list of all plants not already in the container from the database
//Render a List of Plants
//List is a Form, checkboxes next to each plant
////post
//For each plant in the form
//Add relationship
//redirect to editcontainer
?>
<?php
include("inc/sql_queries.php");
$page_title = "Container";
$page_subtitle = "Add Plants";
//Is the user submiting the form?
$post = $_SERVER['REQUEST_METHOD'] == "POST"; //Bool
function validate_submit() {
global $post;
$valid = False;
//If the method is POST
if($post) {
$valid = True;
}
return $valid;
}
if(isset($_REQUEST['ContainerID']) && !empty($_REQUEST['ContainerID'])) {
$ContainerID = $_REQUEST['ContainerID'];
}
else {
$ContainerID = NULL;
}
function echo_data($data, $item) {
/*
Echos existing data, makes sure that it exists
*/
if(isset($data[$item])) {
echo $data[$item];
}
}
function add_plant($plant, $ContainerID) {
$data["ContainerID"] = $ContainerID;
$data["PlantID"] = $plant;
$data["Quantity"] = 1;
/*
Inserts or Updates the database with the current data
Child Functions could render error messages (Unlikely)
*/
$ContainerID = create_container_plant($data);
}
//////////////////////////////////
//Template Zone, try to keep functionality out of here when possible. Limiting to simple loops and if statements
//////////////////////////////////
if($ContainerID != NULL):
$plants = select_all_plants($ContainerID);
if(!count($plants)) {
header("Location: editcontainer.php?ContainerID=".$ContainerID);
exit(0);
}
if(validate_submit()): //If Valid and Sumbited
foreach($plants as $plant) {
if(in_array($plant["PlantID"], $_REQUEST)) {
add_plant($plant["PlantID"], $ContainerID);
}
}
//Instead of displaying a success message we will just redirect them back to the container page
header("Location: editcontainer.php?ContainerID=".$ContainerID);
exit(0);
else:
include("templates/header.php"); //It is ok to move this down here because there is no form validation to worry about
//Form only rendered if validate ?>
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">Available Plants</h3>
</div>
<div class="panel-body">
<form method="POST">
<table class="table table-bordered table-hover">
<thead>
<tr>
<th class="text-center" >Add</th>
<th>Plant Name</th>
<th>Color</th>
</tr>
</thead>
<tbody>
<?php foreach($plants as $plant): ?>
<tr>
<td class="text-center" style="width: 50px;"><?php //Checkbox ?>
<input name="<?php echo $plant["PlantID"]; ?>" type="checkbox" value='<?php echo $plant["PlantID"]; ?>'>
</td>
<td><?php echo $plant["CommonName"].(empty($plant["ScientificName"]) ? "" : " (".$plant['ScientificName'].")"); ?></td>
<td><?php echo $plant["Color"]; ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<input name="ContainerID" type="hidden" value="<?php echo_data($_REQUEST, "ContainerID");?>">
<button type="submit" class="btn btn-default">Add Selected Plants</button>
</form>
</div>
</div>
<?php //End Success
include("templates/footer.php");
endif;
else:
?>
<?php
endif;
?>