-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathupdate.php
More file actions
178 lines (147 loc) · 4.87 KB
/
update.php
File metadata and controls
178 lines (147 loc) · 4.87 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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?php include('templates/header.php'); ?>
<?php
require_once "config.php";
$id = @$_GET['id'];
if( $id ) {
$id = (int) $id;
// $recette_data = array();
$sql = "SELECT * FROM RECETTE WHERE ID_RECETTE=$id";
$result = mysqli_query($conn, $sql);
$recette_data = $result->fetch_assoc();
$i_sql = "SELECT * FROM INGREDIENT";
$i_results = mysqli_query($conn, $i_sql);
$u_sql = "SELECT * FROM UNITE";
$u_results = mysqli_query($conn, $u_sql);
}
else {
header("Location: ".base_url());
exit();
}
?>
<div class="row add-receipe-container">
<div class="col-md-12">
<div class="page-heading mt-4 mb-1">
<h2>Modification de la recette</h2>
</div>
<div id="message"></div>
<br />
<form action="actions/recipe/update.php" method="POST" id="addRecipe">
<div class="form-group">
<label for="nom_recette">Nom de la recette</label>
<input type="text" class="form-control" id="nom_recette" name="nom_recette" value="<?php echo ( isset($_POST[ 'nom_recette' ]) ) ? $_POST[ 'nom_recette' ] : $recette_data[ 'NOM_RECETTE' ] ?>" />
</div>
<h4>Modifier les ingrédients</h4>
<div class="table-responsive">
<table class="table recipe-table">
<thead>
<tr>
<th>Ingredient</th>
<th>Quantité</th>
<th>Unité</th>
<th><button type="button" onclick="addRow()" class="btn btn-success"><i class="fas fa-plus"></i></button></th>
</tr>
</thead>
<tbody>
<?php
$sql = "SELECT * FROM COMPOSE WHERE ID_RECETTE=$id";
if($result = mysqli_query($conn, $sql)){
if(mysqli_num_rows($result) > 0){
// print_r($result);
$x=1;
while($row = mysqli_fetch_array($result)){ ?>
<tr data-row-id="<?php echo $x; ?>" id="row_<?php echo $x; ?>">
<td style="width: 40%">
<select class="form-control" name="select[<?php echo $x; ?>][ingredient_id]" class="select2">
<option></option>
<?php
if( $i_results ) {
foreach ( $i_results as $r ) {
$selected = ( $r[ 'ID_INGREDIENT' ] == $row[ 'ID_INGREDIENT' ] ) ? 'selected="selected"' : '';
echo "<option value='". $r[ 'ID_INGREDIENT' ] ."' $selected>".$r[ 'NOM_INGREDIENT' ]."</option>";
}
}
?>
</select>
</td>
<td style="width: 10%">
<input type="text" name="select[<?php echo $x; ?>][qty]" class="form-control" value="<?php echo $row[ 'QUANTITE' ] ?>">
</td>
<td style="width: 40%">
<select class="form-control" name="select[<?php echo $x; ?>][unit_id]">
<option></option>
<?php if( $u_results ) {
foreach ( $u_results as $u ) {
$selected = ( $u[ 'ID_UNITE' ] == $row[ 'ID_UNITE' ] ) ? 'selected="selected"' : '';
echo "<option value='". $u[ 'ID_UNITE' ] ."' $selected>".$u[ 'UNITE_MASSE' ]."</option>";
}
} ?>
</select>
</td>
<td style="width: 10%">
<button type="button" onclick="removeRow(<?php echo $x; ?>)" class="btn btn-danger"><i class="fas fa-minus"></i></button>
</td>
</tr>
<?php
$x++;
}
mysqli_free_result($result);
}
}
mysqli_close($conn);
?>
</tbody>
</table>
</div>
<input type="hidden" name="id" value="<?php echo $_GET['id']; ?>">
<button type="submit" class="btn btn-primary">Valider les modifications</button>
<a href="<?php echo base_url() . '/recette.php' ?>" class="btn btn-danger">Retour</a>
</form>
</div>
</div>
<script type="text/javascript">
var base_url = "<?php echo base_url() ?>";
$('#addRecipe').on( 'submit', function(){
var action = $(this).attr( 'action' );
var method = $(this).attr( 'method' );
$.ajax({
url: action,
type: method,
dataType: 'json',
data: $(this).serialize(),
success:function( response ) {
var pos = jQuery('.add-receipe-container').offset().top - 70;
$("html, body").animate({ scrollTop: pos }, 1000);
$("#message").html( response.message );
}
});
return false;
});
// addRow();
function addRow( id = null )
{
var count_row = $('.recipe-table tbody tr:last-child').attr( 'data-row-id' );
if( count_row >= 1 ) {
count_row = parseInt( count_row ) + 1;
}
else {
count_row = 1;
}
$.ajax({
url: base_url + '/actions/recipe/fetch_table_row.php',
type: 'get',
data: {
row_id : count_row
},
success:function( response ) {
$('.recipe-table tbody').append( response );
}
});
}
function removeRow( row_id = null )
{
if( row_id ) {
$( 'table tbody tr#row_'+row_id ).remove();
}
}
</script>
<?php include('templates/footer.php'); ?>