/
SectionI.php
589 lines (511 loc) · 16.5 KB
/
SectionI.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
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
<?php
// Setting up some variables...
$QName = "Section1";
include "./pdo.php";
require_once('config.php');
$stylesheet = "survey.css";
$title = $meta["title"];
$header = $meta["header"];
set_time_limit(60);
?>
<?php
//**************************
// Survey Transcription Page
// Patrick S Chen 2014
//**********************************
//*********Figure out what Section we're on and get Section variables from Config
{
$SectionNo = 1;
if (isset($_POST[hSectionNo]))
{
$SectionNo = $_POST['hSectionNo'];
//print "HIYA";
}
if ($SectionNo != 'X')
{
$QName = "Section".$SectionNo;
$NextQname = "Section".($SectionNo+1);
//echo $QName.'<br><br>';
$myarray = $GLOBALS[$QName];
$QNameStr = $settings[$QName."sql"];
//echo $QNameStr;
$QNameCols = $settings[$QName."cols"];
}
//*******************
}
//******DB Init: Connect to Database and create section table if it doesn't exist
{
try {
/*** connect to SQLite database ***/
//$dbh = new PDO("sqlite:database.sdb");
PDO_Connect("sqlite:database.sdb");
}
catch(PDOException $e)
{
echo $e->getMessage();
}
$exists = "SELECT name FROM sqlite_master WHERE type='table' AND name='".$QName."'";
$name = PDO_FetchOne($exists);
//echo "name: ".$name."<br>".$exists."<br>";
//echo $QNameStr;
if ($name == '' && $SectionNo != 'X')
{
$create = "Create table ".$QName." (".$QNameStr.")";
PDO_Execute($create);
//print $create;
}
//*************Delete data from table that doesn't conform to ids table
$sql = "Delete from ".$QName." where Id not in (Select id from ids)";
PDO_Execute($sql);
//echo $sql;
}
//********* End DB initialization
if ($SectionNo != 'X') //X means we're outputting csv
{
//***Figure out what ID we're on
{
$id = 0;
IF (isset($_POST['id']))
{
$id = $_POST['id'];
//echo "isset".$id;
}
else
{
//$sql ="Select Min(id) from Ids where id > ".$id;
$sql ="Select Max(id) from ".$QName;
//change on 4/12/15 to go to first unentered id
//echo $sql;
$id = PDO_FetchOne($sql);
//echo ' id: '.$id;
if ($id == '')
{
$id = 0;
}
//echo ' 2ndid: '.$id;
$sql = "Select Min(id) from Ids where id > ".$id;
$id = PDO_FetchOne($sql);
//echo ' lastid '.$id;
}
}
//*** End get initial ID
//*********Posted Data Validation
{
$colnames=array($QNameCols);
$icount = 0;
$list = $QNameCols.',';
while (strpos($list,","))
{
$value = substr($list,0,strpos($list,","));
$colnames[$icount] = $value;
$icount++;
$list = substr($list,strpos($list,",")+1);
}
$boolpassed = 1;
if (isset($_POST['Navid']))
{
//We're navigating, reset the ID and get the columns!
$id = $_POST['Navid'];
}
else
if (isset($_POST['Entry']))
{
//**** We're submitting data, let's Validate it!
$formError =0;
//$colnames=array($QNameCols);
//echo "Col".$colnames[2];
//Build fields!
for($i=0; $i<count($myarray); $i++)
{
$_POST[$colnames[$i+1]] = str_replace("'","",$_POST[$colnames[$i+1]]);
$htmlf = $htmlf."<tr><td align='left' colspan ='2'>".$myarray[$i][4]."</td></tr>";
$htmlf = $htmlf."<tr><td align='left'><span class='title'>".$myarray[$i][0]."</span></td>";
//echo $myarray[$i][1];
//echo $colnames[$i+1];
if ($myarray[$i][1]==1)
{
//echo $colnames[$i+1];
if (!isset($_POST[$colnames[$i+1]]))
{
//This is required right?
$formError ++;
$htmlf = $htmlf."<td align='left'><input type='text' name='".$colnames[$i+1]."'><span style='color:red'>* required</span></td></tr>";
//echo $htmlf;
}
else
{
//echo $myarray[$i][2];
switch($myarray[$i][2])
{
case('#'):
if (!is_numeric($_POST[$colnames[$i+1]]))
{
//echo $_POST[$colnames[$i+1]];
//failed number test
$formError ++;
$htmlf = $htmlf."<td align='left'><input type='text' value='".$_POST[$colnames[$i+1]]."' name='".$colnames[$i+1]."'><span style='color:red'>* Must be numeric</span></td></tr>";
}
else
{
//passed number test
$htmlf = $htmlf."<td align='left'><input type='text' value='".$_POST[$colnames[$i+1]]."' name='".$colnames[$i+1]."'></td></tr>";
}
break;
case('D'):
//Create a date field
$htmlf = $htmlf."<td align='left'><input type='DATE' value='".$_POST[$colnames[$i+1]]."' name='".$colnames[$i+1]."'></td></tr>";
$_POST[$colnames[$i+1]] = "'".$_POST[$colnames[$i+1]]."'";
break;
case('T'):
//Create a time field
$htmlf = $htmlf."<td align='left'><input type='time' value='".$_POST[$colnames[$i+1]]."' name='".$colnames[$i+1]."'></td></tr>";
$_POST[$colnames[$i+1]] = "'".$_POST[$colnames[$i+1]]."'";
break;
default:
//Create a text field
$htmlf = $htmlf."<td align='left'><textarea name='".$colnames[$i+1]."'>".$_POST[$colnames[$i+1]]."</textarea></td></tr>";
//$htmlf = $htmlf."<td align='left'><input type='text' value='".$_POST[$colnames[$i+1]]."' name='".$colnames[$i+1]."'></td></tr>";
$_POST[$colnames[$i+1]] = "'".$_POST[$colnames[$i+1]]."'";
//echo "<br>".$_POST[$colnames[$i+1]];
}
}
}
else
{
$htmlf = $htmlf."<td align='left'><select name='".$colnames[$i+1]."' width='100%'>";
$options = $myarray[$i][2];
while (strpos($options,";")>0)
{
$option = substr($options,0,strpos($options,";"));
$options = substr($options,strpos($options,";")+1);
if (substr($option,0,strpos($option,"|")) == $_POST[$colnames[$i+1]])
{
$htmlf = $htmlf."<option value=".substr($option,0,strpos($option,"|"))." selected>".substr($option,strpos($option,"|")+1)."</option>";
}
else
{
$htmlf = $htmlf."<option value=".substr($option,0,strpos($option,"|")).">".substr($option,strpos($option,"|")+1)."</option>";
}
}
$htmlf = $htmlf."</select></td></tr>";
$htmlf = $htmlf."<input type='hidden' value='".count($myarray)."' name='hCount'>";
//$html = $html."<input type='hidden' value='".$id."' name='id'>";
$htmlf = $htmlf."<input type='hidden' value='".($SectionNo)."' name='hSectionNo'>".$village;
}
//echo $formError;
} //for loop
}
//************End of Processing!
}
//************** Find Sections' cols and posted values
{
//$dbh->exec("Delete from ".$QName);
//$list = substr($list,0,strlen($list)-1);
$list = $QNameCols.',';
//print $list."<br>";
$values = "";
$icount = 0;
//$QNameCols = str_replace(",","','",$QNameCols);
//print $QNameCols;
$colnames=array($QNameCols);
while (strpos($list,","))
{
$value = substr($list,0,strpos($list,","));
$colnames[$icount] = $value;
//print "-".$value."-".$_POST[$value];
$values = $values.$_POST[$value].",";
//$colnames[$icount] = $value;
$icount++;
$list = substr($list,strpos($list,",")+1);
}
$values = substr($values,0,strlen($values)-1);
}
//******* End retrieve Section col and posted values
//*********** If Validation Passes, Push data into db
if (isset($_POST['Entry']))
{
if ($formError ==0 )
{
//print $values."-hiyo";
//echo "post".$_POST[$id]."<br>";
//**************Are we trying to push data? if so, let's do it!
if ($_POST['id']!='')
{
$sql = "Delete from ".$QName." Where Id = ".$id;
PDO_Execute($sql);
$sql = "INSERT INTO ".$QName." (".$QNameCols.") Values (".$id.$values.")";
//echo $sql;
PDO_Execute($sql);
$sql ="Select Min(id) from Ids where id > ".$id;
//echo $sql;
$id = PDO_FetchOne($sql);
//print "post_Insert: ".$id;
if ($id =='')
{
$sql ="Select Min(id) from Ids";
//echo $sql;
$id = PDO_FetchOne($sql);
}
//print $sql;
}
//******* End of push data attempt
} //($formError>0)
}
//************* End of data retrieve and push
//Let's build our page!
//*************Get IDs combobox
{
//$idlist =
$result = PDO_FetchAll("SELECT * FROM Ids");
foreach($result as $row)
{
if ($id == $row['Id'])
{
$idlist = $idlist."<option value='".$row['Id']."' selected>".$row['Id']."</option>";
}
else
{
$idlist = $idlist."<option value='".$row['Id']."'>".$row['Id']."</option>";
}
}
$navList = "<select name='Navid'>".$idlist."</select>";
$idlist = "<select name='id'>".$idlist."</select>";
}
//***********End of Get IDs combobox section
//************Let's print all current records to a grid
{
$current= "<table cellpadding='1' border=1>";
$current=$current."<tr><td>Id</td>";
for($i=1; $i<count($colnames); $i++)
{
$current=$current."<td>".$colnames[$i]."</td>";
}
$current."</tr>";
//$result = $dbh->query('SELECT * FROM IDs');
//print 'building';
$result = PDO_FetchAll("SELECT * FROM ".$QName);
//echo "SELECT * FROM ".$QName;
foreach($result as $row)
{
if ($id == $row['Id'])
{
$current = $current."<tr style='background-color:#00ff00;'><td>".$row['Id']."</td>";
}
else
{
$current = $current."<tr style='background-color:#ffffff;'><td>".$row['Id']."</td>";
}
for($i=1; $i<count($colnames); $i++)
{
if ($myarray[$i-1][1]==2)
{
$options = $myarray[$i-1][2];
//echo $colnames[$i];
//echo $options;
while (strpos($options,";")>0)
{
$option = substr($options,0,strpos($options,";"));
$options = substr($options,strpos($options,";")+1);
//echo $row[$colnames[$i]];
//echo substr($option,0,strpos($option,"|")).' - '.$row[$colnames[$i]].'; ';
if (substr($option,0,strpos($option,"|")) == $row[$colnames[$i]])
{
//echo substr($option,strpos($option,"|")+1);
$current=$current."<td>".substr($option,strpos($option,"|")+1)."</td>";
}
}
}
else
{
$current=$current."<td>".$row[$colnames[$i]]."</td>";
}
}
$current = $current."</tr>";
}
$current = $current."</table>";
}
//******* End of print records to grid
//**************Let's create the Entry form from the previously discovered cols!
{
//print "colnames: ".$colnames[1];
//Get Questions and create form controls
if (isset($settings[$QName]) && $settings[$QName] == true)
{
//Get values
//echo "SELECT * FROM ".$QName." Where Id = ".$id;
$result = PDO_FetchAll("SELECT * FROM ".$QName." Where Id = ".$id);
//echo print_r($result);
//print 'section';
$html = "<form action='SectionI.php' method='post'><input type='hidden' value='1' name='Entry'>";
$html = $html."<table cellpadding='5' width='90%' align='center'><tr><td align='left' colspan='2'><span class='large_title'>".$QName."</span></td></tr>";
$html = $html."<tr><td align='left'><span class='title'>Ids</span></td><td align='left'>".$idlist."</td></tr>";
if ($formError == 0)
{
for($i=0; $i<count($myarray); $i++)
{
//Build fields!
//echo $result[0][$colnames[$i+1]];
$html = $html."<tr><td align='left' colspan='2'>".$myarray[$i][4]."</td></tr>";
$html = $html."<tr><td align='left'><span class='title'>".$myarray[$i][0]."</span></td>";
$fvalue = $result[0][$colnames[$i+1]];
if ($fvalue=='')
{
$fvalue = $myarray[$i][3];
}
if ($myarray[$i][1]==1)
{
//Create Date Field
switch ($myarray[$i][2])
{
case('D'):
//Create date field
$html = $html."<td align='left'><input type='date' value='".$fvalue."' name='".$colnames[$i+1]."'></td></tr>";
break;
case('A'):
//Create Text field
$html = $html."<td align='left'><textarea name='".$colnames[$i+1]."'>".$fvalue."</textarea></td></tr>";
break;
case('T'):
//Create Time field
$html = $html."<td align='left'><input type='time' value='".$fvalue."' name='".$colnames[$i+1]."'></td></tr>";
break;
case('#'):
//Create Number field
$html = $html."<td align='left'><input type='text' value='".$fvalue."' name='".$colnames[$i+1]."'></td></tr>";
}
}
else
{
$html = $html."<td align='left'><select name='".$colnames[$i+1]."' width='100%'>";
$options = $myarray[$i][2];
while (strpos($options,";")>0)
{
$option = substr($options,0,strpos($options,";"));
$options = substr($options,strpos($options,";")+1);
if (substr($option,0,strpos($option,"|")) == $fvalue)
{
$html = $html."<option value=".substr($option,0,strpos($option,"|"))." selected>".substr($option,strpos($option,"|")+
1)."</option>";
}
else
{
$html = $html."<option value=".substr($option,0,strpos($option,"|")).">".substr($option,strpos($option,"|")+1)."</option>";
}
}
$html = $html."</select></td></tr>";
$html = $html."<input type='hidden' value='".count($myarray)."' name='hCount'>";
//$html = $html."<input type='hidden' value='".$id."' name='id'>";
$html = $html."<input type='hidden' value='".($SectionNo)."' name='hSectionNo'>".$village;
}
} //for loop
}
else //Did you fail Validation?! Hilarious,try again.
{
$html = $html.$htmlf;
}
//$html = $html."<tr><td></td><td></td></tr></table></DIV>";
$html = $html."<tr><td align='left'><input type='submit' value='submit'></td></tr></table>";
$html = $html."</form>";
} //if (isset
}
//***********end Create entry form
//*********** Let's create navigation for other Sections
{
if ($settings[$NextQname]==true)
{
$exit = "";
//echo $SectionNo;
if ($SectionNo >1)
{
$exit = $exit."<form action='SectionI.php' method='post'>Previous Section<input type='hidden' value='".($SectionNo-1)."' name='hSectionNo'><input type='submit' value='Go'></form></td><td>";
}
$exit = $exit."<form action='SectionI.php' method='post'>Next Section<input type='hidden' value='".($SectionNo+1)."' name='hSectionNo'>";
$exit = $exit."<input type='submit' value='Go'></form>";
}
else
{
if ($SectionNo >1)
{
$exit = $exit."<form action='SectionI.php' method='post'>Previous Section<input type='hidden' value='".($SectionNo-1)."' name='hSectionNo'><input type='submit' value='submit'></form>";
}
//$exit = "";
}
$Nav = "<form action='SectionI.php' method = 'post'><input type='hidden' value='".($SectionNo)."' name='hSectionNo'>Move to ID: ".$navList."<input type='submit' value='submit'></form>";
$totals = "<form action='SectionI.php' method='post'><input type='hidden' value='X' name = 'hSectionNo'>Output file<input type = 'submit' value='submit'></form>";
}
//************End of Navigation
}
else
{
//************Let's print all Section info to table & export
{
$sql = "Select * From ids ";
$tables = PDO_FetchAll("SELECT name FROM sqlite_master WHERE type='table' and name not like 'IDs' ");
foreach($tables as $row )
{
foreach( $row as $row2 => $row2_value)
{
$sql = $sql.' inner join '.$row2_value.' on ids.id='.$row2_value.'.id ';
}
}
//print_r($tables);
//echo $sql;
//$result = $dbh->query('SELECT * FROM IDs');
//print 'building';
if (file_exists("datafile.csv"))
{
copy("datafile.csv","datafile_copy.csv");
}
$myfile = fopen("datafile.csv", "w") or die("Unable to open file!");
$result = PDO_FetchAll($sql);
//print_r($result);
//echo $result[1][0].' : '.$result[2][1];
$rows = '';
foreach($result as $row )
{
$cols = '<tr>';
$rows=$rows.'<tr>';
$txt='';
foreach( $row as $row2 => $row2_value)
{
$cols= $cols.'<th>'.$row2.'</th>';
$rows = $rows.'<td>'.$row2_value.'</td>';
$txt = $txt.$row2_value.'|';
//echo $row2.' : '.$row2_value;
//print_r($row);
//echo '<br>';
}
$rows = $rows.'</tr>';
$txt = $txt."\n";
fwrite($myfile, $txt);
}
$cols = "<table cellpadding='1' border='1'>".$cols.'</tr>'.$rows.'</table>';
}
fclose($myfile);
$html= $cols."<p style='text-align:left'>Data has been saved to datafile.csv in the 'www' folder of this application</p>";
}
?>
<!DOCTYPE html>
<html>
<head>
<title><?php echo $title; ?></title>
<link rel="stylesheet" type="text/css" href="<?php echo $stylesheet; ?>" />
</head>
<body>
<div id="content">
<div id="header">
<span class="header_name"><?php echo $header; ?></span><br />
<hr><br>
</div>
<div id="left_col">
<?php echo $html; ?>
<?php echo $table; echo $current; ?>
</div>
<div id="right_col">
</div>
<div id="footer">
<br><hr><table cellpadding='5'><tr><td><?echo $Nav;?></td><td><? echo $exit ?></td><td><a href="Start.php">Return to start</a></td><td><? echo $totals?></td></tr></table><hr>
</div>
</div>
</body>
</html>