Script is erg langzaam.

Overzicht Reageren

Sponsored by: Vacatures door Monsterboard

Thomas de vries

thomas de vries

03/03/2018 16:32:29
Quote Anchor link
Goedendag,

Ik heb een site gemaakt waarin mensen hun trein vloot kunnen organizeren.
Echter als een user wat wil veranderen. Dan duurt dit soms enkele seconden voordat het zichtbaar is.

het veranderen gebeurt zo

user drukt op een select en kiest wat anders.
dan word jquery uitgevoerd en die laat een andere pagina in die pagina staat php die ervoor zorgt dat het wordt uitgevoerd.
Na uitvoering laat hij de website opnieuw zien.

de scripts
fleet.php
Code (php)
PHP script in nieuw venster Selecteer het PHP script
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
<?php
include('inc/config.php');
echo $UserFleet->Notice;
?>

<style>
#fleet .rTable {
      display: table;
      width: 100%;
}
#fleet .rTableRow {
      display: table-row;
}
#fleet .rTableHeading {
      display: table-header-group;
      background-color: #ddd;
}
#fleet .rTableCell, .rTableHead {
      display: table-cell;
      padding: 3px 10px;
      border: 1px solid #999999;
}
#fleet .rTableHeading {
      display: table-header-group;
      background-color: #ddd;
      font-weight: bold;
}
#fleet .rTableFoot {
      display: table-footer-group;
      font-weight: bold;
      background-color: #ddd;
}
#fleet .rTableBody {
      display: table-row-group;
}
#AddFleet .rTable {
      display: table;
      width: 50%;
}
#AddFleet .rTableRow {
      display: table-row;
}
#AddFleet .rTableHeading {
      display: table-header-group;
      background-color: #ddd;
}
#AddFleet .rTableCell, .rTableHead {
      display: table-cell;
      padding: 3px 10px;
      border: 1px solid #999999;
    width: 100px;
}
#AddFleet .rTableHeading {
      display: table-header-group;
      background-color: #ddd;
      font-weight: bold;
}
#AddFleet .rTableFoot {
      display: table-footer-group;
      font-weight: bold;
      background-color: #ddd;
}
#AddFleet .rTableBody {
      display: table-row-group;
}
</style>
<script type="text/javascript" language="JavaScript" src="js/jquery.js"></script>
<script type="text/javascript" language="JavaScript" src="js/jquery.confirm.min.js"></script>
<script>
function sendData(nr) {
    var data = $("#myform" + nr).serialize();
    
    console.log(data);
    
    $("#myForm" + nr).submit(function(event){
        event.preventDefault();
    });
    
    jQuery.ajax({
       url: "inc/FleetData.php?a=Update&" + data,
        datatype: "html",
        type: "GET",
        success:function(data)
        {
            if(data) {
                //$("#container").load(window.location + " #container");
                $('#fleet').html(data);
                //alert(data);
            } else {
                $("#fleet").load(window.location + " #container");
                //alert("Something is wrong with the update of the collor. Please notify an admin");
                //alert(data);
            }
        }
    });
}

function OrderBy(fieldName) {
    var data = fieldName;
    
    console.log(data);
    
    $.ajax({
        url: 'inc/FleetData.php?a=OrderBy&p=' + data,
        type: 'GET',
        dataType: 'HTML',
        timeout: 1000,
        success: function(response) {
            $("#fleet").html(response);
        }
    });
}

function AddTrain() {
    document.getElementById('box10').style.display = 'block';
    $.ajax({
        url: '/inc/trainFunctions.php?a=AddTrain',
        type: 'GET',
        dataType: 'HTML',
        timeout: 1000,
        success: function(response) {
            $("#box10").html(response);
        }
    });
}

function DeleteTrain(TrainId) {
    
    document.getElementById('box10').style.display = 'block';
    $.ajax({
        url: '/inc/deleteTrain.php?a=delete&sid=' + TrainId,
        type: 'GET',
        dataType: 'HTML',
        timeout: 1000,
        success: function(response) {
            $("#box10").html(response);
        }
    });

}

function EditField(nr) {
    var data = $("#myform" + nr).serialize();
    
    console.log(data);
    
    $.ajax({
        url: 'inc/FleetData.php?a=EditField&' + data,
        type: 'GET',
        dataType: 'HTML',
        timeout: 1000,
        success: function(response) {
            $("#fleet").html(response);
        }
    });
}

function UpdateGold(TrainId) {
    var data = TrainId;
    
    console.log(data);
    $.ajax({
        url: 'inc/FleetData.php?a=UpdateGold&p=' + data,
        type: 'GET',
        dataType: 'HTML',
        timeout: 1000,
        success: function(response) {
            $("#fleet").html(response);
        }
    });
}

</script>
<div id="box10"
    style="
    padding: 10px;
    background: #fff;
    border: 3px solid #ccc;
    box-shadow: 0 0 10px #ffffff;
    z-index: 1000;
    display: none;
    position: absolute;
    bottom: ;
    right: ; margin: 0 auto;"></div>
<?php
if($_SERVER['REQUEST_METHOD'] == "POST")
{

    if(isset($_POST['save'])) {
        $UserFleet->AddTrain(
            $_POST['train_name'],
            $_POST['train_image'],
            $_POST['train_power'],
            $_POST['Type'],
            $_POST['Set'],
            $_POST['Use'],
            $_POST['train_value'],
            $_POST['train_passenger'],
            $_POST['train_mail']
        );
    }
}

?>

<div id="AddFleet">
<h3>Add Train!</h3>
<form method="post" action="">
<div class="rTable">
        <div class="rTableHeading">
            <div class="rTableRow">
                <div class="rTableCell">Engine Name:</div>
                <div class="rTableCell"><input type="text" name="train_name" /></div>
            </div>
            <div class="rTableRow">
                <div class="rTableCell">Engine Image:</div>
                <div class="rTableCell"><input type="text" name="train_image" /></div>
            </div>
            <div class="rTableRow">
                <div class="rTableCell">Engine Power:</div>
                <div class="rTableCell"><input type="text" name="train_power" /></div>
            </div>
            <div class="rTableRow">
                <div class="rTableCell">Engine Type:</div>
                <div class="rTableCell"><select name="Type"><option value="0"></option><option value="1">Steam</option><option value="2">Diesel</option><option value="3">Electric</option><option value="4">Maglev</option></select></div>
            </div>
            <div class="rTableRow">
                <div class="rTableCell">Engine Set:</div>
                <div class="rTableCell"><select name="Set"><option value="0">No</option><option value="1">Yes</option></select></div>
            </div>
            <div class="rTableRow">
                <div class="rTableCell">Engine In Use:</div>
                <div class="rTableCell"><select name="Use"><option value="0">No</option><option value="1">Yes</option></select></div>
            </div>
            <div class="rTableRow">
                <div class="rTableCell">Engine Value(Idle):</div>
                <div class="rTableCell"><input type="text" name="train_value" /></div>
            </div>
            <div class="rTableRow">
                <div class="rTableCell">Engine Passengers:</div>
                <div class="rTableCell"><input type="text" name="train_passenger" /></div>
            </div>
            <div class="rTableRow">
                <div class="rTableCell">Engine Mail:</div>
                <div class="rTableCell"><input type="text" name="train_mail" /></div>
            </div>
            <div class="rTableRow">
                <div class="rTableCell">Engine Mail:</div>
                <div class="rTableCell"><input type="submit" name="save" value="Save Engine" /></div>
            </div>
        </div>
        <?php echo $UserFleet->Notice; ?>
    </div>
    </form>
</div>
<div id="fleet">
<div class="rTable">
    <div class="rTableHeading">
        <div class="rTableRow">
            <div class="rTableCell"></div>
            <div class="rTableCell"></div>
            <div class="rTableCell"></div>
            <div class="rTableCell"></div>
            <div class="rTableCell"></div>
            <div class="rTableCell"></div>
            <div class="rTableCell">Total Trains:<?php echo $UserFleet->GetTotalFleet(); ?></div>
            <div class="rTableCell">Gold to 1:</div>
            <div class="rTableCell"></div>
            <div class="rTableCell"> </div>
            <div class="rTableCell">Gold to 8:</div>
            <div class="rTableCell">Pass. to 8</div>
            <div class="rTableCell">Mail to 8</div>
            <div class="rTableCell"> </div>
        </div>
        <div class="rTableRow">
            <div class="rTableHead">Color</div>
            <div class="rTableHead">Image</div>
            <div class="rTableHead"><a href="#" onclick="OrderBy('train_name');">Name:</a></div>
            <div class="rTableHead"><a href="#" onclick="OrderBy('train_power');">Power:</a></div>
            <div class="rTableHead"><a href="#" onclick="OrderBy('train_type');">S/D/E/M</a></div>
            <div class="rTableHead"><a href="#" onclick="OrderBy('train_set');">Set</a></div>
            <div class="rTableHead"><a href="#" onclick="OrderBy('train_use');">In use</a>(<?php echo $UserFleet->GetTotalFleetInUse(); ?>)</div>
            <div class="rTableHead"><a href="#" onclick="OrderBy('train_value');">Value(Idle)</a></div>
            <div class="rTableHead"><a href="#" onclick="OrderBy('train_passenger');">Pass.(Idle)</a></div>
            <div class="rTableHead"><a href="#" onclick="OrderBy('train_mail');">Mail(Idle)</a></div>
            <div class="rTableHead"><?php echo $Func->NumberFormat($UserFleet->GetTotalGoldPassMailTo8('total_gold')); ?></div>
            <div class="rTableHead"><?php echo $Func->NumberFormat($UserFleet->GetTotalGoldPassMailTo8('total_passenger')); ?></div>
            <div class="rTableHead"><?php echo $Func->NumberFormat($UserFleet->GetTotalGoldPassMailTo8('total_mail')); ?></div>
            <div class="rTableHead"> </div>
        </div>
        <!--Get Data from Database-->
        <?php
        $i
= 2;
        foreach($UserFleet->GetFleet() AS $row)
        {

            echo '
            <div class="rTableRow">
                <div class="rTableCell" style="background-color: #'
.$row['train_row_color'].'";>
                    <form method="post" action="" id="myform'
. ++$i . '">
                    <select class="test" name="value" onchange="sendData('
.$i.');" style="background-color: #'.$row['train_row_color'].';">
                    <option value="" style="background-color: #ffffff"'
; if($row['train_row_color'] == "") { echo 'selected'; } echo '></option>
                    <option value="ff0000" style="background-color: #ff0000"'
; if($row['train_row_color'] == "ff0000") { echo 'selected'; } echo '>Red</option>
                    <option value="3000ff" style="background-color: #3000ff"'
; if($row['train_row_color'] == "3000ff") { echo 'selected'; } echo '>Blue</option>
                    <option value="36ff00" style="background-color: #36ff00"'
; if($row['train_row_color'] == "36ff00") { echo 'selected'; } echo '>Green</option>
                    <option value="e400ff" style="background-color: #e400ff"'
; if($row['train_row_color'] == "e400ff") { echo 'selected'; } echo '>Purple</option>
                    <option value="fff000" style="background-color: #fff000"'
; if($row['train_row_color'] == "fff000") { echo 'selected'; } echo '>Yellow</option>
                    <input type="hidden" name="field" value="train_row_color" />
                    <input type="hidden" name="id" value="'
.$row['id'].'" />
                    </select>
                    </form>
                </div>
                <div class="rTableCell" style="background-color: #'
.$row['train_row_color'].'";>
                <form method="post" action="" id="myform'
. ++$i . '" onchange="sendData('.$i.');"/>
                        <input type="text" name="value" value="'
.$row['train_image'].'" style="width: 200px;">
                        <input type="hidden" name="field" value="train_image"/>
                        <input type="hidden" name="id" value="'
.$row['id'].'">
                    </form>
                <img src="'
.$row['train_image'].'" alt="" /></div>
                <div class="rTablecell" style="background-color: #'
.$row['train_row_color'].'";>
                    <form method="post" action="" id="myform'
. ++$i . '" onchange="sendData('.$i.');"/>
                        <input type="text" name="value" value="'
.$row['train_name'].'">
                        <input type="hidden" name="field" value="train_name"/>
                        <input type="hidden" name="id" value="'
.$row['id'].'">
                    </form>
                </div>
                <div class="rTableCell" style="background-color: #'
.$row['train_row_color'].'";>
                    <form method="post" action="" id="myform'
. ++$i . '" onchange="sendData('.$i.');"/>
                        <input type="text" name="value" value="'
.$row['train_power'].'" style="width: 50px;">
                        <input type="hidden" name="field" value="train_power"/>
                        <input type="hidden" name="id" value="'
.$row['id'].'">
                    </form>
                </div>
                <div class="rTableCell" style="background-color: #'
.$row['train_row_color'].'";>
                    <form method="post" action="" id="myform'
. ++$i . '">
                        <select name="value" onchange="sendData('
.$i.');">
                            <option value="1"'
;if($row['train_type'] == 1) { echo 'selected=selected'; } echo '>S</option>
                            <option value="2"'
;if($row['train_type'] == 2) { echo 'selected=selected'; } echo '>D</option>
                            <option value="3"'
;if($row['train_type'] == 3) { echo 'selected=selected'; } echo '>E</option>
                            <option value="4"'
;if($row['train_type'] == 4) { echo 'selected=selected'; } echo '>M</option>
                            <input type="hidden" name="field" value="train_type" />
                            <input type="hidden" name="id" value="'
.$row['id'].'" />
                        </select>
                    </form>
                </div>
                <div class="rTableCell" style="background-color: #'
.$row['train_row_color'].'";>
                    <form method="post" action="" id="myform'
. ++$i . '"/>
                        <select name="value"  onchange="sendData('
.$i.');">
                            <option value="0"'
;if($row['train_set'] == 0) { echo 'selected'; } echo '>0</option>
                            <option value="1"'
;if($row['train_set'] == 1) { echo 'selected'; } echo '>1</option>
                            <input type="hidden" name="field" value="train_set" />
                            <input type="hidden" name="id" value="'
.$row['id'].'" />
                        </select>
                    </form>
                </div>
                <div class="rTableCell" style="background-color: #'
.$row['train_row_color'].'";>
                    <form method="post" action="" id="myform'
. ++$i . '">
                        <select name="value" onchange="sendData('
.$i.');">
                            <option value="1"'
; if($row['train_use'] == "1") { echo 'selected'; } echo '>1</option>
                            <option value="0"'
; if($row['train_use'] == "0") { echo 'selected'; } echo '>0</option>
                            <input type="hidden" name="field" value="train_use" />
                            <input type="hidden" name="id" value="'
.$row['id'].'" />
                        </select>
                    </form>
                </div>
                <div class="rTableCell" style="background-color: #'
.$row['train_row_color'].'";>
                    <form method="post" action="" id="myform'
. ++$i . '" onchange="sendData('.$i.');"/>
                        <input type="text" name="value" value="'
.$Func->NumberFormat($row['train_value']).'" style="width: 100px;">
                        <input type="hidden" name="field" value="train_value"/>
                        <input type="hidden" name="id" value="'
.$row['id'].'">
                    </form>
                </div>
                <div class="rTableCell" style="background-color: #'
.$row['train_row_color'].'";>
                    <form method="post" action="" id="myform'
. ++$i . '" onchange="sendData('.$i.');"/>
                        <input type="text" name="value" value="'
.$Func->NumberFormat($row['train_passenger']).'" style="width: 100px;">
                        <input type="hidden" name="field" value="train_passenger"/>
                        <input type="hidden" name="id" value="'
.$row['id'].'">
                    </form>
                </div>
                <div class="rTableCell" style="background-color: #'
.$row['train_row_color'].'";>
                    <form method="post" action="" id="myform'
. ++$i . '" onchange="sendData('.$i.');"/>
                        <input type="text" name="value" value="'
.$Func->NumberFormat($row['train_mail']).'" style="width: 100px;">
                        <input type="hidden" name="field" value="train_mail"/>
                        <input type="hidden" name="id" value="'
.$row['id'].'">
                    </form>
                </div>
                <div class="rTableCell" style="background-color: #'
.$row['train_row_color'].'";>'.$Func->NumberFormat($row['train_value'] * $row['train_use'] * 2.050015).'</div>
                <div class="rTableCell" style="background-color: #'
.$row['train_row_color'].'";>'.$Func->NumberFormat($row['train_passenger'] * $row['train_use'] * 2.050015).'</div>
                <div class="rTableCell" style="background-color: #'
.$row['train_row_color'].'";>'.$Func->NumberFormat($row['train_mail'] * $row['train_use'] * 2.050015).'</div>
                <div class="rTableCell" style="background-color: #'
.$row['train_row_color'].'";>
                    <form id="myform'
.++$i.'" method="post">
                        <img src="/images/check_off.png" style="width: 20px; height:20px;"onclick="DeleteTrain('
.$row['id'].');"/>
                        
                        <input type="hidden" name="delete" value="delete"/>
                        <input type="hidden" name="id" value="'
.$row['id'].'" />
                    </form>
                </div>
            </div>'
;
        }

        ?>

        <!--End Get Data-->
    </div>
</div>
</div>


FleetData.php
Code (php)
PHP script in nieuw venster Selecteer het PHP script
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
<?php
include('config.php');
if($_GET['a'] == "Update") {
    $value = $Func->ReplaceDots($_GET['value']);
    $UserFleet->UpdateTrainRow($_GET['id'],$_GET['field'], $value);
    //echo $_GET['id'].'<br />'.$_GET['field'].'<br />'.$_GET['value'];
}
if($_GET['a'] == "OrderBy") {
    $UserFleet->FieldOrder = $_GET['p'];
    if($UserFleet->FieldOrder == $_GET['p']) {
        if($UserFleet->GetFleetFieldOrderBy($_GET['p']) == "asc") {
            $UserFleet->UpdateFleetFieldOrderBy($_GET['p'], "desc");
        }
else {
            $UserFleet->UpdateFleetFieldOrderBy($_GET['p'], "asc");
        }
    }
}


//echo $UserFleet->OrderBy;
?>

<div id="fleet">
<div class="rTable">
    <div class="rTableHeading">
        <div class="rTableRow">
            <div class="rTableCell"></div>
            <div class="rTableCell"></div>
            <div class="rTableCell"></div>
            <div class="rTableCell"></div>
            <div class="rTableCell"></div>
            <div class="rTableCell"></div>
            <div class="rTableCell">Total Trains:<?php echo $UserFleet->GetTotalFleet(); ?></div>
            <div class="rTableCell">Gold to 1:</div>
            <div class="rTableCell"></div>
            <div class="rTableCell"> </div>
            <div class="rTableCell">Gold to 8:</div>
            <div class="rTableCell">Pass. to 8</div>
            <div class="rTableCell">Mail to 8</div>
            <div class="rTableCell"> </div>
        </div>
        <div class="rTableRow">
            <div class="rTableHead">Color</div>
            <div class="rTableHead">Image</div>
            <div class="rTableHead"><a href="#" onclick="OrderBy('train_name');">Name:</a></div>
            <div class="rTableHead"><a href="#" onclick="OrderBy('train_power');">Power:</a></div>
            <div class="rTableHead"><a href="#" onclick="OrderBy('train_type');">S/D/E/M</a></div>
            <div class="rTableHead"><a href="#" onclick="OrderBy('train_set');">Set</a></div>
            <div class="rTableHead"><a href="#" onclick="OrderBy('train_use');">In use</a>(<?php echo $UserFleet->GetTotalFleetInUse(); ?>)</div>
            <div class="rTableHead"><a href="#" onclick="OrderBy('train_value');">Value(Idle)</a></div>
            <div class="rTableHead"><a href="#" onclick="OrderBy('train_passenger');">Pass.(Idle)</a></div>
            <div class="rTableHead"><a href="#" onclick="OrderBy('train_mail');">Mail(Idle)</a></div>
            <div class="rTableHead"><?php echo $Func->NumberFormat($UserFleet->GetTotalGoldPassMailTo8('total_gold')); ?></div>
            <div class="rTableHead"><?php echo $Func->NumberFormat($UserFleet->GetTotalGoldPassMailTo8('total_passenger')); ?></div>
            <div class="rTableHead"><?php echo $Func->NumberFormat($UserFleet->GetTotalGoldPassMailTo8('total_mail')); ?></div>
            <div class="rTableHead"> </div>
        </div>
        <!--Get Data from Database-->
        <?php
        $i
= 2;
        foreach($UserFleet->GetFleet() AS $row)
        {

            echo '
            <div class="rTableRow">
                <div class="rTableCell" style="background-color: #'
.$row['train_row_color'].'";>
                    <form method="post" action="" id="myform'
. ++$i . '">
                    <select class="test" name="value" onchange="sendData('
.$i.');" style="background-color: #'.$row['train_row_color'].';">
                    <option value="" style="background-color: #ffffff"'
; if($row['train_row_color'] == "") { echo 'selected'; } echo '></option>
                    <option value="ff0000" style="background-color: #ff0000"'
; if($row['train_row_color'] == "ff0000") { echo 'selected'; } echo '>Red</option>
                    <option value="3000ff" style="background-color: #3000ff"'
; if($row['train_row_color'] == "3000ff") { echo 'selected'; } echo '>Blue</option>
                    <option value="36ff00" style="background-color: #36ff00"'
; if($row['train_row_color'] == "36ff00") { echo 'selected'; } echo '>Green</option>
                    <option value="e400ff" style="background-color: #e400ff"'
; if($row['train_row_color'] == "e400ff") { echo 'selected'; } echo '>Purple</option>
                    <option value="fff000" style="background-color: #fff000"'
; if($row['train_row_color'] == "fff000") { echo 'selected'; } echo '>Yellow</option>
                    <input type="hidden" name="field" value="train_row_color" />
                    <input type="hidden" name="id" value="'
.$row['id'].'" />
                    </select>
                    </form>
                </div>
                <div class="rTableCell" style="background-color: #'
.$row['train_row_color'].'";><img src="'.$row['train_image'].'" alt="" /></div>
                <div class="rTablecell" style="background-color: #'
.$row['train_row_color'].'";>
                    <form method="post" action="" id="myform'
. ++$i . '" onchange="sendData('.$i.');"/>
                        <input type="text" name="value" value="'
.$row['train_name'].'">
                        <input type="hidden" name="field" value="train_name"/>
                        <input type="hidden" name="id" value="'
.$row['id'].'">
                    </form>
                </div>
                <div class="rTableCell" style="background-color: #'
.$row['train_row_color'].'";>
                    <form method="post" action="" id="myform'
. ++$i . '" onchange="sendData('.$i.');"/>
                        <input type="text" name="value" value="'
.$row['train_power'].'" style="width: 50px;">
                        <input type="hidden" name="field" value="train_power"/>
                        <input type="hidden" name="id" value="'
.$row['id'].'">
                    </form>
                </div>
                <div class="rTableCell" style="background-color: #'
.$row['train_row_color'].'";>
                    <form method="post" action="" id="myform'
. ++$i . '">
                        <select name="value" onchange="sendData('
.$i.');">
                            <option value="1"'
;if($row['train_type'] == 1) { echo 'selected=selected'; } echo '>S</option>
                            <option value="2"'
;if($row['train_type'] == 2) { echo 'selected=selected'; } echo '>D</option>
                            <option value="3"'
;if($row['train_type'] == 3) { echo 'selected=selected'; } echo '>E</option>
                            <option value="4"'
;if($row['train_type'] == 4) { echo 'selected=selected'; } echo '>M</option>
                            <input type="hidden" name="field" value="train_type" />
                            <input type="hidden" name="id" value="'
.$row['id'].'" />
                        </select>
                    </form>
                </div>
                <div class="rTableCell" style="background-color: #'
.$row['train_row_color'].'";>
                    <form method="post" action="" id="myform'
. ++$i . '"/>
                        <select name="value"  onchange="sendData('
.$i.');">
                            <option value="0"'
;if($row['train_set'] == 0) { echo 'selected'; } echo '>0</option>
                            <option value="1"'
;if($row['train_set'] == 1) { echo 'selected'; } echo '>1</option>
                            <input type="hidden" name="field" value="train_set" />
                            <input type="hidden" name="id" value="'
.$row['id'].'" />
                        </select>
                    </form>
                </div>
                <div class="rTableCell" style="background-color: #'
.$row['train_row_color'].'";>
                    <form method="post" action="" id="myform'
. ++$i . '">
                        <select name="value" onchange="sendData('
.$i.');">
                            <option value="1"'
; if($row['train_use'] == "1") { echo 'selected'; } echo '>1</option>
                            <option value="0"'
; if($row['train_use'] == "0") { echo 'selected'; } echo '>0</option>
                            <input type="hidden" name="field" value="train_use" />
                            <input type="hidden" name="id" value="'
.$row['id'].'" />
                        </select>
                    </form>
                </div>
                <div class="rTableCell" style="background-color: #'
.$row['train_row_color'].'";>
                    <form method="post" action="" id="myform'
. ++$i . '" onchange="sendData('.$i.');"/>
                        <input type="text" name="value" value="'
.$Func->NumberFormat($row['train_value']).'" style="width: 100px;">
                        <input type="hidden" name="field" value="train_value"/>
                        <input type="hidden" name="id" value="'
.$row['id'].'">
                    </form>
                </div>
                <div class="rTableCell" style="background-color: #'
.$row['train_row_color'].'";>
                    <form method="post" action="" id="myform'
. ++$i . '" onchange="sendData('.$i.');"/>
                        <input type="text" name="value" value="'
.$Func->NumberFormat($row['train_passenger']).'" style="width: 100px;">
                        <input type="hidden" name="field" value="train_passenger"/>
                        <input type="hidden" name="id" value="'
.$row['id'].'">
                    </form>
                </div>
                <div class="rTableCell" style="background-color: #'
.$row['train_row_color'].'";>
                    <form method="post" action="" id="myform'
. ++$i . '" onchange="sendData('.$i.');"/>
                        <input type="text" name="value" value="'
.$Func->NumberFormat($row['train_mail']).'" style="width: 100px;">
                        <input type="hidden" name="field" value="train_mail"/>
                        <input type="hidden" name="id" value="'
.$row['id'].'">
                    </form>
                </div>
                <div class="rTableCell" style="background-color: #'
.$row['train_row_color'].'";>'.$Func->NumberFormat($row['train_value'] * $row['train_use'] * 2.050015).'</div>
                <div class="rTableCell" style="background-color: #'
.$row['train_row_color'].'";>'.$Func->NumberFormat($row['train_passenger'] * $row['train_use'] * 2.050015).'</div>
                <div class="rTableCell" style="background-color: #'
.$row['train_row_color'].'";>'.$Func->NumberFormat($row['train_mail'] * $row['train_use'] * 2.050015).'</div>
                <div class="rTableCell" style="background-color: #'
.$row['train_row_color'].'";>
                    <form id="myform'
.++$i.'" method="post">
                        <img src="/images/check_off.png" style="width: 20px; height:20px;"onclick="DeleteTrain('
.$row['id'].');"/>
                        
                        <input type="hidden" name="delete" value="delete"/>
                        <input type="hidden" name="id" value="'
.$row['id'].'" />
                    </form>
                </div>
            </div>'
;
        }

        ?>

        <!--End Get Data-->
    </div>
</div>
</div>


Het gaat dan om de functie sendData

Hopelijk zien jullie iets wat ik niet zie of wat er verbeterd kan worden.

Met vriendelijke groet,

Thomas de Vries.
 
PHP hulp

PHP hulp

21/05/2019 01:42:02
Honeypot
 
- Ariën -
Beheerder

- Ariën -

03/03/2018 16:46:36
Quote Anchor link
Ik vermoed een trage query?
 
Thomas de vries

thomas de vries

03/03/2018 17:09:01
Quote Anchor link
Query is dit
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
public function UpdateTrainRow($train_id, $field, $val) {
        $query = $this->db->query('UPDATE user_fleet SET '.$field.' = "'.$val.'" WHERE id = "'.$train_id.'" AND user_id = "'.$this->User.'"');
    }


Dus hij update alleen het veld waar de waarde van bekend is.
 
- Ariën -
Beheerder

- Ariën -

03/03/2018 17:18:37
Quote Anchor link
En als je die query uitvoert in bijv. phpMyAdmin? Duurt het dan ook zo lang?
 
Thomas de vries

thomas de vries

03/03/2018 17:43:22
Quote Anchor link
Hoi nee dit duurde 1 rij bijgewerkt. (Query duurde 0,0000 seconden.)
via php zelf duurt het soms tot 5 seconden tot ik de data terug krijg.
Denk zelf dat het laden van de fleetData.php te lang duurt.
Gewijzigd op 03/03/2018 17:45:24 door thomas de vries
 
- Ariën -
Beheerder

- Ariën -

03/03/2018 17:45:40
Quote Anchor link
Dan zal er een andere vertragende actie moeten plaatsvinden in je script.
 
Jan R

Jan R

03/03/2018 18:32:52
Quote Anchor link
stel in je script eens een paar time echo's op strategische plaatsen en bekijk de waarden eens
 
Thomas van den Heuvel

Thomas van den Heuvel

03/03/2018 19:44:06
Quote Anchor link
Ergens ook een werkend voorbeeld? ~500 regels code dumpen en dan vragen "waarom is het traag" is nou niet echt handig.


Daarnaast bieden developer tabs / modi in je browsers mogelijk uitkomst, daarin zitten tijdslijnen enzo (doorgaans in de "network" tab).
 
Thomas de vries

thomas de vries

03/03/2018 21:57:24
Quote Anchor link
Ja gelijk heb je Thomas van den Heuvel. Werkend voorbeeld is te vinden op.
www.trainstationswapleague.tk

Via de dev tabs zie ik wel tijden staan van vaak boven de 50ms als er een verzoek wordt gedaan naar fleetdata.php.
ik zal eens verder kijken anders toch even opnieuw scripten en kijken of ik zoals Jan R zegt een paar time echo's kan plaatsen.
 
Thomas van den Heuvel

Thomas van den Heuvel

03/03/2018 23:20:18
Quote Anchor link
Ook zou je alle queries die uitgevoerd worden eens kunnen loggen en die dan aflopen met een EXPLAIN. Misschien is het een kwestie van een missende foreign key.

Maar we kunnen niet zien hoe je db-oplossing er uitziet, dus daar kunnen we eigenlijk niets van zeggen. Wat gebruik je onder de motorkap? MySQLi? PDO/MySQL? Iets anders?
 
Thomas de vries

thomas de vries

04/03/2018 10:05:37
Quote Anchor link
Hoi,

Ik heb het een en ander aangepast om te kijken waar het nu weg komt.
Fleet.php blijft hetzelfde.

Voor het testen heb ik fleetData.php aangepast.
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?php
include('config.php');
if($_GET['a'] == "OrderBy") {
    $UserFleet->FieldOrder = $_GET['p'];
    if($UserFleet->FieldOrder == $_GET['p']) {
        if($UserFleet->GetFleetFieldOrderBy($_GET['p']) == "asc") {
            $UserFleet->UpdateFleetFieldOrderBy($_GET['p'], "desc");
        }
else {
            $UserFleet->UpdateFleetFieldOrderBy($_GET['p'], "asc");
        }
    }
}

echo 'test';
?>


Nu gaat het super snel met het terug geven van de div. Dus nu staat er op fleet.php alleen nog maar rows met test.
Als ik dat script weer omzet naar dit

Code (php)
PHP script in nieuw venster Selecteer het PHP script
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
<?php
include('config.php');
if($_GET['a'] == "OrderBy") {
    $UserFleet->FieldOrder = $_GET['p'];
    if($UserFleet->FieldOrder == $_GET['p']) {
        if($UserFleet->GetFleetFieldOrderBy($_GET['p']) == "asc") {
            $UserFleet->UpdateFleetFieldOrderBy($_GET['p'], "desc");
        }
else {
            $UserFleet->UpdateFleetFieldOrderBy($_GET['p'], "asc");
        }
    }
}

$i = 2;
$query = $db->query('SELECT * FROM user_fleet WHERE user_id = "'.$User->User.'"');
while($row = $query->fetch_assoc())
{

echo '
<!--Get Data from Database-->
<div class="rTableRow1">
    <div class="rTableCell" style="background-color: #'
.$row['train_row_color'].'";>
        <form method="post" action="" id="myform'
. ++$i . '">
            <select class="test" name="value" onchange="sendData('
.$i.');" style="background-color: #'.$row['train_row_color'].';">
                <option value="" style="background-color: #ffffff"'
; if($row['train_row_color'] == "") { echo 'selected'; } echo '></option>
                <option value="ff0000" style="background-color: #ff0000"'
; if($row['train_row_color'] == "ff0000") { echo 'selected'; } echo '>Red</option>
                <option value="3000ff" style="background-color: #3000ff"'
; if($row['train_row_color'] == "3000ff") { echo 'selected'; } echo '>Blue</option>
                <option value="36ff00" style="background-color: #36ff00"'
; if($row['train_row_color'] == "36ff00") { echo 'selected'; } echo '>Green</option>
                <option value="e400ff" style="background-color: #e400ff"'
; if($row['train_row_color'] == "e400ff") { echo 'selected'; } echo '>Purple</option>
                <option value="fff000" style="background-color: #fff000"'
; if($row['train_row_color'] == "fff000") { echo 'selected'; } echo '>Yellow</option>
                <input type="hidden" name="field" value="train_row_color" />
                <input type="hidden" name="id" value="'
.$row['id'].'" />
            </select>
        </form>
    </div>
</div>'
;
}

?>


Duurt het weer een eeuwigheid om de data terug te geven. terwijl ik de query nu ook heb aangepast zodat die niet meer linkt naar een functie.

Het duurt gemiddeld tussen de 7 en 8 seconden voordat de div reload met de nieuwe data. Soms is het zelfs zo dat de pagina helemaal niet meer reageert.

Ik ben een beetje clueless nu.
 
- Ariën -
Beheerder

- Ariën -

04/03/2018 12:06:09
Quote Anchor link
Ik zou dan beginnen met het isoleren van je probleem. Er zal blijkbaar toch echt een functie zijn die voor vertraging lijkt te zorgen. Dus begin je functie eerst eens met een simpele query die volgens jouw niet vertraagd zou zijn. En bouw die functie steeds verder uit tot hoe het nu is, en controleer tussentijden wanneer het probleem optreedt.

Uiteindelijk moet je dan de bron van de traagheid kunnen vinden.
 
Thomas van den Heuvel

Thomas van den Heuvel

04/03/2018 15:08:10
Quote Anchor link
Hoelang duurt het uitvoeren van $query zelf?
Hoeveel resultaten heeft $query?
Hoeveel records heeft user_fleet?
Heeft de tabel user_fleet een index of foreign key op user_id? (en welke database engine gebruik je uberhaupt)
 
Aad B

Aad B

04/03/2018 15:21:40
Quote Anchor link
Thomas de vries op 03/03/2018 17:43:22:
Hoi nee dit duurde 1 rij bijgewerkt. (Query duurde 0,0000 seconden.)
via php zelf duurt het soms tot 5 seconden tot ik de data terug krijg.
Denk zelf dat het laden van de fleetData.php te lang duurt.
Zet exact dezelfde query in een kort PHP script. Wel met gedeclareerde en ingevulde variabelen voorafgaand zodat de query 100% identiek is. Duurt dat ook 0,0000 seconden of slechts een paar millisec meer?? Het kan ook zijn dat er onderweg vanuit php naar MySQL in MySQL type conversie moet plaatsvinden en doorgaans worden dan je indexen genegeerd. Dit is een veel voorkomend tuning valkuiltje.
Gewijzigd op 04/03/2018 15:23:35 door Aad B
 
Thomas de vries

thomas de vries

04/03/2018 15:35:20
Quote Anchor link
Hoi het probleem is gevonden.

vindt het wel vreemd dat hij dit doet.
in fleet.php heb ik nu dit

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
$query = $db->query('SELECT * FROM user_fleet WHERE user_id = "'.$User->User.'"');
while($row = $query->fetch_assoc())
{
    //rest van de code
}


Klik ik nu bijvoorbeeld op name om op alfabet te selecteren dan gaat die naar deze functie.

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<script>
function OrderBy(fieldName) {
    var data = fieldName;
    
    console.log(data);
    
    $.ajax({
        url: 'inc/FleetData.php?a=OrderBy&p=' + data,
        type: 'GET',
        dataType: 'HTML',
        timeout: 1000,
        success: function(response) {
            $("#fleet").html(response);
        }
    });
}
</script>


Dan gaat die naar fleetData.php
daar staat dit in
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
$query = $db->query('SELECT id,user_id,train_name FROM user_fleet WHERE user_id = "100886610003250557837"');
while($row = $query->fetch_assoc())
{
    echo '
    <div class="rTableRow">
    <div class="rTablecell" style="background-color: #";>
                    <form method="post" action="" id="myform1" onchange="sendData(1);"/>
                        <input type="text" name="value" value="'.$row['train_name'].'">
                        <input type="hidden" name="field" value="train_name"/>
                        <input type="hidden" name="id" value="'.$row['id'].'">
                    </form>
                </div></div>';
}


Op zich niks bijzonders maar zie nu dat die ongeveer 500x loopt door dezelfde query.
Nu heb ik wat dingen aangepast. waaronder hoe die de data terug geeft.
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<script>
function OrderBy(fieldName) {
    var data = fieldName;
    console.log(data);
    
    $.ajax({
        type: 'GET',
        url: 'inc/FleetData.php?a=OrderBy&p=' + data,
        success: function(data) {
            $("#fleet").load(window.location + "#fleet"); <!--Deze is aangepast-->
        }
    });
}
</script>


fleetData.php
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
include('config.php');

if(isset($_GET['a']) == "OrderBy") {
    $UserFleet->FieldOrder = $_GET['p'];
    if($UserFleet->FieldOrder == $_GET['p']) {
        if($UserFleet->GetFleetFieldOrderBy($_GET['p']) == "asc") {
            $UserFleet->UpdateFleetFieldOrderBy($_GET['p'], "desc");
        }
else {
            $UserFleet->UpdateFleetFieldOrderBy($_GET['p'], "asc");
        }
    }
}


?>


Nu werkt het wel en gaat het ook sneller. op een paar ms na
 



Overzicht Reageren

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.