3 arrays combineren
Code (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
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
Dump => array(3) {
["category_id"] => array(14) {
[0] => string(1) "1"
[1] => string(1) "1"
[2] => string(1) "6"
[3] => string(1) "6"
[4] => string(1) "2"
[5] => string(1) "2"
[6] => string(1) "4"
[7] => string(1) "4"
[8] => string(1) "7"
[9] => string(1) "7"
[10] => string(1) "3"
[11] => string(1) "3"
[12] => string(1) "8"
[13] => string(1) "8"
}
["stock_id"] => array(14) {
[0] => string(1) "0"
[1] => string(1) "1"
[2] => string(1) "0"
[3] => string(1) "1"
[4] => string(1) "0"
[5] => string(1) "1"
[6] => string(1) "0"
[7] => string(1) "1"
[8] => string(1) "0"
[9] => string(1) "1"
[10] => string(1) "0"
[11] => string(1) "1"
[12] => string(1) "0"
[13] => string(1) "1"
}
["del_times_id"] => array(14) {
[0] => string(1) "1"
[1] => string(1) "2"
[2] => string(1) "1"
[3] => string(1) "1"
[4] => string(1) "1"
[5] => string(1) "1"
[6] => string(1) "1"
[7] => string(1) "1"
[8] => string(1) "1"
[9] => string(1) "1"
[10] => string(1) "1"
[11] => string(1) "1"
[12] => string(1) "1"
[13] => string(1) "1"
}
}
["category_id"] => array(14) {
[0] => string(1) "1"
[1] => string(1) "1"
[2] => string(1) "6"
[3] => string(1) "6"
[4] => string(1) "2"
[5] => string(1) "2"
[6] => string(1) "4"
[7] => string(1) "4"
[8] => string(1) "7"
[9] => string(1) "7"
[10] => string(1) "3"
[11] => string(1) "3"
[12] => string(1) "8"
[13] => string(1) "8"
}
["stock_id"] => array(14) {
[0] => string(1) "0"
[1] => string(1) "1"
[2] => string(1) "0"
[3] => string(1) "1"
[4] => string(1) "0"
[5] => string(1) "1"
[6] => string(1) "0"
[7] => string(1) "1"
[8] => string(1) "0"
[9] => string(1) "1"
[10] => string(1) "0"
[11] => string(1) "1"
[12] => string(1) "0"
[13] => string(1) "1"
}
["del_times_id"] => array(14) {
[0] => string(1) "1"
[1] => string(1) "2"
[2] => string(1) "1"
[3] => string(1) "1"
[4] => string(1) "1"
[5] => string(1) "1"
[6] => string(1) "1"
[7] => string(1) "1"
[8] => string(1) "1"
[9] => string(1) "1"
[10] => string(1) "1"
[11] => string(1) "1"
[12] => string(1) "1"
[13] => string(1) "1"
}
}
Is er een manier om het als volgt weg te schrijven:
Uiteraard zou het kunnen, maar wat is de reden dat je dit format wil hebben?
Kun je me SVP helpen, ik heb echt zoveel geprobeerd.
Code (php)
Voor de veiligheid zou ik wel even of per stuk controleren of de waarden wel getallen zijn, of quotes om de getallen zetten en escaping toepassen. Whatever floats your boat.
Gewijzigd op 25/06/2017 00:33:59 door Ben van Velzen
Ik ben je zeer dankbaar!
Kijk is hoe mooi jouw code in het script staat:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
function updateDTC($p){
$sql_del = "DELETE FROM delivery_times_category ";
$this->db->query($sql_del);
$ins_sql = "INSERT INTO delivery_times_category (del_times_id, category_id, stock_id) VALUES ";
$output = '';
foreach ($p['del_times_id'] as $k => $v) {
$output .= '(' . $this->db->escape($v) . ',' . $this->db->escape($p['category_id'][$k]) . ',' . $this->db->escape($p['stock_id'][$k]) . '),';
}
$output = substr($output, 0, -1);
$ins_sql .= $output;
$this->db->query($ins_sql);
}
$sql_del = "DELETE FROM delivery_times_category ";
$this->db->query($sql_del);
$ins_sql = "INSERT INTO delivery_times_category (del_times_id, category_id, stock_id) VALUES ";
$output = '';
foreach ($p['del_times_id'] as $k => $v) {
$output .= '(' . $this->db->escape($v) . ',' . $this->db->escape($p['category_id'][$k]) . ',' . $this->db->escape($p['stock_id'][$k]) . '),';
}
$output = substr($output, 0, -1);
$ins_sql .= $output;
$this->db->query($ins_sql);
}
Je moet alleen nog wel even wat extra quotes toevoegen zodat het resultaat vormt als ('1', '2', '3') omdat anders escaping niets uithaalt.
Code (php)
1
2
3
4
5
6
7
2
3
4
5
6
7
<input type="text" name="items[1][category_id]" value="1" />
<input type="text" name="items[1][stock_id]" value="0" />
<input type="text" name="items[1][del_times_id]" value="1" />
<input type="text" name="items[2][category_id]" value="1" />
<input type="text" name="items[2][stock_id]" value="1" />
<input type="text" name="items[2][del_times_id]" value="2" />
<input type="text" name="items[1][stock_id]" value="0" />
<input type="text" name="items[1][del_times_id]" value="1" />
<input type="text" name="items[2][category_id]" value="1" />
<input type="text" name="items[2][stock_id]" value="1" />
<input type="text" name="items[2][del_times_id]" value="2" />
<pre>
</pre>
Code (php)
Gewijzigd op 25/06/2017 12:27:04 door Frank Nietbelangrijk
Toevoeging op 25/06/2017 17:33:36:
Het formulier ziet er nu zo uit:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<tr>
<td><?php echo $value->category; ?></td>
<input type="hidden" name="category_id[]" value="<?php echo $value->category_id; ?>">
<?php $stock = ($value->stock_id == 0)?'<i class="fa fa-times"></i> no stock':'<i class="fa fa-check"></i> stock'; ?>
<td><?php echo $stock ?></td>
<input type="hidden" name="stock_id[]" value="<?php echo $value->stock_id; ?>">
<td>
<select name="del_times_id[]" class="form-control">
<option value="">-- select a category --</option>
<?php
foreach ($rules as $rule) :
$select = ($rule->del_times_id == $value->del_times_id) ? 'selected="selected"' : '';
echo '<option value="' . $rule->del_times_id . '" ' . $select . '>' . $rule->rule . '</option>';
endforeach;
?>
</select>
</td>
</tr>
<td><?php echo $value->category; ?></td>
<input type="hidden" name="category_id[]" value="<?php echo $value->category_id; ?>">
<?php $stock = ($value->stock_id == 0)?'<i class="fa fa-times"></i> no stock':'<i class="fa fa-check"></i> stock'; ?>
<td><?php echo $stock ?></td>
<input type="hidden" name="stock_id[]" value="<?php echo $value->stock_id; ?>">
<td>
<select name="del_times_id[]" class="form-control">
<option value="">-- select a category --</option>
<?php
foreach ($rules as $rule) :
$select = ($rule->del_times_id == $value->del_times_id) ? 'selected="selected"' : '';
echo '<option value="' . $rule->del_times_id . '" ' . $select . '>' . $rule->rule . '</option>';
endforeach;
?>
</select>
</td>
</tr>
doe je iets als
$i is een tellertje van 1 tot ..